dimanche 1 avril 2018

If-statement with list inside for-loop over same list

I am appending similarity scores of all pairs in a list.

data = []

for i1, i2 in list: 
    data.append([i1, i2, cosine_similarity([X[df.index.get_loc(i1)]],[X[df.index.get_loc(i2)]]).ravel()[0]])

However, I need it to only append scores that are non-zero.

I put in an if statement, but it produces an error since it is not of int type.

for i1, i2 in list:
    if [cosine_similarity([X[df.index.get_loc(i1)]], [X[df.index.get_loc(i2)]])] > 0:
        data.append([i1, i2, cosine_similarity([X[df.index.get_loc(i1)]], [X[df.index.get_loc(i2)]]).ravel()[0]])

Any way of only appending only none-zeros as part of the iteration?

Aucun commentaire:

Enregistrer un commentaire