首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scipy Minimize使用NoneType

Scipy Minimize使用NoneType
EN

Stack Overflow用户
提问于 2013-06-08 02:27:16
回答 1查看 991关注 0票数 1

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

代码语言:javascript
复制
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。resid是每个点的残差列表,residsq是残差的N2范数(平方和的sqrt)。

代码语言:javascript
复制
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在哪里调用了一个浮点?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-08 20:31:24

我刚刚编辑了我的Python3.2IDLE,PyShell.py (修复了59和62行)

代码语言:javascript
复制
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而不是使用sys.__stderr__的全局warning_stream。在我的例子中,sys.__stderr__是None。我不知道为什么要使用全局变量。

warnings.formatwarning的调用包含额外的无效file关键字。

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

代码语言:javascript
复制
>>> 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 Nonehttp://bugs.python.org/issue13582问题尚未解决

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16990664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档