numpy.clip用法


numpy.clip

numpy.clip(a, a_min, a_max, out=None)

官方解释:Clip (limit) the values in an array.
[a_min, a_max] 里面的数被保留下来,外面的被截取为a_min或者a_max
例如,如果指定间隔为[0, 1],则小于0的值赋值为0,并且大于1的值赋值为1 。

用法

import numpy as np
a = np.arange(10)
# 则小于1的值变为1,并且大于8的值变为8。
np.clip(a,1,8)
array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8])
np.clip(a, 3, 6, out=a)
#可以把结果放置在此数组中
print(a)
[3 3 3 3 4 5 6 6 6 6]
a = np.arange(10)
#可以用数组进行逐个比较,小于数组中的数时就返回数组的数,要求数组的size要相同
np.clip(a, [3,4,1,1,1,4,4,4,4,4], 8)
array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8])

转载自:https://blog.csdn.net/bubble_story/article/details/79530058

You may also like...