python的numpy.reshape函数运行实例详解

官网

incompatible shape for a non-contiguous array

numpy.reshape

numpy. reshape ( anewshapeorder='C' ) [source]

Gives a new shape to an array without changing its data.  在不改变其数据的情况下,为数组提供新的形状(数据不变,形状改变)。

Parameters:

a : array_like

a:数组状(阵列状的)

Array to be reshaped. 

源数据

newshape : int or tuple of ints

新形状:整型或整型元组

The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

(数据)新的形状应该与原来的形状兼容。如果是整数,则结果将是该长度的一维数组。一个形状尺寸可以是-1。在这种情况下,从数组的长度和剩余维度推断出该值。

order : {‘C’, ‘F’, ‘A’}, optional

顺序:{‘C’, ‘F’, ‘A’}, 可选

Read the elements of a using this index order, and place the elements into the reshaped array using this index order. ‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. ‘A’ means to read / write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.

使用索引顺序,读取a里面的元素(成分),并使用此索引顺序将元素放入重新排列的数组中。“C”是指使用类似C(语言)索引顺序读写元素,最后一个轴索引变化最快,回到第一个轴索引变化最慢。“F”是指使用类似FORTRAN(语言)索引顺序读/写元素,第一个索引变化最快,最后一个索引变化最慢。注意,“C”和“F”选项不考虑底层数组的内存布局,只引用索引的顺序。“A”是指在Fortran类索引顺序中读或写元素,如果A是FORTRAN在存储器中邻接,则类似于C顺序。

Returns:

reshaped_array : ndarray

返回   改变后形状数组:ndarray(类型)

This will be a new view object if possible; otherwise, it will be a copy. Note there is no guarantee of the memory layout (C- or Fortran- contiguous) of the returned array.

如果可能的话,这将是一个新的视图对象;否则,它将是一个副本。注意,返回数组的内存布局(C或类似FORTRAN(的编程语言))没有保证。

See also

ndarray.reshape
Equivalent method. 等效方法。

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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 16 into shape (0,newaxis)
>>> a.reshape(3,-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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)

猜你喜欢

转载自blog.csdn.net/wyx100/article/details/80998320