numpy增加维度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
array([[[[1],
         [2],
         [3]],

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

        [[4],
         [5],
         [6]]]])

axis = -1 也就是相当于在最后插入 (1, 2, 3, 1)

你可能感兴趣的:(numpy,numpy,python)