问题描述
我有一个一维 numpy
数组,我需要确定任何值是零还是非常接近。
通过这一行,我可以快速检查零:
I have a one dimensional numpy
array for which I need to find out if any value is zero or very close to it.With this line I can check for zeros rapidly:
if 0. in my_array:
# do something
,但是我也有一些很小的元素,例如1.e-22,我也想处理设为零(否则我会得到进一步的被零除警告)
but I also have very small elements like 1.e-22 which I would also like to treat as zeros (otherwise I get a Divide by zero warning further down the road)
说我的阈值为1.e-6,我想有效检查我数组中的 any 值是否小于该值。我该怎么做呢?
Say my threshold is 1.e-6 and I want to efficiently check if any value in my array is smaller than that. How can I do this?
推荐答案
没有理由在Python中循环;因为没有理由在Python中循环。只是播放Abs和<并使用:
There's no reason to loop in Python; just broadcast the abs and the < and use np.any
:
np.any(np.absolute(my_array) < eps)
这篇关于检查数组中的值是否等于或非常接近零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!