3
그레이 스케일 이미지 데이터 (0-255)가 있습니다. NumPy dtype에 따라 다른 내적 결과가 나타납니다. 그들은 같은 이미지 때문에, 코사인 거리가 0이어야하기 때문에이 두 번째 내적가 정확한지 알고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
:의
>>> from scipy.spatial import distance
>>> distance.cosine(x0, x1)
0.99998029729164795
>>> distance.cosine(x0.astype(np.float64), x1.astype(np.float64))
0.0
예를 들어, x0
및 x1
는 같은 이미지입니다 물론, 내적 제품은 정수를 위해 작동해야합니다. 작은 배열의 경우 :
>>> 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
무슨 일이 일어나고있는 중입니다. 내 제품이 dtype에 따라 다른 대답을주는 이유는 무엇입니까?
고맙습니다! 관심이 있으시면 실제로 관련된 후속 질문이 있습니다. https://math.stackexchange.com/questions/2286058 – gwg