Reputation: 1307
I am using sklearn.metrics.pairwise.paired_distances
to calculate distances between a single vector and a matrix. I want to calculate the distance between every row of the matrix and the single vector. Since sklearn.metrics.pairwise.paired_distances
requires the two arrays to have equal dimensions, I use np.tile
to create a matrix which contains multiple copies of the vector to create a matrix that has the same size as the first one.
Example:
import numpy as np
from sklearn.metrics.pairwise import paired_distances
# get matrix a and vector b
a = np.array([[1,2],[3,4]])
b = np.array([[5],[6]]).transpose()
# create a matrix with copies of b that has the same size as matrix a
b = np.tile(b,(a.shape[0],1))
distances = paired_distances(a,b)
Just out of curiosity: Is there a function that does that out of the box? Time is not critical here, since I don't deal with very big arrays. But the function should offer different kinds of metrics.
Upvotes: 0
Views: 467
Reputation: 7063
You could use numpy.apply_along_axis
method to apply a given function to each rows.
Upvotes: 1