np.expand_dims()

目录

np.expand_dims:用于扩展数组的形状

axis=0

axis=1 

axis=2

axis=3

axis=4

axis=-1 


np.expand_dims:用于扩展数组的形状

原始数组:

import numpy as np
 
In [12]:
a = np.array([[[1,2,3],[4,5,6]]])
a.shape
Out[12]:
(1, 2, 3)

 原位置:(0, 1, 2, 3...)

axis=0

 np.expand_dims(a, axis=0)表示在原0位置前添加数据,转换结果如下:

In [13]:
b = np.expand_dims(a, axis=0)
b
Out[13]:
array([[[[1, 2, 3],
         [4, 5, 6]]]])
 
In [14]:
b.shape
Out[14]:
(1, 1, 2, 3)

axis=1 

np.expand_dims(a, axis=1)表示在原1位置前添加数据,转换结果如下:

In [15]:
c = np.expand_dims(a, axis=1)
c
Out[15]:
array([[[[1, 2, 3],
         [4, 5, 6]]]])
 
In [16]:
c.shape
Out[16]:
(1, 1, 2, 3)

axis=2

np.expand_dims(a, axis=2)表示在原2位置前添加数据,转换结果如下:

In [17]:
d = np.expand_dims(a, axis=2)
d
Out[17]:
array([[[[1, 2, 3]],
        [[4, 5, 6]]]])
 
In [18]:
d.shape
Out[18]:
(1, 2, 1, 3)

axis=3

np.expand_dims(a, axis=3)表示在原3位置前添加数据,转换结果如下:

In [19]:
e = np.expand_dims(a, axis=3)
e
 
In [20]:
e.shape
Out[20]:
(1, 2, 3, 1)

axis=4

能在(1,2,3)中插入的位置总共为4个,再添加就会出现以下的警告,要不然也会在后面某一处提示AxisError。

In [21]:
f = np.expand_dims(a, axis=4)
D:\ProgramData\Anaconda3\envs\dlnd\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: Both axis > a.ndim and axis < -a.ndim - 1 are deprecated and will raise an AxisError in the future.
  """Entry point for launching an IPython kernel.

axis=-1 

 axis = -1 也就是相当于在最后1一维后插入,就变成了:

a = np.array([[[1,2,3],[4,5,6]]])
b = np.expand_dims(a,axis=-1)
print(b.shape)
(1, 2, 3, 1)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值