[Numpy-discussion] array indexing problem
Robert Kern
robert.kern at gmail.com
Tue Aug 29 19:13:39 CDT 2006
Rahul Kanwar wrote:
> Hello,
>
> I am trying to extract a column from a 2D array here is what is have
> done:
>
> --------------------------------------------
> In [3]: a = array([[1,2,3],[1,2,3]])
>
> In [4]: a
> Out[4]:
> array([[1, 2, 3],
> [1, 2, 3]])
>
> In [5]: a[:, 1]
> Out[5]: array([2, 2])
>
> In [6]: a[:, 1:2]
> Out[6]:
> array([[2],
> [2]])
> --------------------------------------------
>
> when i use a[:, 1] i get a 1x2 array where as when i use a[:, 1:2] i get
> a 2x1 array. The intuitive behavior of a[:, 1] should be a 2x1 array. Am
> i doing something wrong here or is there some reason for this behavior ?
Indexing reduces the rank of the array. Slicing does not. In the first instance,
you do not get a 1x2 array; you get an array with shape (2,).
This choice dates from the earliest days of Numeric. It ends up being quite
useful in most contexts. However, it is somewhat less so when you want to treat
these arrays as matrices and row and column vectors.
--
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