I have an np.array
:
a = np.array([x for x in range(10) for y in range(10)]).reshape(10, 10)
I want to get the 3rd
and the 6th
rows columns 4,7,10
- marked in green.
I tried:
a[[2,5]] # gives me the rows I want
a[[2,5], [3,6,10]] # produces an an error
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,)
In the end the result should look like:
[[2,2,2],
[5,5,5]]
Where is my mistake?
CodePudding user response:
the first index list must the (2,1) shape (or list equivalent):
In [31]: a[[[2], [5]], [3, 6, 9]]
Out[31]:
array([[2, 2, 2],
[5, 5, 5]])
Do you understand what the error message means by broadcasting
?
For simple addition, a (2,1) array broadcasts with a (3,) to produce a (2,3) result:
In [32]: I, J = np.array((2, 5)), np.array((3, 6, 9))
In [33]: I, J
Out[33]: (array([2, 5]), array([3, 6, 9]))
In [34]: I J
Traceback (most recent call last):
Input In [34] in <module>
I J
ValueError: operands could not be broadcast together with shapes (2,) (3,)
In [35]: I[:, None] J
Out[35]:
array([[ 5, 8, 11],
[ 8, 11, 14]])
The same idea applies to indexing with several arrays.
Your a
can be created with the same logic:
In [38]: np.arange(10)[:, None] np.zeros(10,int)
Out[38]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]])
With 2 arrays (lists) of matching size, the effect is to select a "diagonal", or 1d array of values (rather than the block that you were trying to get):
In [39]: a[[2, 3], [5, 6]]
Out[39]: array([2, 3])
In [40]: a[2, 5], a[3, 6]
Out[40]: (2, 3)
CodePudding user response:
I believe this link will explain everything since the question is the same and was given great answer: Selecting specific rows and columns from NumPy array