Discussion:
[SciPy-user] extract array elements whis where() output
Steve Schmerler
2006-06-21 20:04:54 UTC
Permalink
First of all: what's better (a) post this only on the numpy list (b)
only on the scipy list (I think many scipy users may find the answer to
questions like this one interesting) or (c) post on both.

I can't extract elements from array x with a mask array of indices but
x.take (Numeric style works). I'm sure that I have done such things
before .... and they worked.

In [58]: x
Out[58]: array([0, 0, 1, 2, 3, 0, 0, 9])

In [59]: mask=where(x!=0.0)[0]

In [60]: mask
Out[60]: array([2, 3, 4, 7])

In [61]: x.take(mask)
Out[61]: array([1, 2, 3, 9])

In [62]: x(mask)
---------------------------------------------------------------------------
exceptions.TypeError Traceback (most
recent call last)

/home/elcorto/ode_testdata/<ipython console>

TypeError: 'numpy.ndarray' object is not callable

In [63]: numpy.__version__
Out[63]: '0.9.9.2612'

cheers,
steve
--
Random number generation is the art of producing pure gibberish as
quickly as possible.
Robert Kern
2006-06-21 20:12:55 UTC
Permalink
Post by Steve Schmerler
First of all: what's better (a) post this only on the numpy list (b)
only on the scipy list (I think many scipy users may find the answer to
questions like this one interesting) or (c) post on both.
Probably (a). (c) should never be done except for single-shot announcements.
Post by Steve Schmerler
I can't extract elements from array x with a mask array of indices but
x.take (Numeric style works). I'm sure that I have done such things
before .... and they worked.
In [58]: x
Out[58]: array([0, 0, 1, 2, 3, 0, 0, 9])
In [59]: mask=where(x!=0.0)[0]
In [60]: mask
Out[60]: array([2, 3, 4, 7])
In [61]: x.take(mask)
Out[61]: array([1, 2, 3, 9])
In [62]: x(mask)
---------------------------------------------------------------------------
exceptions.TypeError Traceback (most
recent call last)
/home/elcorto/ode_testdata/<ipython console>
TypeError: 'numpy.ndarray' object is not callable
You meant x[mask], not x(mask).
--
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
Steve Schmerler
2006-06-21 23:31:56 UTC
Permalink
Post by Robert Kern
You meant x[mask], not x(mask).
Of course .... :) Thanks.

cheers,
steve
--
Random number generation is the art of producing pure gibberish as
quickly as possible.
Johannes Loehnert
2006-06-21 20:14:34 UTC
Permalink
Hi,
Post by Steve Schmerler
In [62]: x(mask)
try x[mask]

Johannes
Loading...