Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

gradient descent python

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 :: get method in python dictionary 
Python :: max deviation in pandas 
Python :: django dumpdata specific app 
Python :: how to make tkinter look modern 
Python :: python override string class 
Python :: Python random integer number between min, max 
Python :: delimiter pandas 
Python :: IQR to remove outlier 
Python :: python datetime minus datetime 
Python :: know the type of variable in python 
Python :: generate random list and find max in list python 
Python :: pandas count number of repetitions of each diferent value 
Python :: how to convert list to all uppercase 
Python :: how to scale an array between two values python 
Python :: pyton recognize any datetime format 
Python :: datetime column only extract date pandas 
Python :: django forms error customize 
Python :: If elif else 
Python :: How do I stop Selenium from closing my browser 
Python :: while loops python 
Python :: django composite primary key 
Python :: download images off unsplash python 
Python :: python typecast 
Python :: python if true 
Python :: how to make a comment in python 
Python :: django login required 
Python :: how to remove an element from a list in python 
Python :: scikit learn to identify highly correlated features 
Python :: python default keyword parameter list 
Python :: calculate quantiles python 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =