## numpy-discussion

 [Numpy-discussion] array indexing problem From: Rahul Kanwar - 2006-08-29 23:58:18 ```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 ? regards, Rahul ```
 [Numpy-discussion] array indexing problem From: Rahul Kanwar - 2006-08-30 00:05:08 ```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 ? regards, Rahul ```
 Re: [Numpy-discussion] array indexing problem From: Bill Baxter - 2006-08-30 00:02:26 ```That's just the way it works in numpy. Slices return arrays of lower rank. If you want arrays that behave like they do in linear algebra you can use 'matrix' instead. Check out the Numpy for Matlab users page for more info on array vs. matrix. http://www.scipy.org/NumPy_for_Matlab_Users --bb On 8/30/06, 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 ? > > regards, > Rahul > > > > ------------------------------------------------------------------------- > Using Tomcat but need to do more? Need to support web services, security? > Get stuff done quickly with pre-integrated technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Numpy-discussion mailing list > Numpy-discussion@... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > ```
 Re: [Numpy-discussion] array indexing problem From: Charles R Harris - 2006-08-30 00:11:16 Attachments: Message as HTML ```On 8/29/06, 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 ? The behaviour is expected. a[:,1] is returned with one less dimension, just as for a one dimensional array b[1] is zero dimensional (a scalar). For instance In [65]: int64(2).shape Out[65]: () You can get what you expect using matrices: In [67]: a = mat(arange(6).reshape(2,3)) In [68]: a[:,1] Out[68]: matrix([[1], [4]]) But generally it is best to just use arrays and get used to the conventions. regards, > Rahul Chuck ```
 Re: [Numpy-discussion] array indexing problem From: Sven Schreiber - 2006-08-30 09:58:07 ```Charles R Harris schrieb: > You can get what you expect using matrices: > ... > But generally it is best to just use arrays and get used to the conventions. > Well, there are different views on this subject, and I'm happy that the numpy crew is really trying (and good at it) to make array *and* matrix users happy. So please let us coexist peacefully. -sven ```
 Re: [Numpy-discussion] array indexing problem From: Robert Kern - 2006-08-30 00:13:54 ```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 ```