官网
incompatible shape for a non-contiguous array
numpy.
reshape
(
a,
newshape,
order='C'
)
[source]
Gives a new shape to an array without changing its data. 在不改变其数据的情况下,为数组提供新的形状(数据不变,形状改变)。
Parameters: | a : array_like a:数组状(阵列状的)
newshape : int or tuple of ints 新形状:整型或整型元组
order : {‘C’, ‘F’, ‘A’}, optional 顺序:{‘C’, ‘F’, ‘A’}, 可选
|
---|---|
Returns: | reshaped_array : ndarray 返回 改变后形状数组:ndarray(类型)
|
See also
ndarray.reshape
Notes 注意
It is not always possible to change the shape of an array without copying the data. If you want an error to be raised when the data is copied, you should assign the new shape to the shape attribute of the array:
在不复制数据的情况下,改变数组的形状并不总是可能的。如果希望在复制数据时引发错误,则应将新形状赋给数组的形状属性:
>>> a = np.zeros((10, 2))
# A transpose makes the array non-contiguous 转置使得数组不相邻。
>>> b = a.T
# Taking a view makes it possible to modify the shape without modifying
# the initial object.采用视图可以修改形状而不修改初始对象。
>>> c = b.view()
>>> c.shape = (20)
AttributeError: incompatible shape for a non-contiguous array # attributeerror:a的不相容的非连续阵列形状
(C:\ProgramData\Anaconda3) C:\Users\Administrator>python
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.
1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = np.zeros((10, 2))
Traceback (most recent call last):
File "", line 1, in
NameError: name 'np' is not defined
>>> import numpy as np
>>> a = np.zeros((10, 2))
>>> a
array([[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]])
>>> b = a.T
>>> b
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> c = b.view()
>>> c
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> c.shape = (20)
Traceback (most recent call last):
File "", line 1, in
AttributeError: incompatible shape for a non-contiguous array
>>>
The order keyword gives the index ordering both for fetching the values from a, and then placing the values into the output array. For example, let’s say you have an array:
顺序关键词给出了从A中取值的索引排序,然后将值放入输出数组中。例如,假设你有一个数组:
>>> a = np.arange(6).reshape((3, 2)) >>> a array([[0, 1], [2, 3], [4, 5]])
You can think of reshaping as first raveling the array (using the given index order), then inserting the elements from the raveled array into the new array using the same kind of index ordering as was used for the raveling.
你可以把整形看作是第一次拆开数组(使用给定的索引顺序),然后使用与拆开一样的索引顺序将从数组中的元素插入到新数组中。
>>> np.reshape(a, (2, 3)) # C-like index ordering array([[0, 1, 2], [3, 4, 5]]) >>> np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape array([[0, 1, 2], [3, 4, 5]]) >>> np.reshape(a, (2, 3), order='F') # Fortran-like index ordering array([[0, 4, 3], [2, 1, 5]]) >>> np.reshape(np.ravel(a, order='F'), (2, 3), order='F') array([[0, 4, 3], [2, 1, 5]])
Examples 实例
>>> a = np.array([[1,2,3], [4,5,6]])
>>> np.reshape(a, 6)
array([1, 2, 3, 4, 5, 6])
>>> np.reshape(a, 6, order='F')
array([1, 4, 2, 5, 3, 6])
>>> np.reshape(a, (3,-1)) # the unspecified value is inferred to be 2
array([[1, 2],
[3, 4],
[5, 6]])
(C:\ProgramData\Anaconda3) C:\Users\Administrator>python
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.
1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.arange(6).reshape((3, 2))
>>> a
array([[0, 1],
[2, 3],
[4, 5]])
>>> a.reshape(6)
array([0, 1, 2, 3, 4, 5])
>>> a.reshape((2,4))
Traceback (most recent call last):
File "", line 1, in
ValueError: cannot reshape array of size 6 into shape (2,4)
>>> a.reshape((2,3))
array([[0, 1, 2],
[3, 4, 5]])
>>> a = np.arange(8)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> a.reshape((2, 2, 2))
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> a.dtype
dtype('int32')
>>> a.reshape((2,2))
Traceback (most recent call last):
File "", line 1, in
ValueError: cannot reshape array of size 8 into shape (2,2)
>>>
新数组的形状(shape)属性应该要与原数组的一致,即新数组元素数量与原数组元素数量要相等。
一个参数为-1时,那么reshape函数会根据另一个参数的维度计算出数组的另外一个shape属性值。
(C:\ProgramData\Anaconda3) C:\Users\Administrator>python
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.
1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16]])
>>> a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
>>> a.shape
(4, 4)
>>> a.reshape(-1,1)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12],
[13],
[14],
[15],
[16]])
>>> a.reshape(2,-1)
array([[ 1, 2, 3, 4, 5, 6, 7, 8],
[ 9, 10, 11, 12, 13, 14, 15, 16]])
>>> a.reshape(1,-1)
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]])
>>> a.reshape(0,-1)
Traceback (most recent call last):
File "", line 1, in
ValueError: cannot reshape array of size 16 into shape (0,newaxis)
>>> a.reshape(3,-1)
Traceback (most recent call last):
File "", line 1, in
ValueError: cannot reshape array of size 16 into shape (3,newaxis)
>>> a.reshape(4,-1)
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
>>>
a.reshape(-1,1)这一行 ,a的形状属性未知,期望a变成(只有)一列,行数未知,
Numpy根据a.reshape(-1,1) 自动计算出行数(16),新数组shape属性为(16, 1),与原来的(4, 4)对应。如果a元素总数不能整除期望行(列)数,报错,例如a.reshape(3,-1)