numpy.reshape

numpy.reshape

函数的原型

numpy.reshape(a, newshape, order='C')

参数解析

a:输入一个将要被reshape的数组

newshape:两种情况:1)整数;2)元组

如果是整数的话,那么这个结果将会变成1-D,也就是一维的形式;

注意 维度可以是-1,这种情况表示,numpy可以根据数组的长度以及剩下的维度数来自动推断维度的数量。

order:

{'C','F','A'},这里的参数是可以选的;这个表示的是元素读取的顺序;

C,C-like order, 它的意思是,最后一个坐标轴变化最快,第一个坐标轴变换最慢;

F,Frotran-like index order, 指的是,第一个坐标轴变化最快,最后一个坐标周变换最慢;

这里的C和F并没有考虑要reshape的数组的内存组织,仅仅是索引的顺序;

A指的是,如果是Fortran的连续内存,则采用类似Frotran-index order,否则采用C-like order。

return 返回

返回reshape后的数组;

例1:
a = np.zeros((10, 2))
b = a.T  ## A transpose makes the array non-contiguousc = b.view() #这样的话是无法修改C的形状的;
c.shape = (20)
会返回错误:
Traceback (most recent call last):
...AttributeError: Incompatible shape for in-place modification. Use `.reshape()` to make a copy with the desired shape
例2:
a = np.arange(6).reshape((3, 2))
输出:a
array([[0, 1],
	   [2, 3],
	   [4, 5]])

可以将整形看作是首先对数组进行拆散(使用给定的索引顺序),然后使用与拆散使用相同的索引顺序将元素从拆散的数组插入到新数组中。
posted @ 2020-12-19 11:39  hi_mxd  阅读(277)  评论(0编辑  收藏  举报