numpy.pd 用于对数组上下左右(2d)进行扩充
语法结构:numpy.pad(array, pad_width, mode='constant', **kwargs)
参数说明:
array——表示需要填充的数组;
pad_width——表示每个轴(axis)边缘需要填充的数值数目。
参数输入方式为:((before_1, after_1), … (before_N, after_N)),其中(before_1, after_1)表示第1轴两边缘分别填充before_1个和after_1个数值。取值为:{sequence, array_like, int}
mode——表示填充的方式(取值:str字符串或用户提供的函数),总共有11种填充模式;
三种方式,单个int
,表示上下左右都填充这么多,如果是一个(int a ,int b)
表示每个轴开始都这样填充,或者((int a , int b ), (int c, int d))
np.expand_dims:用于扩展数组的形状
np.expand_dims(a, axis=0)
表示在0位置添加数据,转换结果如下:
import numpy as np
a = np.array([[[1, 2, 3], [4, 5, 6]]])
a.shape
Out[2]: (1, 2, 3)
np.expand_dims(a, axis=1)
表示在1位置添加数据,转换结果如下:
c = np.expand_dims(a, axis=1)
c
Out[3]:
array([[[[1, 2, 3],
[4, 5, 6]]]])
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)
axis = -1
也就是相当于在最后插入
a = np.array([[[1,2,3],[4,5,6]]])
b = np.expand_dims(a,axis=-1)
print(b.shape)
(1, 2, 3, 1)