np.expand_dims()
阿新 • • 發佈:2018-12-17
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)
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)
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)
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)
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)
能在(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.