numpy.ndarray.shape

原文:https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html

译者:飞龙 UsyiyiCN

校对:(虚位以待)

ndarray.shape

数组维数组。

笔记

可以用于“重塑”数组,只要这不需要改变元素的总数

例子

>>> x = np.array([1, 2, 3, 4])
>>> 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