努比。Python中的clip()

numpy.clip() 函数用于剪裁(限制)数组中的值。

null

给定一个间隔,间隔之外的值将被剪裁到间隔边。例如,如果指定了[0,1]的间隔,则小于0的值变为0,大于1的值变为1。

语法: 努比。剪辑(a、a_最小值、a_最大值、输出=无)

参数: a: 包含要剪辑的元素的数组。 阿敏: 最小值。 –>如果没有,则不会在下间隔边上执行剪裁。a_min和a_max中不能有超过一个为无。 a_max: 最大值。 –>如果没有,则不会在上间隔边上执行剪裁。a_min和a_max中不能有超过一个为无。 –>如果一个_min或一个_max类似于数组,那么三个数组将被广播以匹配它们的形状。 出: 结果将放置在此数组中。它可能是就地剪切的输入阵列。out必须具有正确的形状以保持输出。它的类型被保留下来。

返回: 削波阵列

代码#1:

# Python3 code demonstrate clip() function
# importing the numpy
import numpy as np
in_array = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
print ( "Input array : " , in_array)
out_array = np.clip(in_array, a_min = 2 , a_max = 6 )
print ( "Output array : " , out_array)


输出:

Input array :  [1, 2, 3, 4, 5, 6, 7, 8]
Output array :  [2 2 3 4 5 6 6 6]

代码#2:

# Python3 code demonstrate clip() function
# importing the numpy
import numpy as np
in_array = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
print ( "Input array : " , in_array)
out_array = np.clip(in_array, a_min = [ 3 , 4 , 1 , 1 , 1 , 4 , 4 , 4 , 4 , 4 ],
a_max = 9 )
print ( "Output array : " , out_array)


输出:

Input array :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output array :  [3 4 3 4 5 6 7 8 9 9]
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享