Getting only particular columns in every row in a numpy array
Question:
Possible Duplicate:
numpy: access an array by column
I have a numpy array (numpy is imported as np)
gona = np.array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
I can get the values of entire column of 1th row by gona[1][:].
array([4, 5, 6])
But if I try to get all values of a particular column of all rows (say I want values of 1st column in every row) I would try the gona[:][1]. But the result I get from this is same as before.
What can be the reason for this? How do I do such a thing in numpy?
Answers:
Like this:
gona = numpy.array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
# List comprehension, just get each element in 'gona', and then get first element in that list
out = [x[0] for x in gona]
print out
Output:
>>>
[1, 4, 7, 10]
>>>
You actually want to do this:
>>> a
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
>>> a[:,1]
array([ 2, 5, 8, 11])
a[:]
just returns the entire array, so then a[:][1]
is returning the second row of a
. I think that’s where your confusion arises.
See this section of the Tentative Numpy Tutorial for more information on indexing multidimensional arrays.
There seems to be a slight confusion in terms of the positioning of the braces, gona[:][1]
first selects everything from the array, and from that array then selects the second row. To select particular columns you put the indices within the same square brackets separated by a comma:
gona = np.array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
gona[1,:]
Out[21]: array([4, 5, 6])
gona[:,1]
Out[22]: array([ 2, 5, 8, 11])
gona[:,0]
Out[23]: array([ 1, 4, 7, 10])
you can also just select a range of rows for instance
gona[0:2,0] # only take the two first rows of the first column
Out[24]: array([2, 5])
Possible Duplicate:
numpy: access an array by column
I have a numpy array (numpy is imported as np)
gona = np.array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
I can get the values of entire column of 1th row by gona[1][:].
array([4, 5, 6])
But if I try to get all values of a particular column of all rows (say I want values of 1st column in every row) I would try the gona[:][1]. But the result I get from this is same as before.
What can be the reason for this? How do I do such a thing in numpy?
Like this:
gona = numpy.array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
# List comprehension, just get each element in 'gona', and then get first element in that list
out = [x[0] for x in gona]
print out
Output:
>>>
[1, 4, 7, 10]
>>>
You actually want to do this:
>>> a
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
>>> a[:,1]
array([ 2, 5, 8, 11])
a[:]
just returns the entire array, so then a[:][1]
is returning the second row of a
. I think that’s where your confusion arises.
See this section of the Tentative Numpy Tutorial for more information on indexing multidimensional arrays.
There seems to be a slight confusion in terms of the positioning of the braces, gona[:][1]
first selects everything from the array, and from that array then selects the second row. To select particular columns you put the indices within the same square brackets separated by a comma:
gona = np.array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
gona[1,:]
Out[21]: array([4, 5, 6])
gona[:,1]
Out[22]: array([ 2, 5, 8, 11])
gona[:,0]
Out[23]: array([ 1, 4, 7, 10])
you can also just select a range of rows for instance
gona[0:2,0] # only take the two first rows of the first column
Out[24]: array([2, 5])