Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. from sklearn.metrics.pairwise import cosine_similarity
  2. from scipy import sparse
  3.  
  4. a = np.random.random((3, 10))
  5. b = np.random.random((3, 10))
  6.  
  7. # Create sparse matrices, which compute faster and give more understandable output
  8. a_sparse, b_sparse = sparse.csr_matrix(a), sparse.csr_matrix(b)
  9.  
  10. sim_sparse = cosine_similarity(a_sparse, b_sparse, dense_output=False)
  11. print(sim_sparse)
  12.  
  13. (0, 2) 0.7938732813430508
  14. (0, 1) 0.7575978172453429
  15. (0, 0) 0.7897664361147338
  16. (1, 2) 0.740418315571796
  17. (1, 1) 0.833981672896221
  18. (1, 0) 0.7184526671218405
  19. (2, 2) 0.8746293481677073
  20. (2, 1) 0.6456666045233884
  21. (2, 0) 0.7925289217609924
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement