2017-05-18 28 views
3

我有一些灰度图像数据(0-255)。根据NumPy dtype,我得到不同的点积结果。例如,x0x1是相同的图像:NumPy的点积根据数组dtype给出两个不同的结果

>>> x0 
array([0, 0, 0, ..., 0, 0, 0], dtype=uint8) 
>>> x1 
array([0, 0, 0, ..., 0, 0, 0], dtype=uint8) 
>>> (x0 == x1).all() 
True 
>>> np.dot(x0, x1) 
133 
>>> np.dot(x0.astype(np.float64), x1.astype(np.float64)) 
6750341.0 

我知道第二点产品是正确的,因为,因为它们是相同的图像,余弦距离应该是0:

>>> from scipy.spatial import distance 
>>> distance.cosine(x0, x1) 
0.99998029729164795 
>>> distance.cosine(x0.astype(np.float64), x1.astype(np.float64)) 
0.0 

中当然,点积应该用于整数。对于小阵列,它确实:

>>> v = np.array([1,2,3], dtype=np.uint8) 
>>> v 
array([1, 2, 3], dtype=uint8) 
>>> np.dot(v, v) 
14 
>>> np.dot(v.astype(np.float64), v.astype(np.float64)) 
14.0 
>>> distance.cosine(v, v) 
0.0 

发生了什么事。为什么dot产品根据dtype给我不同的答案?

回答

8

数据类型uint8限于8位,因此它只能表示值0,1,...,255.您的点积溢出了可用的值范围,因此只保留最后8位。那些最后8位包含值133.您可以验证此:

6750341 % (2 ** 8) == 133 
# True 
+0

谢谢!如果你有兴趣,我实际上有一个跟这个问题有关的后续问题:https://math.stackexchange.com/questions/2286058 – gwg

相关问题