3
我有一些灰度圖像數據(0-255)。根據NumPy dtype,我得到不同的點積結果。例如,x0
和x1
是相同的圖像: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給我不同的答案?
謝謝!如果你有興趣,我實際上有一個跟這個問題有關的後續問題:https://math.stackexchange.com/questions/2286058 – gwg