问题描述
SciPy 似乎在其自己的命名空间中提供了大部分(但不是全部 [1])NumPy 的函数.换句话说,如果有一个名为 numpy.foo
的函数,那么几乎可以肯定有一个 scipy.foo
.大多数时候,两者看起来完全一样,甚至经常指向同一个函数对象.
SciPy appears to provide most (but not all [1]) of NumPy's functions in its own namespace. In other words, if there's a function named numpy.foo
, there's almost certainly a scipy.foo
. Most of the time, the two appear to be exactly the same, oftentimes even pointing to the same function object.
有时,它们是不同的.举一个最近出现的例子:
Sometimes, they're different. To give an example that came up recently:
numpy.log10
是一个 ufunc 为否定参数返回 NaN;scipy.log10
返回负参数的复数值并且看起来不是 ufunc.
numpy.log10
is a ufunc that returns NaNs for negative arguments;scipy.log10
returns complex values for negative arguments and doesn't appear to be a ufunc.
对于log
、log2
和logn
也可以这样说,但对于log1p
则不然[2].
The same can be said about log
, log2
and logn
, but not about log1p
[2].
另一方面,对于同一个 ufunc,numpy.exp
和 scipy.exp
似乎是不同的名称.scipy.log1p
和 numpy.log1p
也是如此.
On the other hand, numpy.exp
and scipy.exp
appear to be different names for the same ufunc. This is also true of scipy.log1p
and numpy.log1p
.
另一个例子是 numpy.linalg.solve
与 scipy.linalg.solve
.它们很相似,但后者比前者提供了一些额外的功能.
Another example is numpy.linalg.solve
vs scipy.linalg.solve
. They're similar, but the latter offers some additional features over the former.
为什么明显重复?如果这意味着将 numpy
批量导入到 scipy
命名空间中,为什么在行为和缺少函数方面存在细微差别?是否有一些总体逻辑可以帮助消除混淆?
Why the apparent duplication? If this is meant to be a wholesale import of numpy
into the scipy
namespace, why the subtle differences in behaviour and the missing functions? Is there some overarching logic that would help clear up the confusion?
[1] numpy.min
、numpy.max
、numpy.abs
和其他一些在 中没有对应项scipy
命名空间.
[1] numpy.min
, numpy.max
, numpy.abs
and a few others have no counterparts in the scipy
namespace.
[2] 使用 NumPy 1.5.1 和 SciPy 0.9.0rc2 进行测试.
[2] Tested using NumPy 1.5.1 and SciPy 0.9.0rc2.
推荐答案
上次查了一下,scipy的__init__
方法执行了一个
Last time I checked it, the scipy __init__
method executes a
from numpy import *
以便在导入 scipy 模块时将整个 numpy 命名空间包含到 scipy 中.
so that the whole numpy namespace is included into scipy when the scipy module is imported.
您描述的 log10
行为很有趣,因为 两个 版本都来自 numpy.一个是ufunc
,另一个是numpy.lib
函数.为什么 scipy 比 ufunc
更喜欢库函数,我不知道.
The log10
behavior you are describing is interesting, because both versions are coming from numpy. One is a ufunc
, the other is a numpy.lib
function. Why scipy is preferring the library function over the ufunc
, I don't know off the top of my head.
事实上,我可以回答 log10
问题.查看 scipy __init__
方法我看到了这个:
In fact, I can answer the log10
question. Looking in the scipy __init__
method I see this:
# 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
.查看该代码,它说:
The log10
function you get in scipy comes from numpy.lib.scimath
. Looking at that code, it says:
"""
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
.这解释了您所看到的行为.这样做的底层设计原因可能隐藏在某处的邮件列表帖子中.
It seems that module overlays the base numpy ufuncs for sqrt
, log
, log2
, logn
, log10
, power
, arccos
, arcsin
, and arctanh
. That explains the behavior you are seeing. The underlying design reason why it is done like that is probably buried in a mailing list post somewhere.
这篇关于SciPy 和 NumPy 之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!