问题
我已经写了这段代码,但是它给出了错误:
RuntimeWarning: overflow encountered in multiply
t2_temp = sum(x*(y_temp - y))
RuntimeWarning: overflow encountered in double_scalars
t1_temp = sum(y_temp - y)
我应该使用特征缩放还是我的代码有问题?
代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def gradient_descent(x,y,t1,t2,repeat,alpha):
n = x.size
for i in range(repeat):
y_temp = x*t2 + t1
t1_temp = sum(y_temp - y)
t2_temp = sum(x*(y_temp - y))
t1 = t1 - alpha * (t1_temp/n)
t2 = t2 - alpha * (t2_temp/n)
return [t1,t2]
d = pd.read_csv("train.csv")
x = d['GrLivArea']
y = d['SalePrice']
x = (np.array(x.values))
y = (np.array(y.values))
alpha = 0.047
repeat = 3000
theta = [1.23154644,1.654132398]
tt = gradient_descent(x,y,theta[0],theta[1],repeat,alpha)
print("FINISH")
最佳答案
您的代码没问题; x
和 y
太大了。您可以执行标准化或某种其他形式的缩放。
关于python - 机器学习梯度下降python实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55292917/