我正在尝试实现此算法以查找单个变量的截距和斜率:
这是我更新拦截和斜率的Python代码.但它并没有趋同.RSS随着迭代而不是减少而增加,并且在一些迭代之后它变得无限.我没有发现任何实现算法的错误.我怎么能解决这个问题?我也附上了csv文件.这是代码.
import pandas as pd
import numpy as np
#Defining gradient_decend
#This Function takes X value, Y value and vector of w0(intercept),w1(slope)
#INPUT FEATURES=X(sq.feet of house size)
#TARGET VALUE=Y (Price of House)
#W=np.array([w0,w1]).reshape(2,1)
#W=[w0,
# w1]
def gradient_decend(X,Y,W):
intercept=W[0][0]
slope=W[1][0]
#Here i will get a list
#list is like this
#gd=[sum(predicted_value-(intercept+slope*x)),
# sum(predicted_value-(intercept+slope*x)*x)]
gd=[sum(y-(intercept+slope*x) for x,y in zip(X,Y)),
sum(((y-(intercept+slope*x))*x) for x,y in zip(X,Y))]
return np.array(gd).reshape(2,1)
#Defining Resudual sum of squares
def RSS(X,Y,W):
return sum((y-(W[0][0]+W[1][0]*x))**2 for x,y in zip(X,Y))
#Reading …Run Code Online (Sandbox Code Playgroud) python numpy machine-learning linear-regression gradient-descent