SciPy/NumPy import guideline SciPy/NumPy import guideline numpy numpy

SciPy/NumPy import guideline


I recommend doing something like

import numpy as npimport 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.


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.


What about making classes and use just what you will need, fx: class one:

import cv2from SIGBWindows import SIGBWindowsfrom SIGBAssg import *

class two:

import cv2import numpy as npfrom pylab import *from scipy.cluster.vq import *from scipy.misc import imresize

class three:

import cv2import numpy as np

and Finally where we call the object:

import cv2from SIGBWindows import SIGBWindowsfrom SIGBAssg import *windows = SIGBWindows(mode="video")windows.openVideo("somevideo.avi")kmeans(windows)

I don't know if it is what you are looking for, but this approach it makes the code really clean and easy to add more features to it.