python - Calculating cosine distance between the rows of matrix -
i'm trying calculate cosine distance in python between rows in matrix , have couple questions.so i'm creating matrix matr , populating lists, reshaping analysis purposes:
s = [] in range(len(a)): j in range(len(b_list)): s.append(a[i].count(b_list[j])) matr = np.array(s) d = matr.reshape((22, 254))
the output of d gives me smth like:
array([[0, 0, 0, ..., 0, 0, 0], [2, 0, 0, ..., 1, 0, 0], [2, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [1, 0, 0, ..., 0, 0, 0]])
then want use scipy.spatial.distance.cosine package calculate cosine first row every other else in d matrix. how can perform that? should loop that? not experience matrix , array operations.
so how can use loop second argument (d[1],d[2], , on) in construction not launch every time:
from scipy.spatial.distance import cosine x=cosine (d[0], d[6])
you can use simple loop scipy.spatial.distance.cosine
:
dists = [] row in matr: dists.append(scipy.spatial.distance.cosine(matr[0,:], row))
Comments
Post a Comment