What better way to parallel this function?

0

I'm trying to parallelize a function that calculates the cosine similarity:

Here is my code:

import numpy as np
def cos_sim(a,b):
    dot_product = np.dot(a,b)
    norm_a = np.linalg.norm(a)
    norm_b = np.linalg.norm(b)
    if(norm_a == 0 or norm_b == 0):
        return 0
    else:
        return dot_product / (norm_a * norm_b)

def newsimilarityitem(matriz):
    cs = []
    for i in range(0, len(matriz)):
        cs.append([0]*len(matriz))

    for i in range(0,len(matriz)-1): #AQUI
        for l in range(i+1,len(matriz)): #AQUI
            a = np.array(matriz[i])
            b = np.array(matriz[l])
            r = cos_sim(a,b)
            cs[i][l] = r
            cs[l][i] = r
    return cs

What the code does:

matriz = [[4,3,0,0,5,0],
          [5,0,4,0,4,0],
          [4,0,5,3,4,0],
          [0,3,0,0,0,5],
          [0,4,0,0,0,4],
          [0,0,2,4,0,5]]

Given a matrix (not necessarily quadratic) where the rows are represented by items and the columns are represented by users and the cells are notes, I will calculate the cosine similarity between the

asked by anonymous 20.02.2018 / 19:15

0 answers