Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

batch gradient descent

import numpy as np

def hw(w0, w1, x):
    final = w0 + w1*x
    return final

def error(x,y,w0,w1):
    return y - hw(w0, w1 ,x)

def sumError(training_points, w0, w1):
    sum = 0
    for i in training_points:
        sum += error(i[0], i[1], w0, w1)
    return sum

def sumError2(training_points, w0, w1):
    sum = 0
    for i in training_points:
        sum += error(i[0], i[1], w0, w1) * i[0]
    return sum

def updateW0(training_points, w0 , w1, learning_rate):
    final = w0 + learning_rate * sumError(training_points, w0, w1)
    final = np.round(final,round)
    return final

def updateW1(training_points, w0, w1, learning_rate):
    final = w1 + learning_rate * sumError2(training_points, w0, w1)
    final = np.round(final,round)
    return final

def update_iteration(number_of_iterations, training_points, w0, w1, learning_rate):
    print("----Batch Gradient Descend----")
    w0_new = 0
    w1_new = 0
    for i in range(number_of_iterations):
        w0_new = updateW0(training_points, w0, w1, learning_rate)
        w1_new =updateW1(training_points, w0, w1, learning_rate)

        print("--------------------------------")
        print(f"iteration {i+1}: w0 = {w0_new}")
        print(f"iteration {i+1}: w1 = {w1_new}")
        w0 = w0_new
        w1 = w1_new
    print("================================")
    return w0_new, w1_new
  
# round 3 decimal points
round = 3 
    
training_points = [
    (1.5, 1),
    (3.5, 3),
    (3,2),
    (5,3),
    (2,2.5),
]

w0 = 0
w1 = 0
learning_rate = 0.01
number_of_iterations = 3

update_iteration(number_of_iterations,training_points,w0, w1,learning_rate)
Comment

PREVIOUS NEXT
Code Example
Python :: how to use def in python 
Python :: python turtle shapes 
Python :: print animation python 
Python :: how to make tkinter look better 
Python :: remove from list if not maches in list 
Python :: convert utc to gmt+7 pandas 
Python :: loginrequiredmixin django 
Python :: python discord bot 
Python :: numpy reshape 
Python :: iterate through list python with index 
Python :: comment lister les fichiers un dossier avec python 
Python :: indexes meta django 
Python :: django create view 
Python :: switch between frames in tkinter 
Python :: python log file 
Python :: bmi calculator in python 
Python :: Check np.nan value 
Python :: if condition in print statement python 
Python :: calculate mean of column pandas 
Python :: how to get timezone in python 
Python :: assertionerror-accepted-renderer-not-set-on-response-in-django 
Python :: insert data in sqlite database in python 
Python :: how to remove an element from dictionary using his value python 
Python :: python check equality of floats 
Python :: install scrapy on pycharm 
Python :: if statement in python 
Python :: python curl 
Python :: restart python after script execution 
Python :: how to check if string is in byte formate pythin 
Python :: makemigration django 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =