Relationship between SciPy and NumPy

前端 未结 8 1761
清酒与你
清酒与你 2020-11-27 09:34

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 alm

相关标签:
8条回答
  • 2020-11-27 10:08

    From the SciPy Reference Guide:

    ... all of the Numpy functions have been subsumed into the scipy namespace so that all of those functions are available without additionally importing Numpy.

    The intention is for users not to have to know the distinction between the scipy and numpy namespaces, though apparently you've found an exception.

    0 讨论(0)
  • 2020-11-27 10:14

    Last time I checked it, the scipy __init__ method executes a

    from numpy import *
    

    so that the whole numpy namespace is included into scipy when the scipy module is imported.

    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.


    EDIT: 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 *
    

    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.
    """
    

    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.

    0 讨论(0)
提交回复
热议问题