且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SciPy/NumPy导入指南

更新时间:2023-02-26 18:35:37

我建议您做类似的事情

import numpy as np
import scipy as sp

相反. from ... import *总是很危险的,尤其是对于大型模块,例如numpyscipy.以下说明了原因:

instead. It is always dangerous to do from ... import * especially with large modules such as numpy and scipy. The following illustrates why:

>>> any(['foo'])
True
>>> from scipy import *
>>> any(['foo'])

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
     any(['foo'])
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 1575, in any
    return _wrapit(a, 'any', axis, out)
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 37, in _wrapit
    result = getattr(asarray(obj),method)(*args, **kwds)
TypeError: cannot perform reduce with flexible type

这是怎么回事?标准的python内置函数any被具有不同行为的scipy.any代替.这可能会破坏任何使用标准any的代码.

What happens here? The standard python builtin function any is replaced by scipy.any which has different behavior. That might break any code that uses the standard any.