Numpy array index handling

Robert Kern robert.kern at gmail.com
Fri Jul 13 18:00:42 EDT 2007


phishboh at gmail.com wrote:
> Thanks a lot for your reply.
> I'll have a look at the numpy-discussion for future issues.
> 
> FYI, I'm using Python 2.4.3 for Windows (Enthought Edition) and the
> included IPython shell. I found my mistake; importing of pylab.
> E.g., this works
> from pylab import * ; from scipy import * ; y = arange(3) ; y[y>1] = 0
> but not
> from scipy import * ; from pylab import * ; z = arange(3) ; z[z>1] = 0
> 
> Using the 'whos' command, I can see that y is an ndarray type, whereas
> z is an array type.

Ah, this means that matplotlib is configured to use Numeric, numpy's
predecessor, which does not have the capability to index with boolean masks. You
need to change matplotlib's "numerix" setting to "numpy". See its documentation
on how to do that. Also, the Enthought Edition that you downloaded is quite old.
I recommend getting newer versions of the packages.

> (I also see a lot of functions, etc. How can I list just the
> variables?)

Functions are first-class objects in Python, so you are seeing "just the
variables". However, you are probably only interested in a few types. You can
look at the help for %who, which will show you how to restrict the types of
objects that are displayed by that command. Unfortunately, it doesn't have a way
to specifically exclude types. Most likely, you can get by with the following:


In [15]: %who ndarray float int complex str
ALLOW_THREADS   BUFSIZE CLIP    ERR_CALL        ERR_DEFAULT     ERR_DEFAULT2
ERR_IGNORE      ERR_LOG ERR_PRINT
ERR_RAISE       ERR_WARN        FLOATING_POINT_SUPPORT  FPE_DIVIDEBYZERO
FPE_INVALID     FPE_OVERFLOW    FPE_UNDERFLOW   Inf   Infinity
MAXDIMS NAN     NINF    NZERO   NaN     PINF    PZERO   RAISE   SHIFT_DIVIDEBYZERO
SHIFT_INVALID   SHIFT_OVERFLOW  SHIFT_UNDERFLOW UFUNC_BUFSIZE_DEFAULT
UFUNC_PYVALS_NAME       WRAP    e       inf     infty
nan     pi      z


You can assign that to a macro to make it easier to type:

In [16]: %macro mywho 15
Macro `mywho` created. To execute, type its name (without quotes).
Macro contents:
_ip.magic("who ndarray float int complex str")

In [17]: mywho
-------> mywho()
ALLOW_THREADS   BUFSIZE CLIP    ERR_CALL        ERR_DEFAULT     ERR_DEFAULT2
ERR_IGNORE      ERR_LOG ERR_PRINT
ERR_RAISE       ERR_WARN        FLOATING_POINT_SUPPORT  FPE_DIVIDEBYZERO
FPE_INVALID     FPE_OVERFLOW    FPE_UNDERFLOW   Inf   Infinity
MAXDIMS NAN     NINF    NZERO   NaN     PINF    PZERO   RAISE   SHIFT_DIVIDEBYZERO
SHIFT_INVALID   SHIFT_OVERFLOW  SHIFT_UNDERFLOW UFUNC_BUFSIZE_DEFAULT
UFUNC_PYVALS_NAME       WRAP    e       inf     infty
nan     pi      z

> It seems a bit risky to use 'from scipy import *'. Maybe it's better
> to use 'import scipy' and then scipy.arange, to be sure what is
> actually being used? Would there be any disadvanages with that
> approach, other than more use of the keyboard?

There really aren't any besides the extra verbosity. Using "from foo import *"
is often fine for interactive use, but for code that you write in a file, it is
*highly* recommended that you import modules and use them explicitly for the
reasons you mention. You can reduce the typing burden somewhat like so:

  import numpy as N
  a = N.arange(10)

That's a fairly common idiom in the numpy community. You can also import
specific symbols if you know what you are going to use. If you're doing a lot of
trig, for example:

  from numpy import sin, cos, tan, pi

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list