bobo老师您好,课程让我受益匪浅,感谢。我参照上课的练习在用梯度下降法处理波士顿房产数据这个多元线性回归问题的时候,运行出现了异常,一时搞不明白来请教。以下是代码(Pycharm环境)
def lossFunction(y, X_b, theta):
try:
return np.sum((y - X_b.dot(theta))**2)/len(X_b)
except:
return float("inf")
def dlossFunction(y, X_b, theta):
res = np.empty(len(theta)) #列向量
res[0] = np.sum(X_b.dot(theta) - y)
for i in range(1, len(theta)):
res[i] = np.sum((X_b.dot(theta) - y).T.dot(X_b[:,1]))
return res * 2. / len(X_b)
def gradientDescent(X_b, y, initial_theta, eta, n_iters=1e4, epsilon=1e-8):
theta = initial_theta
i_iter = 0
while i_iter < n_iters:
gradient = dlossFunction(y, X_b, theta)
last_theta = theta
theta = theta - eta * gradient
if abs(lossFunction(y, X_b, theta) - lossFunction(y, X_b, last_theta)) < epsilon:
break
i_iter += 1
return theta
if __name__ == "__main__":
bosten = datasets.load_boston()
X = bosten.data
y = bosten.target
X = X[y<50]
y = y[y<50]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=666)
X_b = np.hstack([np.ones((len(X), 1)), X])
initial_theta = np.zeros(X_b.shape[1])
eta = 0.01
print(gradientDescent(X_b, y, initial_theta, eta))
运行结果:
E:/pycharm/workspace/gradient2.py:16: RuntimeWarning: overflow encountered in square
return np.sum((y - X_b.dot(theta))**2)/len(X_b)
E:/pycharm/workspace/gradient2.py:34: RuntimeWarning: invalid value encountered in double_scalars
if abs(lossFunction(y, X_b, theta) - lossFunction(y, X_b, last_theta)) < epsilon:
E:/pycharm/workspace/gradient2.py:25: RuntimeWarning: overflow encountered in multiply
return res * 2. / len(X_b)
[nan nan nan nan nan nan nan nan nan nan nan nan nan nan]