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 FAQ来看,NumPy的一些函数是由于历史原因而出现的,而这是应该的
只有在SciPy:
What is the difference between NumPy and SciPy?
In an ideal world, NumPy would contain nothing but the array data type and
the most basic operations: indexing, sorting, reshaping, basic
elementwise functions, et cetera. All numerical code would reside in
SciPy. However, one of NumPy’s important goals is compatibility, so NumPy
tries to retain all features supported by either of its predecessors. Thus
NumPy contains some linear algebra functions, even though these more
properly belong in SciPy. In any case, SciPy contains more fully-featured
versions of the linear algebra modules, as well as many other numerical
algorithms. If you are doing scientific computing with python, you should
probably install both NumPy and SciPy. Most new features belong in SciPy
rather than NumPy.
这就解释了为什么scipy.linalg.solve比numpy.linalg.solve提供了一些额外的特性。
我没有看到塞瑟姆·莫顿对相关问题的回答
除了SciPy FAQ中描述的复制主要是为了向后兼容外,NumPy文档中也进一步澄清了这一点
可选的scipy加速例程(numpy.dual)
可由Scipy加速的函数的别名。
可以构建SciPy以使用加速或改进的库
FFTs,线性代数和特殊函数。这个模块允许
开发人员透明地支持这些加速功能时
SciPy是可用的,但仍然支持只安装了SciPy的用户
NumPy。
为了简单起见,这些是:
线性代数
FFT
一类改进的0阶贝塞尔函数
此外,从SciPy教程:
SciPy的顶层还包含NumPy和
numpy.lib.scimath。但是,最好直接从
而是NumPy模块。
因此,对于新的应用程序,您应该更喜欢使用在SciPy顶层复制的NumPy版本的数组操作。对于上面列出的域,您应该选择SciPy中的域,并在必要时检查NumPy中的向后兼容性。
根据我个人的经验,我使用的大多数数组函数都存在于NumPy的顶层(random除外)。然而,所有特定于领域的例程都存在于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。统计数据等。