python - Converting nested loop for faster computation of adjusted cosine similarity for sparse matrices -
i have following function calculating adjusted cosine similarity. takes in 2 numpy matrices. mean centered ratings matrix of size 22000 users x 503 items
, similarity matrix empty matrix of size 22000 x 22000
since user user similarity. takes hours calculate similarity. how can make run faster?
def adjusted_cosine_similarity(ratings_centered_matrix, similarity_matrix): x in range(0, ratings_centered_matrix.shape[0]): # index of array y in range(0, ratings_centered_matrix.shape[0]): s = np.sum(ratings_centered_matrix[x, :] * ratings_centered_matrix[y, :] si = np.sqrt(np.sum(ratings_centered_matrix[x, :] ** 2)) sj = np.sqrt(np.sum(ratings_centered_matrix[y, :] ** 2)) sim = s / (si * sj) similarity_matrix[x, y] = round(sim, 2) similarity_matrix[x, y] = sim
Comments
Post a Comment