【发布时间】:2019-07-15 16:37:53
【问题描述】:
我已尝试搜索遇到的溢出错误,但没有成功。
当我运行这个程序时,我遇到了运行时错误,这对我来说毫无意义。 这是我使用的数据:https://pastebin.com/MLWvUarm
import numpy as np
def loadData():
data = np.loadtxt('data.txt', delimiter=',')
x = np.c_[data[:,0:2]]
y = np.c_[data[:,-1]]
return x, y
def hypothesis(x, theta):
h = x.dot(theta)
return h
def computeCost(x, y, theta):
m = np.size(y, 0)
h = hypothesis(x, theta)
J = (1/(2*m)) * np.sum(np.square(h-y))
return J
def gradient_descent(x, y, theta, alpha, mxIT):
m = np.size(y, 0)
J_history = np.zeros((mxIT, 1))
for it in range(mxIT):
hyp = hypothesis(x, theta)
err = hyp - y
theta = theta - (alpha/m) * (x.T.dot(err))
J_history[it] = computeCost(x, y, theta)
return theta, J_history
def main():
x, y = loadData()
x = np.c_[np.ones(x.shape[0]), x]
theta = np.zeros((np.size(x, 1), 1))
alpha = 0.01
mxIT = 400
theta, j_his = gradient_descent(x, y, theta, alpha, mxIT)
print(theta)
if __name__ == "__main__":
main()
我该如何解决这个问题?
【问题讨论】:
-
能否提供loadData()返回的数据?该算法适用于随机 x 和 y。顺便说一句:使用梯度下降进行简单的线性回归是大材小用。
-
我已经编辑了我的帖子并更新了代码。我知道 GD 是一种矫枉过正,但我只是为了测试,谢谢。 @tstanisl