Issue
Let's say I have these two variables:
import numpy as np
a = np.array([[ 0, 1, 10, 2, 5]])
b = np.array([[ 0, 1, 18, 15, 5],
[13, 9, 23, 3, 22],
[ 2, 10, 17, 4, 8]])
Method 1
m1 = -np.linalg.norm(a[:, np.newaxis, :] - b[np.newaxis, :, :], axis=-1) ** 2 / 2
Method 2
m2 = -np.sum(np.square(a[:, np.newaxis, :] - b[np.newaxis, :, :]), axis=-1) / 2
Both of these outputs look alike (at least according to print()
):
array([[-116.5, -346. , -73.5]])
But
>>> np.array_equal(m1, m2)
False
What makes it interesting is that defining a literal to check equality leads to:
>>> sanity_check = np.array([[-116.5, -346. , -73.5]])
>>> np.array_equal(sanity_check, m1)
False
>>> np.array_equal(sanity_check, m2)
True
How come using the method with np.linalg.norm
is the odd one out? If m1
is unequal, how come its print()
looks equal?
Solution
There is a tiny amount of numerical error in the actual values:
>>> m1.tolist()
[[-116.49999999999999, -346.0, -73.5]]
>>> m2.tolist()
[[-116.5, -346.0, -73.5]]
This is because:
>>> np.sqrt(8**2 + 13**2)**2
232.99999999999997
>>> 8**2 + 13**2
233
NumPy's printoptions
controls how many digits are printed. By default, they are:
>>> np.get_printoptions()
{'edgeitems': 3, 'threshold': 1000, 'floatmode': 'maxprec', 'precision': 3, 'suppress': False, 'linewidth': 75, 'nanstr': 'nan', 'infstr': 'inf', 'sign': '-', 'formatter': None, 'legacy': False}
Answered By - Mateen Ulhaq
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.