Indices returned by where()

Dear list, apologies if the answer to my question is obvious... Is the following intentional? $>python Python 2.4 (#1, Mar 22 2005, 21:42:42) [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
import numpy as np print np.__version__ 1.0
x = np.array([1., 2., 3., 4., 5.])
idx = np.where(x > 6.) print len(idx) 1
The reason is of course that where() returns a tuple of index arrays instead of simply an index array:
print idx (array([], dtype=int32),)
Does that mean that one always has to explicitely request the first element of the returned tuple in order to check how many matches were found, even for 1d arrays? What's the reason for designing it that way? Many thanks, Christian

On 12/6/06, Christian Marquardt <christian@marquardt.sc> wrote:
Dear list,
apologies if the answer to my question is obvious...
Is the following intentional?
$>python
Python 2.4 (#1, Mar 22 2005, 21:42:42) [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
import numpy as np print np.__version__ 1.0
x = np.array([1., 2., 3., 4., 5.])
idx = np.where(x > 6.) print len(idx) 1
The reason is of course that where() returns a tuple of index arrays instead of simply an index array:
print idx (array([], dtype=int32),)
Does that mean that one always has to explicitely request the first element of the returned tuple in order to check how many matches were found, even for 1d arrays? What's the reason for designing it that way?
Fancy indexing. In [1]: a = arange(10).reshape(2,5) In [2]: i = where(a>3) In [3]: i Out[3]: (array([0, 1, 1, 1, 1, 1]), array([4, 0, 1, 2, 3, 4])) In [4]: a[i] = 0 In [5]: a Out[5]: array([[0, 1, 2, 3, 0], [0, 0, 0, 0, 0]]) If you just want a count, try In [6]: a = arange(10).reshape(2,5) In [7]: sum(a>3) Out[7]: 6 Chuck
participants (2)
-
Charles R Harris
-
Christian Marquardt