Python - numpy.clip()函数
np.clip( a, a_min, a_max, out=None):
部分参数解释:
该函数的作用是将数组a中的所有数限定到范围a_min和a_max中。
a:输入矩阵;
a_min:被限定的最小值,所有比a_min小的数都会强制变为a_min;
a_max:被限定的最大值,所有比a_max大的数都会强制变为a_max;
out:可以指定输出矩阵的对象,shape与a相同
实例:
x
Out[1]:
array([[1, 8, 4, 2, 7],
[1, 5, 5, 9, 5],
[6, 0, 3, 7, 1],
[4, 9, 1, 3, 1],
[9, 9, 7, 6, 8]])
y = np.clip(x, 4, 6)
y
Out[2]:
array([[4, 6, 4, 4, 6],
[4, 5, 5, 6, 5],
[6, 4, 4, 6, 4],
[4, 6, 4, 4, 4],
[6, 6, 6, 6, 6]])