1

我正在尝试编写多元线性回归。这是我的程序引发错误的代码行:

least = optimize.minimize(residsq(xmat, ylist, coeff), coeff, constraints = ({'type': 'eq', 'fun': sum(resid(xmat, ylist, coeff))}), method = 'BFGS') # Choose the coefficients that minimize the sum of the residuals squared subject to keeping the sum of the residuals equal to 0.

xmat 是一个向量列表:[[3,5,2],[3,1,6],[7,2,3],[9,-2,0]]。ylist 是一个与 xmat 长度相同的列表:[5,2,7,7]。coeff 是系数列表,最初是 [mean(ylist), 0, 0, 0] ([constant, b_0, b_1, b_2])。resid 是每个点的残差列表,residsq 是残差的 N2 范数(平方和的 sqrt)。

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import linregtest
  File "C:\Python33\lib\site-packages\linregtest.py", line 4, in <module>
    out = linreg.multilinreg(xmat, ylist, True)
  File "C:\Python33\lib\site-packages\linreg.py", line 120, in multilinreg
    least = optimize.minimize(residsq(xmat, ylist, coeff), coeff, constraints = ({'type': 'eq', 'fun': sum(resid(xmat, ylist, coeff))}), method = 'BFGS') # Choose the coefficients that minimize the sum of the residuals squared subject to keeping the sum of the residuals equal to 0.
  File "C:\Python33\lib\site-packages\scipy\optimize\_minimize.py", line 302, in minimize
    RuntimeWarning)
  File "C:\Python33\lib\idlelib\PyShell.py", line 60, in idle_showwarning
    file.write(warnings.formatwarning(message, category, filename,
AttributeError: 'NoneType' object has no attribute 'write'

文件来自哪里,如何抑制这个错误?

编辑:解决一个问题,找到另一个。也许你可以帮我确定 SciPy 在哪里调用浮点数?

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import linregtest
  File "C:\Python33\lib\site-packages\linregtest.py", line 4, in <module>
    out = linreg.multilinreg(xmat, ylist, True)
  File "C:\Python33\lib\site-packages\linreg.py", line 123, in multilinreg
    least = optimize.minimize(residsq(xmat, ylist, coeff), coeff, constraints = ({'type': 'eq', 'fun': sumresid(xmat, ylist, coeff)}), method = 'SLSQP') # Choose the coefficients that minimize the sum of the residuals squared subject to keeping the sum of the residuals equal to 0.
  File "C:\Python33\lib\site-packages\scipy\optimize\_minimize.py", line 364, in minimize
    constraints, **options)
  File "C:\Python33\lib\site-packages\scipy\optimize\slsqp.py", line 301, in _minimize_slsqp
    meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['eq']]))
  File "C:\Python33\lib\site-packages\scipy\optimize\slsqp.py", line 301, in <listcomp>
    meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['eq']]))
TypeError: 'float' object is not callable
4

1 回答 1

1

我刚刚编辑了我的 python 3.2 IDLE,PyShell.py(修复了第 59 和 62 行)

def idle_showwarning(message, category, filename, lineno,
                     file=None, line=None):
    if file is None:
        file = sys.stderr #warning_stream
    try:
        file.write(warnings.formatwarning(message, category, filename,
                                          lineno, line=line))

使用sys.stderr而不是warning_stream使用sys.__stderr__. sys.__stderr__在我的情况下是 None 。我不知道为什么要使用全局。

调用warnings.formatwarning有一个额外的无效file关键字。

现在,我打印了警告,例如

>>> import numpy as np
>>> np.uint(1) - np.uint(2)

Warning (from warnings module):
  File "C:\Programs\Python32\Lib\idlelib\idle.pyw", line 1
    try:
RuntimeWarning: overflow encountered in ulong_scalars
>>> 4294967295
>>> 

编辑:

搜索 python 错误报告

http://bugs.python.org/issue12438错误file参数已修复

http://bugs.python.org/issue13582问题sys.__stderr__ is None是开放的

于 2013-06-08T12:31:24.343 回答