numpy中如何改变数组维度呢?
写在前面
所有的重排原则:
从原数组最深维度开始依次取元素排到转换后数组最深维度处
1、reshape & resize & shape 改变数组维度
reshape函数:不改变原数组维度,有返回值
resize函数:直接改变原数组维度,无返回值
shape属性:直接改变原数组维度
>>> import numpy as np
>>> a=np.arange(12)
>>> a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> a.reshape(2,6)
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
>>> a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> a.reshape(2,3,2)
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]]])
>>> a.resize(2,6)
>>> a
>>> array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
>>> a.shape=(2,6)
>>> a
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
>>> a.shape=(2,3,2)
>>> a
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]]])
>>>
2、ravel & flatten 将数组变为一维
两个函数都不会改变原数组维度
区别在于:ravel、flatt函数都返回一维数组的一个视图(View)
但是flatten函数还会请求分配内存来保存结果
>>> a
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]]])
>>> a.ravel()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> a
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]]])
>>> a.flatten()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> a
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]]])
3 transpose & T 矩阵转置
transpose函数与T 属性功能一致:不改变原数组,有转置后的返回值,且一维数组返回值为它本身
>>> a
array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]])
>>> a.T
array([[ 0, 2, 4, 6, 8, 10],
[ 1, 3, 5, 7, 9, 11]])
>>> a
array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]])
>>> a.transpose()
array([[ 0, 2, 4, 6, 8, 10],
[ 1, 3, 5, 7, 9, 11]])
>>> a
array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]])
4、细说transpose函数
当维度大于等于三维时,transpose函数可以交换维度顺序
当transpose()参数为空时,默认参数是维度序号的倒序排列
以三维为例 transpose() 等价于 transpose(2,1,0) 即深度变为行,行变为深度
>>> a
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]]])
#深度变为行,行变为深度
>>> a.transpose()
array([[[ 0, 6],
[ 2, 8],
[ 4, 10]],
[[ 1, 7],
[ 3, 9],
[ 5, 11]]])
>>> a.transpose(2,1,0)
array([[[ 0, 6],
[ 2, 8],
[ 4, 10]],
[[ 1, 7],
[ 3, 9],
[ 5, 11]]])
#列变为行,行变为列
>>> a.transpose(1,0,2)
array([[[ 0, 1],
[ 6, 7]],
[[ 2, 3],
[ 8, 9]],
[[ 4, 5],
[10, 11]]])
#深度变为列,列变为深度
>>> a.transpose(0,2,1)
array([[[ 0, 2, 4],
[ 1, 3, 5]],
[[ 6, 8, 10],
[ 7, 9, 11]]])
5、reshape & np.squeeze 去除值为1的维度轴
numpy.squeeze(a,axis=None)
a: 数组
axis: 指定数组a中要删除的值为1的轴
>>> a=np.array([[[1],[2]],[[3],[4]]])
>>> a
array([[[1],
[2]],
[[3],
[4]]])
>>> a.shape
(2, 2, 1)
>>> a=np.array([[[[1],[2]],[[3],[4]]]])
>>> a.shape
(1, 2, 2, 1)
>>> a
array([[[[1],
[2]],
[[3],
[4]]]])
>>> a.reshape(2,2).shape
(2, 2)
>>> a.reshape(1,2,2).shape
(1, 2, 2)
>>> a.reshape(2,2,1).shape
(2, 2, 1)
>>> np.squeeze(a)
array([[1, 2],
[3, 4]])
>>> np.squeeze(a).shape
(2, 2)
>>> np.squeeze(a,axis=0).shape
(2, 2, 1)
>>> np.squeeze(a,axis=3).shape
(1, 2, 2)
6 、reshape & np.newaxis 增加值为1的维度轴
>>> a=np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
[3, 4]])
>>> a.shape
(2, 2)
>>> a.reshape((1,)+a.shape).shape
(1,2,2)
>>> a.reshape((1,)+a.shape+(1,))
(1,2,2,1)
>>> a[...,np.newaxis].shape
(2, 2, 1)
>>> a[np.newaxis,...,np.newaxis].shape
(1, 2, 2, 1)