Vladislav Ladenkov Vladislav Ladenkov - 11 months ago 54
Python Question

Fastest way to calculate "cosine" metrcis with scipy

Iam given matrix of ones and zeros. I need to find 20 rows which have the highest cosine metrics towards 1

row in matrix:
if I have 10 rows, and 5th is called
i want to choose the highest value between these:

First, i tried to count metrics.
This didnt work:

A = ratings[:,100]
A = A.reshape(1,A.shape[0])
B = ratings.transpose()
similarity = -cosine(A,B)+1
A.shape = (1L, 71869L)
B.shape = (10000L, 71869L)

Error is:
Input vector should be 1-D.
I'd like to know, how to implement this with estetical beuty and no errors, but the most important - which solution will be the fastest?
In my opinion, the fastest way is not realized with help of
We just have to take all ones in
row and look at these indexes in all other rows. Those rows, which have the highest coincidence will have the highest matrix. Are there any faster ways?

Answer Source

The fastest way is to use matrix operations: something like np.multipy(A,B)