我有找到最小成本函数的代码,但是当我添加阈值以在局部最小值处停止梯度下降时,变得低于错误,有人可以帮忙。
cannot convert the series to <class 'float'>
Input
Math CS
92 98
56 68
88 81
70 80
代码:-
def gradient_descent(x,y):
iterations=50
n=len(x)
exponent = -9
m_curr=b_curr=0
learning_rate=0.01
for i in range(iterations):
y_pred=m_curr*x+b_curr
cost=(1/n)*sum([val**2 for val in (y-y_pred)])
md=-(2/n)*sum(x*(y-y_pred))
bd=-(2/n)*sum(y-y_pred)
m_curr=m_curr-learning_rate*md
b_curr=b_curr-learning_rate*bd
math.isclose(x,y,rel_tol=math.exp(exponent),abs_tol=0.0)
print("m {},b {},cost {},iteration {},learning_rate {}".format(m_curr,b_curr,cost,i,learning_rate)) x=df['math'] y=df['cs'] gradient_descent(x,y)