Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SciPy/NumPy import guideline

Notice: I checked for duplicate and nothing clearly answers my question. I trust you'll let me know if I missed something!

In an effort to clean up my code, I have been looking for a standard convention for importing SciPy and NumPy in my programs. I know there is no strict guideline and I can do it the way I want, but from time to time, I still find contradictory instructions.

For example, I've read somewhere that NumPy is meant to only implement the array object, while SciPy is there for every other scientific algorithms. So NumPy should be used for array operation and SciPy for everything else... On the other hand, SciPy imports every Numpy functions in its main namespace, such that scipy.array() is the same thing as numpy.array() (see this question), so NumPy should only be used when SciPy is not being used, as they are duplicates...

What is the recommended way to work with SciPy and NumPy? Being a scientist, sqrt(-1) should return a complex number, so I'm inclined to go with SciPy only.

Right now, my code starts with:

import numpy as np
from scipy import *
from matplotlib import pyplot as plt

I use scipy for mathematical operation (such as log10()) and numpy for array creation/operations (such as np.zeros()). Would it be fine to go all the way with SciPy and never import NumPy explicitly? Will a future update remove NumPy's array manipulation from SciPy?

like image 369
PhilMacKay Avatar asked Mar 20 '13 16:03

PhilMacKay


People also ask

Is SciPy compatible with NumPy?

SciPy takes a somewhat conservative approach, maintaining compatibility with several major releases of Python and NumPy on the major platforms.

Is SciPy better than NumPy?

NumPy has a faster processing speed than other python libraries. SciPy on the other hand has slower computational speed. NumPy is basically for basic operations such as sorting, indexing, and elementary functioning on the array data type.

How do you reference SciPy documents?

If SciPy has been significant in your research, and you would like to acknowledge the project in your academic publication, we suggest citing the following paper: Pauli Virtanen, Ralf Gommers, Travis E.


2 Answers

I recommend doing something like

import numpy as np
import scipy as sp

instead. It is always dangerous to do from ... import * especially with large modules such as numpy and scipy. The following illustrates why:

>>> any(['foo'])
True
>>> from scipy import *
>>> any(['foo'])

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
     any(['foo'])
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 1575, in any
    return _wrapit(a, 'any', axis, out)
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 37, in _wrapit
    result = getattr(asarray(obj),method)(*args, **kwds)
TypeError: cannot perform reduce with flexible type

What happens here? The standard python builtin function any is replaced by scipy.any which has different behavior. That might break any code that uses the standard any.

like image 123
Johan Råde Avatar answered Sep 22 '22 06:09

Johan Råde


This post has some good information about the two modules (Relationship between scipy and numpy). It seems that Numpy's functionality is meant to be completely included within Scipy, although there are a few exceptions (see post). I would say it is safe to simply use Scipy for all of your needs since most important things like mathematical functions, arrays, and other things are included within Scipy.

like image 20
astromax Avatar answered Sep 20 '22 06:09

astromax