numpy 数组维度操作总汇

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)

  • 8
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值