HalfPintBoy - 2 months ago 30x

Python Question

I'm trying to calculate cosine distance in python between the rows in matrix and have couple a questions.So I'm creating matrix matr and populating it from the lists, then reshaping it for analysis purposes:

`s = []`

for i in range(len(a)):

for 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 I want to use scipy.spatial.distance.cosine package to calculate cosine from first row to every other else in the d matrix.

How can I perform that? Should it be some for loop for that? Not too much experience with matrix and array operations.

So how can I use for loop for second argument (d[1],d[2], and so on) in that construction not to launch it every time:

`from scipy.spatial.distance import cosine`

x=cosine (d[0], d[6])

Answer

You can just use a simple for loop with `scipy.spatial.distance.cosine`

:

```
dists = []
for row in matr:
dists.append(scipy.spatial.distance.cosine(matr[0,:], row))
```

Source (Stackoverflow)

Comments