Is there a way to get a weight into the

pairwise_distances(X, metric='cosine')

Potentially using **kwrds?

from sklearn.metrics import pairwise_distances

In the scipy cosine distance it's possible to add in an array for weights, but that doesn't give a pairwise matrix.

a = np.array([9,8,7,5,2,9])
b = np.array([9,8,7,5,2,2])
w = np.array([1,1,1,1,1,1])


Where w is the weights.


1 Answer 1


Instead of using pairwise_distances you can use the pdist method to compute the distances. This will use the distance.cosine which supports weights for the values.

import numpy as np
from scipy.spatial.distance import pdist, squareform

X = np.array([[5, 4, 3], [4, 2, 1], [5, 6, 2]])
w = [1, 2, 3]

distances = pdist(X, metric='cosine', w=w)

# change the result to a square matrix
distances = squareform(distances)


array([[0.        , 0.05508882, 0.04898252],
       [0.05508882, 0.        , 0.07833123],
       [0.04898252, 0.07833123, 0.        ]])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.