属性
char.chararray.
shape
数组维度的元组。
shape属性通常用于获取数组的当前形状,但也可以用于通过为数组指定数组维数的元组来就地调整数组的形状。和一样 numpy.reshape ,其中一个新形状维度可以是-1,在这种情况下,它的值是根据数组的大小和其余维度推断出来的。如果需要副本,则在适当位置重新整形数组将失败。
numpy.reshape
参见
相似功能
ndarray.reshape
相似方法
实例
>>> 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 >>> np.zeros((4,2))[::2].shape = (-1,) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Incompatible shape for in-place modification. Use `.reshape()` to make a copy with the desired shape.