【发布时间】:2019-03-22 04:26:55
【问题描述】:
问题
我已经写了这段代码,但是这给出了错误:
RuntimeWarning:在乘法中遇到溢出
t2_temp = sum(x*(y_temp - y))RuntimeWarning: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")
【问题讨论】:
-
尝试按一个因子缩放列中的值,然后运行它。很可能列值太大,导致溢出。
-
您使用的是哪个版本的 Python?
标签: python machine-learning linear-regression gradient-descent