import numpy as np
a = np.random.rand(3,4,5)
a.shape
What's the result of this?
a[0].shape
And this?
a[...,2].shape
a[1,0,3]
Like all other things in Python, numpy indexes from 0.
a[3,2,2].shape
a[:,2].shape
Indexing into numpy arrays usually results in a so-called view.
a = np.zeros((4,4))
a
Let's call b
the top-left $2\times 2$ submatrix.
b = a[:2,:2]
b
What happens if we change b
?
b[1,0] = 5
b
print(a)
To decouple b
from a
, use .copy()
.
b = b.copy()
b[1,1] = 7
print(a)
You can also index with other arrays:
a = np.random.rand(4,4)
a
i = np.array([0,2])
a[i]
And with conditionals:
a>0.5
a[a>0.5]