【问题标题】:Overflow error encountered in double scalars双标量中遇到溢出错误
【发布时间】:2021-10-08 15:25:53
【问题描述】:

您好,我正在尝试进行线性回归,当我尝试运行代码时会发生这种情况 代码是:

    import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('train.csv')
df = data.dropna()
X = np.array(df.x)
Y = np.array(df.y)
def compute_error(x,y,m,b):
    error = 0
    for i in range(len(x)):
        error+= (y[i]-(m*x[i]+b))**2
    return error / float(len(x))

def step_graident_descent(x,y,m,b , alpha):
    N = float(len(x))
    b_graident = 0
    m_graident = 0
    for i in range(0 , len(x)):
        x = X[i]
        y = Y[i]
        b_graident +=(-2/N) * (y-(m*x+b))
        m_graident += (-2/N) * x*(y-(m*x+b))
    new_m = m - alpha*m_graident
    new_b = b - alpha*b_graident

    return new_m , new_b
def graident_decsent(x,y,m,b,num_itters,alpha):
    for i in range(num_itters):
        m,b = step_graident_descent(x,y,m,b,alpha)
    return m,b
def run():
    b=0
    m=0
    numberOfIttertions = 1000
    m,b = graident_decsent(X , Y ,m,b,numberOfIttertions , 0.001)
    print(m,b)
    
if __name__ == '__main__':
    run()   

我得到的错误是:

    linearRegression.py:22: RuntimeWarning: overflow encountered in double_scalars
  m_graident += (-2/N) * x*(y-(m*x+b))
linearRegression.py:21: RuntimeWarning: invalid value encountered in double_scalars
  b_graident +=(-2/N) * (y-(m*x+b))
linearRegression.py:22: RuntimeWarning: invalid value encountered in double_scalars
  m_graident += (-2/N) * x*(y-(m*x+b))

如果有人可以帮助我,我会非常高兴,因为我坚持了大约两个月,谢谢

【问题讨论】:

  • 可以加个Minimal Reproducible Example吗?
  • 当然,但我认为这已经足够了,谢谢
  • 嗯,由于您没有提供输入数据,因此无法重现。
  • 那么你想让我如何添加它是 ariund 700 输入的数据,它是一个 csv 所以你想让我如何提供它

标签: python pandas numpy machine-learning linear-regression


【解决方案1】:

编辑:tl;dr 解决方案

好的,这是我所说的最小可重现示例。我将您的 X,Y 替换为以下内容。

n = 10**2
X = np.linspace(0,10**6,n)
Y = 1.5*X+0.2*10**6*np.random.normal(size=n)

如果我再跑

b=0
m=0
numberOfIttertions = 1000
m,b = graident_decsent(X , Y ,m,b,numberOfIttertions , 0.001)

我明白你描述的问题。唯一令人惊讶的是解决方案的简单性。我只是将您的 alpha 替换为 10**-14,一切正常。

为什么以及如何给出一个最小的、可重现的例子

您的示例无法重现,因为我们没有 train.csv。通常,为了自己理解您的问题并获得具体答案,非常有一个非常小的示例供人们运行和修改它是很有帮助的。例如。也许您可以考虑对回归进行更短的输入,这也会导致此错误。

第一次运行时警告

但是现在回答你的问题。你的第一个 RuntimeWarning

    linearRegression.py:22: RuntimeWarning: overflow encountered in double_scalars
  m_graident += (-2/N) * x*(y-(m*x+b))

表示x,因此m_graident 的类型为numpy.double=numpy.float64。此数据类型可以存储(-1.79769313486e+308, 1.79769313486e+308). 范围内的数字,如果您变大或变小,则称为溢出。例如。 np.double(1.79769313486e+308) 仍然可以,但如果你将它乘以 1.1,你会得到你最喜欢的运行时警告。请注意,这“只是”一个警告并且仍在运行。但它不能给你一个数字,因为它太大了。相反,它会给你inf

其他 RuntimeWarnings

好的,但是有什么作用

linearRegression.py:21: RuntimeWarning: invalid value encountered in double_scalars
  b_graident +=(-2/N) * (y-(m*x+b))

是什么意思?

它来自于我刚才提到的无限计算。一些无穷大的计算是有效的。

np.inf-10**6 -> inf
np.inf+10**6 -> inf
np.inf/10**6 -> inf
np.inf*10**6 -> inf
np.inf*(-10**6) -> -inf
1/np.inf -> 0
np.inf *np.inf -> inf

但有些不是并给出nan,即不是数字。

np.inf/np.inf 
np.inf-np.inf 

这些在数学中被称为不确定形式,因为它取决于你如何到达无穷大你会得到什么。例如

(np.double(1e+309)+np.double(1e+309))-np.double(1e+309)
np.double(1e+309)-(np.double(1e+309)+np.double(1e+309))

都是inf-inf,但你会期待不同的结果。 获得nan 是不幸的,因为使用nan 的计算总是产生nan。一旦添加了nan,就不能再使用渐变了。

其他资源

另一种选择是使用现有的线性回归实现。例如。来自scikit-learn。见

【讨论】:

  • 非常感谢您,我看到您在此感谢您花费了时间和精力!
  • @majduddinalboon 解决了。再次查看我的答案。
猜你喜欢
  • 2011-11-25
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 2020-08-30
  • 1970-01-01
相关资源
最近更新 更多