Reputation: 357
Im trying to calculate my own distance with numpy array by adding a weight to each sum in euclidean distance, for example:
a = ((1, 2, 3))
b = ((4, 5, 6))
distance = np.sum((a-b)**2)
but what I want is set my distance as:
a = ((1, 2, 3))
b = ((4, 5, 6))
w = ((0.2, 0,3, 0,5))
distance = 0.2*((1-4)**2) + 0.3*((2-5)**2) + 0.5*((3-6)**2)
is it any form of do this with numpy without iterate over echa vector and do this manually?
Upvotes: 1
Views: 13595
Reputation: 44906
You're halfway there:
a = np.array([[1., 2, 3]])
b = np.array([[4., 5, 6]])
w = np.array([[0.2, 0.3, 0.5]])
result = float(np.dot((a - b)**2, w.T))
So, you simply multiply a row-vector (a - b)**2
by a column-vector w.T
to get the number you want.
Please note that you'll have to make sure the arrays' dimensions match.
Upvotes: 7