Hi all,
A couple of times I've been confused by numpy.where(), and I think part of it comes from the docstring. Searching my gmail archive seems to indicate I'm not the only one bitten by this.
Compare:
In [14]: pdoc numpy.where Class Docstring: where(condition, | x, y)
The result is shaped like condition and has elements of x and y where condition is respectively true or false. If x or y are not given, then it is equivalent to condition.nonzero().
To group the indices by element, rather than dimension, use
transpose(where(condition, | x, y))
instead. This always results in a 2d array, with a row of indices for each element that satisfies the condition.
with (b is just any array):
In [17]: pdoc b.nonzero Class Docstring: a.nonzero() returns a tuple of arrays
Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values can be obtained with a[a.nonzero()].
To group the indices by element, rather than dimension, use transpose(a.nonzero()) instead. The result of this is always a 2d array, with a row for each non-zero element.;
The sentence "The result is shaped like condition" in the where() docstring is misleading, since the behavior is really that of nonzero(). Where() *always* returns a tuple, not an array shaped like condition. If this were more clearly explained, along with a simple example for the usual case that seems to trip everyone:
In [21]: a=arange(10)
In [22]: N.where(a>5) Out[22]: (array([6, 7, 8, 9]),)
In [23]: N.where(a>5)[0] Out[23]: array([6, 7, 8, 9])
I think we'd get a lot less confusion.
Or am I missing something, or just being dense (quite likely)?
Cheers,
f
Fernando Perez wrote:
Hi all,
A couple of times I've been confused by numpy.where(), and I think part of it comes from the docstring. Searching my gmail archive seems to indicate I'm not the only one bitten by this.
Compare:
In [14]: pdoc numpy.where Class Docstring: where(condition, | x, y)
The result is shaped like condition and has elements of x and y where condition is respectively true or false. If x or y are not given, then it is equivalent to condition.nonzero(). To group the indices by element, rather than dimension, use transpose(where(condition, | x, y)) instead. This always results in a 2d array, with a row of indices for each element that satisfies the condition.
with (b is just any array):
In [17]: pdoc b.nonzero Class Docstring: a.nonzero() returns a tuple of arrays
Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values can be obtained with a[a.nonzero()]. To group the indices by element, rather than dimension, use transpose(a.nonzero()) instead. The result of this is always a 2d array, with a row for each non-zero element.;
The sentence "The result is shaped like condition" in the where() docstring is misleading, since the behavior is really that of nonzero(). Where() *always* returns a tuple, not an array shaped like condition. If this were more clearly explained, along with a simple example for the usual case that seems to trip everyone:
In [21]: a=arange(10)
In [22]: N.where(a>5) Out[22]: (array([6, 7, 8, 9]),)
In [23]: N.where(a>5)[0] Out[23]: array([6, 7, 8, 9])
I think we'd get a lot less confusion.
Or am I missing something, or just being dense (quite likely)?
That sentence applies to the 3-argument form, which has nothing to do with nonzero() and does not yield a tuple. But in general, yes, the docstring leaves much to be desired.
On 12/09/2007, Robert Kern robert.kern@gmail.com wrote:
That sentence applies to the 3-argument form, which has nothing to do with nonzero() and does not yield a tuple. But in general, yes, the docstring leaves much to be desired.
Well, here's what I hope is a step in the right direction.
Anne
Yes, the docs could be clearer (and thanks Ann, that's better), but I'm not sure that's the core problem....
- If x and y are not given, condition.nonzero() is returned. This has
- the effect of returning a tuple suitable for fancy indexing.
Why is this a special case of where? This just seems weird to me. Shouldn't that live somewhere else?
-Chris
Christopher Barker wrote:
Yes, the docs could be clearer (and thanks Ann, that's better), but I'm not sure that's the core problem....
- If x and y are not given, condition.nonzero() is returned. This has
- the effect of returning a tuple suitable for fancy indexing.
Why is this a special case of where? This just seems weird to me.
It was introduced in numarray. I don't know why.
Shouldn't that live somewhere else?
And it does: nonzero().