Hanfei Sun
Hanfei Sun

Reputation: 47051

map a function to all columns for Scipy/numpy matrix

I have a 40*4 matrixM and a vectorA with 40 elements. I want to calculate the cosine distance between A and each column vector in M.

Do I really need to write like this?

 print [cosine(M[:,i],A) for i in range(A.shape[1])]

Or there's another better way to do this?

The document of cosine can be viewed here: http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cosine.html#scipy.spatial.distance.cosine

Thanks!

Upvotes: 3

Views: 2403

Answers (3)

cyon
cyon

Reputation: 9538

Perhaps a more functional way would be to use functools.partial to bind the second argument of cosine to A and then use map to apply this bound function to the columns of M

map(partial(cosine,v=A), M.transpose())

Upvotes: 2

Fred Foo
Fred Foo

Reputation: 363507

It seems scipy.spatial.distance.cosine really only works for vectors. To efficiently compute cosine distances using vectorized expressions, do

normM = np.sqrt((M ** 2).sum(axis=0))
normA = np.sqrt((A ** 2).sum())
cossim = np.dot(M.T, A) / (normM * normA)
dist = 1. - cossim

Assuming M.shape == (40,4), A.shape == (4,), and neither is an np.matrix.

Upvotes: 2

Jānis
Jānis

Reputation: 35

It seems that according to this question, Numpy has a Pythonic way to iterate over the columns of a matrix. This way, you could write:

print [cosine(column,A) for column in M.transpose()]

Upvotes: 2

Related Questions