# pairwise_distances with Cosine and weighting

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])

distance.cosine(a,b,w)


Where w is the weights.

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)


Result:

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