SciPy似乎在它自己的命名空间中提供了NumPy的大部分(但不是所有[1])函数。换句话说,如果有一个名为numpy的函数。Foo,几乎可以肯定有一个sciy。Foo。大多数情况下,两者看起来完全相同,甚至经常指向相同的函数对象。
有时候,它们是不同的。举一个最近出现的例子:
numpy。log10是一个返回负参数nan的ufunc;
scipy。Log10返回负参数的复杂值,并且看起来不是一个ufunc。
对于log、log2和logn也是如此,但对于log1p[2]则不然。
另一方面,numpy。Exp和scipy。Exp似乎是同一个ufunc的不同名称。scipy也是如此。Log1p和numpy.log1p。
另一个例子是numpy.linalg.solve vs scipy.linalg.solve。它们很相似,但后者比前者提供了一些额外的功能。
为什么会出现明显的重复?如果这意味着将numpy大量导入到scipy名称空间中,那么为什么会出现行为上的细微差异和缺少函数呢?是否有一些总体逻辑可以帮助理清混乱?
[1] numpy。分钟,numpy。马克斯,numpy。Abs和其他一些名称在scipy名称空间中没有对应的名称。
[2]使用NumPy 1.5.1和SciPy 0.9.0rc2测试。
选自“数量经济学”讲座
SciPy是一个包,它包含各种构建在NumPy之上的工具,使用NumPy的数组数据类型和相关功能
事实上,当我们导入SciPy时,我们也得到了NumPy,这可以从SciPy初始化文件中看到
# Import numpy symbols to scipy name space
import numpy as _num
linalg = None
from numpy import *
from numpy.random import rand, randn
from numpy.fft import fft, ifft
from numpy.lib.scimath import *
__all__ = []
__all__ += _num.__all__
__all__ += ['randn', 'rand', 'fft', 'ifft']
del _num
# Remove the linalg imported from numpy so that the scipy.linalg package can be
# imported.
del linalg
__all__.remove('linalg')
但是,显式地使用NumPy功能是更常见和更好的实践
import numpy as np
a = np.identity(3)
SciPy中有用的是其子包中的功能吗
scipy。优化,scipy,整合,scipy。统计数据等。
选自“数量经济学”讲座
SciPy是一个包,它包含各种构建在NumPy之上的工具,使用NumPy的数组数据类型和相关功能
事实上,当我们导入SciPy时,我们也得到了NumPy,这可以从SciPy初始化文件中看到
# Import numpy symbols to scipy name space
import numpy as _num
linalg = None
from numpy import *
from numpy.random import rand, randn
from numpy.fft import fft, ifft
from numpy.lib.scimath import *
__all__ = []
__all__ += _num.__all__
__all__ += ['randn', 'rand', 'fft', 'ifft']
del _num
# Remove the linalg imported from numpy so that the scipy.linalg package can be
# imported.
del linalg
__all__.remove('linalg')
但是,显式地使用NumPy功能是更常见和更好的实践
import numpy as np
a = np.identity(3)
SciPy中有用的是其子包中的功能吗
scipy。优化,scipy,整合,scipy。统计数据等。
上次检查时,scipy __init__方法执行了一个
from numpy import *
以便在导入scipy模块时将整个numpy名称空间包含到scipy中。
您描述的log10行为很有趣,因为两个版本都来自numpy。一个是ufunc,另一个是numpy。自由的功能。为什么scipy更喜欢库函数而不是ufunc,我不知道在我的头顶上。
编辑:事实上,我可以回答log10的问题。在scipy __init__方法中,我看到了以下内容:
# Import numpy symbols to scipy name space
import numpy as _num
from numpy import oldnumeric
from numpy import *
from numpy.random import rand, randn
from numpy.fft import fft, ifft
from numpy.lib.scimath import *
在scipy中获得的log10函数来自numpy.lib.scimath。看看这段代码,它说:
"""
Wrapper functions to more user-friendly calling of certain math functions
whose output data-type is different than the input data-type in certain
domains of the input.
For example, for functions like log() with branch cuts, the versions in this
module provide the mathematically valid answers in the complex plane:
>>> import math
>>> from numpy.lib import scimath
>>> scimath.log(-math.exp(1)) == (1+1j*math.pi)
True
Similarly, sqrt(), other base logarithms, power() and trig functions are
correctly handled. See their respective docstrings for specific examples.
"""
该模块似乎覆盖了sqrt, log, log2, logn, log10, power, arccos, arcsin和arctanh的基本numpy ufuncs。这就解释了你所看到的行为。这样做的潜在设计原因可能隐藏在某个邮件列表帖子中。