Reputation: 342
Consider the following code:
arr = np.random.randn(3,3)
print(arr)
>>> array([[-0.46734444, 0.56493252, -0.26263957],
[-0.08779416, -2.29891065, -0.49095811],
[-1.75290304, 0.21304592, -0.91466817]])
I tried to get the top right 2 X 2 square matrix using these 2 methods:
print(arr[:2,1:])
print(arr[:2][1:])
>>> [[ 0.56493252 -0.26263957]
[-2.29891065 -0.49095811]]
>>> [[-0.08779416 -2.29891065 -0.49095811]]
However, the 2nd method gave wrong answer. I do not understand the behaviour of 2nd method. Please explain!!
Upvotes: 0
Views: 33
Reputation: 5012
arr[:2]
will give you a list of row[0]
and row[1]
print(arr[:2,1:])
row[0]
and row[1]
and taking all columns starting from the 1st column from the 2 rows.print(arr[:2][1:])
row[0]
and row[1]
via arr[:2]
Hope this helps.
Upvotes: 2