import numpy as np
ndim shape size reshape
a = np.arange(10)
a # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a.ndim # 1
a.shape # (10,)
a.size # 10
A = np.ones(shape=(3, 5)) # 3行5列的二维数组,值全为1
A
A.ndim # 2
A.shape # (3, 5)
A.size # 15
a = np.array([[1, 2, 3],
[4, 5]])
a # array([list([1, 2, 3]), list([4, 5])], dtype=object)
a.ndim # 1
a.shape # (2,)
a.size # 2
a = np.arange(10)
a # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
X = a.reshape(2, 5)
X
X.ndim # 2
X.shape # (2, 5)
a.reshape(5, -1) # 5行,列自己计算
a.reshape(-1, 2) # 2列,行自己计算
# a.reshape(-1, 3) # ValueError: cannot reshape array of size 10 into shape (3)
取值
X = np.arange(15).reshape(3, 5)
X # 15个数,分成3行5