[Python] numpy.ndarray.shape

时间:2023-03-09 18:59:22
[Python] numpy.ndarray.shape

ndarray.shape

Tuple of array dimensions.

x = np.array([1, 2, 3, 4])
print x.shape
#(4, ) y = np.zeros((2, 3, 4))
y.shape
#(2, 3, 4)
y.shape = (3, 8)
y
#array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
# [ 0., 0., 0., 0., 0., 0., 0., 0.],
# [ 0., 0., 0., 0., 0., 0., 0., 0.]])
y.shape = (3, 6)
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#ValueError: total size of new array must be unchanged