请选择 进入手机版 | 继续访问电脑版
查看: 1164|回复: 1

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

[复制链接]

665

主题

1234

帖子

6566

积分

xdtech

Rank: 5Rank: 5

积分
6566
发表于 2020-5-25 17:03:27 | 显示全部楼层 |阅读模式
原始数组:
import numpy as npIn [12]:a = np.array([[[1,2,3],[4,5,6]]])a.shapeOut[12]1, 2, 3)
np.expand_dims(a, axis=0)表示在0位置添加数据,转换结果如下:
In [13]:b = np.expand_dims(a, axis=0)bOut[13]:array([[[[1, 2, 3],         [4, 5, 6]]]])In [14]:b.shapeOut[14]1, 1, 2, 3)
np.expand_dims(a, axis=1)表示在1位置添加数据,转换结果如下:
In [15]:c = np.expand_dims(a, axis=1)cOut[15]:array([[[[1, 2, 3],         [4, 5, 6]]]])In [16]:c.shapeOut[16]1, 1, 2, 3)
np.expand_dims(a, axis=2)表示在2位置添加数据,转换结果如下:
In [17]:d = np.expand_dims(a, axis=2)dOut[17]:array([[[[1, 2, 3]],        [[4, 5, 6]]]])In [18]:d.shapeOut[18]1, 2, 1, 3)
np.expand_dims(a, axis=3)表示在3位置添加数据,转换结果如下:
In [19]:e = np.expand_dims(a, axis=3)eIn [20]:e.shapeOut[20]1, 2, 3, 1)
能在(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.






回复

使用道具 举报

665

主题

1234

帖子

6566

积分

xdtech

Rank: 5Rank: 5

积分
6566
 楼主| 发表于 2020-5-25 17:03:33 | 显示全部楼层
作者:caoqi95
链接:https://www.jianshu.com/p/da10840660cb
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表