Scipy Minimize使用NoneType

时间:2013-06-07 18:27:16

标签: python scipy python-idle linear-regression

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

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

1 个答案:

答案 0 :(得分: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__是无。我不知道为什么使用全局。

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参数已修复

sys.__stderr__ is None的{​​{1}}问题已打开