填充数组。
参数:array: : array_like of rank N
要填充的阵列。
pad_width: : {sequence, array_like, int}
填充到每个轴边的值的数量。 ((before_1,after_1),…(before_N,after_N))每个轴的唯一焊盘宽度。 ((before,after),)对于每个轴在pad之前和之后产生相同的结果。 (pad)或int是所有轴前=后=垫宽度的快捷方式。
mode: : str 或 function, 可选参数
以下字符串值之一或用户提供的函数。
‘constant’(默认)
具有恒定值的打击垫。
‘edge’
用数组的边值填充。
‘linear_ramp’
线性斜坡在end_value和阵列边值之间填充。
‘maximum’
沿每个轴填充全部或部分矢量的最大值。
‘mean’
沿每个轴填充全部或部分矢量的平均值。
‘median’
沿每个轴填充全部或部分向量的中值。
‘minimum’
沿每个轴填充全部或部分矢量的最小值。
‘reflect’
具有矢量反射的填充沿每个轴在矢量的第一个和最后一个值上镜像。
‘symmetric’
具有矢量反射的填充沿阵列的边镜像。
‘wrap’
沿轴以向量的换行填充。第一个值用于填充结尾,而结束值用于填充开头。
‘empty’
具有未定义值的打击垫。
1.17版中的新功能。
填充功能,请参见注释。
stat_length: : sequence 或 int, 可选参数
在‘maximum’,‘mean’,‘median’和‘minimum’中使用。用于计算统计值的每个轴的边处的值数。
((before_1,after_1),…(before_N,after_N))每个轴的唯一统计长度。
((before,after),)在每个轴的统计长度前后产生相同的结果。
(stat_length,)或int是所有轴统计长度之前=之后=的快捷方式。
默认为None,以使用整个轴。
constant_values: : sequence 或 scalar, 可选参数
在‘constant’中使用。用于设置每个轴的填充值的值。
((before_1, after_1), ... (before_N, after_N))每个轴的唯一填充常数。
((before, after),)每个轴的常数前后均相同。
(constant,)或者constant是的快捷方式before = after = constant对于所有轴。
默认值为0。
end_values: : sequence 或 scalar, 可选参数
在‘linear_ramp’中使用。用于linear_ramp结束值的值将形成填充数组的边。
((before_1, after_1), ... (before_N, after_N))每个轴的唯一最终值。
((before, after),)每个轴的最终值前后均相同。
(constant,)或者constant是的快捷方式before = after = constant对于所有轴。
默认值为0。
reflect_type: : {‘even’, ‘odd’}, 可选参数
在‘reflect’和‘symmetric’中使用。 ‘even’样式是默认样式,其边值周围的反射不变。对于‘odd’样式,通过从两倍边值中减去反射值来创建数组的扩展部分。
返回值:pad: : ndarray
等于的填充数组array形状根据pad_width增加。
注意:
1.7.0版中的新功能。
对于等级大于1的数组,后一个轴的某些填充是根据前一个轴的填充来计算的。这是最容易想到的秩2数组,其中通过使用来自第一个轴的填充值来计算填充数组的角。
如果使用padding函数,则应就地修改等级1数组。它具有以下签名:
padding_func(vector, iaxis_pad_width, iaxis, kwargs)
哪里
vector : ndarray
A rank 1 array already padded with zeros. Padded values are
vector[:iaxis_pad_width[0]] and vector[-iaxis_pad_width[1]:].
iaxis_pad_width : tuple
A 2-tuple of ints, iaxis_pad_width[0] represents the number of
values padded at the beginning of vector where
iaxis_pad_width[1] represents the number of values padded at
the end of vector.
iaxis : int
The axis currently being calculated.
kwargs : dict
Any keyword arguments the function requires.
例子:
>>> a = [1, 2, 3, 4, 5]
>>> np.pad(a, (2, 3), 'constant', constant_values=(4, 6))
array([4, 4, 1, ..., 6, 6, 6])
>>> np.pad(a, (2, 3), 'edge')
array([1, 1, 1, ..., 5, 5, 5])
>>> np.pad(a, (2, 3), 'linear_ramp', end_values=(5, -4))
array([ 5, 3, 1, 2, 3, 4, 5, 2, -1, -4])
>>> np.pad(a, (2,), 'maximum')
array([5, 5, 1, 2, 3, 4, 5, 5, 5])
>>> np.pad(a, (2,), 'mean')
array([3, 3, 1, 2, 3, 4, 5, 3, 3])
>>> np.pad(a, (2,), 'median')
array([3, 3, 1, 2, 3, 4, 5, 3, 3])
>>> a = [[1, 2], [3, 4]]
>>> np.pad(a, ((3, 2), (2, 3)), 'minimum')
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]])
>>> a = [1, 2, 3, 4, 5]
>>> np.pad(a, (2, 3), 'reflect')
array([3, 2, 1, 2, 3, 4, 5, 4, 3, 2])
>>> np.pad(a, (2, 3), 'reflect', reflect_type='odd')
array([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> np.pad(a, (2, 3), 'symmetric')
array([2, 1, 1, 2, 3, 4, 5, 5, 4, 3])
>>> np.pad(a, (2, 3), 'symmetric', reflect_type='odd')
array([0, 1, 1, 2, 3, 4, 5, 5, 6, 7])
>>> np.pad(a, (2, 3), 'wrap')
array([4, 5, 1, 2, 3, 4, 5, 1, 2, 3])
>>> def pad_with(vector, pad_width, iaxis, kwargs):
... pad_value = kwargs.get('padder', 10)
... vector[:pad_width[0]] = pad_value
... vector[-pad_width[1]:] = pad_value
>>> a = np.arange(6)
>>> a = a.reshape((2, 3))
>>> np.pad(a, 2, pad_with)
array([[10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10],
[10, 10, 0, 1, 2, 10, 10],
[10, 10, 3, 4, 5, 10, 10],
[10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10]])
>>> np.pad(a, 2, pad_with, padder=100)
array([[100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100],
[100, 100, 0, 1, 2, 100, 100],
[100, 100, 3, 4, 5, 100, 100],
[100, 100, 100, 100, 100, 100, 100],
[100, 100, 100, 100, 100, 100, 100]])