[Numpy-discussion] take semantics (bug?)

Robert Kern robert.kern at gmail.com
Sun Dec 3 23:41:48 CST 2006

Michael McNeil Forbes wrote:
> What are the semantics of the "take" function?
> I would have expected that the following have the same shape and size:
>>>> a = array([1,2,3])
>>>> inds = a.nonzero()
>>>> a[inds]
> array([1, 2, 3])
>>>> a.take(inds)
> array([[1, 2, 3]])
> Is there a bug somewhere here or is this intentional?

It's a result of a.nonzero() returning a tuple.

In [3]: a.nonzero()
Out[3]: (array([0, 1, 2]),)

__getitem__ interprets tuples specially: a[1,2,3] == a[(1,2,3)], also a[0,] == a[0].

.take() doesn't; it simply tries to convert its argument into an array. It can
convert (array([0, 1, 2]),) into array([[0, 1, 2]]), so it does.

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 Numpy-discussion mailing list