Issue
How do I change the float precision for an entire array without having to do np.set_printoptions
? It is not about printing; I want the values to be compressed to do equality checks with other arrays.
e = np.array([0.8292222222222225, 0.1310000000000003])
to
e = np.array([0.829225, 0.131003])
I want to be able to compare 0.8292222222222225
with 0.829225
in other arrays.
Cannot get equality as True
>>> e = np.array([0.8292222222222225, 0.1310000000000003])
>>> e[0]
0.8292222222222225
>>> e[0]==0.829225
False
>>>
Solution
You should use the numpy.isclose
function. It allows you to compare elements within a tolerance:
>>> a = np.array([0.8292222222222225, 0.1310000000000003])
>>> b = np.array([0.8293, 0.132])
>>> np.isclose(a, b, atol=1e-3)
array([ True, True])
>>> np.isclose(a, b, atol=1e-4)
array([ True, False])
>>> np.isclose(a, b, atol=1e-5)
array([False, False])
Note that you can use both the relative tolerance and the absolute one.
Answered By - Riccardo Bucco
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.