【问题标题】:Overflow encountered in square方中遇到溢出
【发布时间】: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

标签: python numpy overflow


【解决方案1】:

加载x后,尝试除以均值,看是否收敛。平均文档链接:numpy.mean

...
x, y = loadData()
x = x / x.mean(axis=0, keepdims=True)
x = np.c_[np.ones(x.shape[0]), x]
...

目前它似乎存在分歧,这会产生 numpy 抱怨的非常高的错误。您可以从 J_history 中维护的成本历史记录中看到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-15
    • 2020-08-30
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多