Note, numpy recommends here that np.matrix
should not be used, instead just use arrays:
It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.
If you check out what a[0]
is, you'll see the problem. Let's implement this in a smaller size so that it's easier to visualize:
import numpy as np
# I'm using all zeros here for simplicity
y = np.matrix(np.zeros((5, 10)))
y.shape
(5, 10)
y[0]
matrix([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
y[0]
is a matrix consisting of 1 row and 10 columns:
y[0].shape
(1, 10)
If you use np.array
, you avoid this problem altogether
x = np.zeros((5, 10))
x.shape
(5, 10)
len(x[0])
10
x[0].shape
(10,)