Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting specific column in each row from array

Tags:

python

numpy

I am trying to select specific column elements for each row of a numpy array. For example, in the following example:

In [1]: a = np.random.random((3,2))
Out[1]: 
array([[ 0.75670668,  0.1283942 ],
       [ 0.51326555,  0.59378083],
       [ 0.03219789,  0.53612603]])

I would like to select the first element of the first row, the second element of the second row, and the first element of the third row. So I tried to do the following:

In [2]: b = np.array([0,1,0])

In [3]: a[:,b]

But this produces the following output:

Out[3]: 
array([[ 0.75670668,  0.1283942 ,  0.75670668],
       [ 0.51326555,  0.59378083,  0.51326555],
       [ 0.03219789,  0.53612603,  0.03219789]])

which clearly is not what I am looking for. Is there an easy way to do what I would like to do without using loops?

like image 521
astrofrog Avatar asked Jan 21 '10 17:01

astrofrog


People also ask

How do I select a specific column from a Numpy array very important?

Select a single element from 2D Numpy Array by index We can use [][] operator to select an element from Numpy Array i.e. Example 1: Select the element at row index 1 and column index 2. Or we can pass the comma separated list of indices representing row index & column index too i.e.

How do I select a column in Numpy?

Access the ith column of a Numpy array using EllipsisPass the ith index along with the ellipsis to print the returned array column.

How do you select a column from a matrix in python?

To select a column of the matrix you can select the elements of the i-th column by scrolling the rows. This instruction scrolls through all the rows of the matrix m and reads the second element of each using the row[1] function. It is the second column of the initial matrix.

How do you select an element in an array?

Use integer array indexing to select an element by index Use the syntax np. array[i,j] to retrieve an element at row index i and column index j from the array. To retrieve multiple elements, use the syntax np.


2 Answers

You can use:

a[np.arange(3), (0,1,0)]

in your example above.

like image 126
Carlos Santos Avatar answered Nov 14 '22 22:11

Carlos Santos


OK, just to clarify here, lets do a simple example

A=diag(arange(0,10,1))

gives

array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 2, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 4, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 5, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 6, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 7, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 8, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 9]])

then

A[0][0:4]

gives

array([0, 0, 0, 0])

that is first row, elements 0 to 3. But

A[0:4][1]

doesn't give the first 4 rows, the 2nd element in each. Instead we get

array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0])

i.e the entire 2nd column.

A[0:4,1]

gives

array([0, 1, 0, 0])

I'm sure there is a very good reason for this and which makes perfect sense to programmers but for those of us uninitiated in that great religion it can be quite confusing.

like image 45
geo Avatar answered Nov 14 '22 23:11

geo