随笔记录——numpy.pad()

1.直接对列表两端进行填充
a = list(range(1, 6))

np.pad(array=a, pad_width=(2, 3), mode='constant', constant_values=(100, -100))
Out[12]: array([ 100,  100,    1,    2,    3,    4,    5, -100, -100, -100])

其中

  • array: 需要pad的列表
  • pad_width: 填充到每个轴边缘的数值,在这里,就是说在 左边填充两个元素,在右边填充3 个元素
  • mode:
    • constant: 默认, 表示填充常数
    • edge: 用数组的边值填充。
    • linear_ramp: 在end_值和数组边缘值之间使用线性渐变填充。
    • maximum: 沿每个轴的向量的全部或部分的最大值。
    • mean: 沿着每个轴的全部或部分向量的平均值。
    • median: 沿每个轴用全部或部分向量的中值填充。
    • minimum: 沿每个轴的全部或部分矢量的最小值。
    • reflect: Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.
    • wrap: Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.
    • symmetric: Pads with the reflection of the vector mirrored along the edge of the array
  • constant_values: 用于“常量”。设置每个轴的填充值的值。
使用edge模式填充
np.pad(a, (2, 3), 'edge')
Out[13]: array([1, 1, 1, 2, 3, 4, 5, 5, 5, 5])
使用linear_ramp模式填充
np.pad(a, (2, 3), 'linear_ramp', end_values=(5, -4))
Out[14]: array([ 5,  3,  1,  2,  3,  4,  5,  2, -1, -4])
使用maximum模式填充
np.pad(a, (2,), 'maximum')
Out[15]: array([5, 5, 1, 2, 3, 4, 5, 5, 5])
使用mean模式填充
np.pad(a, (2,), 'mean')
Out[16]: array([3, 3, 1, 2, 3, 4, 5, 3, 3])
使用median模式填充
np.pad(a, (2,), 'median')
Out[17]: array([3, 3, 1, 2, 3, 4, 5, 3, 3])
使用minimum模式填充
np.pad(array=a, pad_width=((3, 2), (2, 3)), mode='minimum')
Out[28]: 
array([[1, 1, 1, 2, 1, 1, 1],
       [1, 1, 1, 2, 1, 1, 1],
       [1, 1, 1, 2, 1, 1, 1],
       [1, 1, 1, 2, 1, 1, 1],
       [3, 3, 3, 4, 3, 3, 3],
       [1, 1, 1, 2, 1, 1, 1],
       [1, 1, 1, 2, 1, 1, 1]])

这里在指定pad_width时,指定了多个轴: 其中

  • (3, 2) 代表 上面填充3行,下面填充2行
  • (2, 3) 代表 左边填充2列,右边填充3列。
    使用最小值填充。
使用reflect模式填充
np.pad(a, (2, 3), 'reflect')
Out[30]: array([3, 2, 1, 2, 3, 4, 5, 4, 3, 2])

np.pad(a, (2, 3), 'reflect', reflect_type='odd')
Out[31]: array([-1,  0,  1,  2,  3,  4,  5,  6,  7,  8])

第二种填充,增加了reflect_type参数
reflect_type: 用于“reflect”和“symmetric”。“even”样式是默认样式,在边值周围具有不变的反射。对于“odd”样式,阵列的扩展部分是通过将反射值从边缘值的两倍中减去来创建的

使用symmetric模式进行填充
np.pad(a, (2, 3), 'symmetric')
Out[32]: array([2, 1, 1, 2, 3, 4, 5, 5, 4, 3])
np.pad(a, (2, 3), 'symmetric', reflect_type='odd')
Out[33]: array([0, 1, 1, 2, 3, 4, 5, 5, 6, 7])
使用wrap模式进行填充
np.pad(a, (5, 3), 'wrap')
Out[35]: array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3])

参考: https://docs.scipy.org/doc/numpy-1.17.0/reference/generated/numpy.pad.html
暂时完结。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值