SciPy和NumPy之间的关系 [英] Relationship between SciPy and NumPy

查看:597
本文介绍了SciPy和NumPy之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SciPy似乎在其自己的名称空间中提供了NumPy的大多数(但不是全部[1])功能.换句话说,如果有一个名为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.

对于loglog2logn可以说相同,但是对于log1p [2]则不能.

The same can be said about log, log2 and logn, but not about log1p [2].

另一方面,对于同一ufunc,numpy.expscipy.exp似乎是不同的名称. scipy.log1pnumpy.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 vs 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.minnumpy.maxnumpy.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.
"""

似乎该模块覆盖了sqrtloglog2lognlog10powerarccosarcsinarctanh的基本numpy ufuncs.这就解释了您所看到的行为.这样做的根本设计原因可能埋在某个地方的邮件列表中.

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之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆