aat2703 - 3 months ago 44

Python Question

I'm currently working with a Python library called LightFM. But i'm having some trouble with passing the interactions to the fit() method.

Python version: 3

Library: http://lyst.github.io/lightfm/docs/lightfm.html

The documentation states that i should make an sparse matrix of the following type: **interactions (np.float32 coo_matrix of shape [n_users, n_items]) – the matrix**

But i can't seem to make it work it always recommends the same...

**Updated**: When executed it the top_items variable say the following no matter which user it iterates over and not any of the other items (Beef or salad), so it seems like i'm doing something wrong. It outputs: ['Cake' 'Cheese'] everytime

Here is my code:

`import numpy as np`

from lightfm.datasets import fetch_movielens

from lightfm import LightFM

from scipy.sparse import coo_matrix

import scipy.sparse as sparse

import scipy

// Users, items

data = [

[1, 0],

[2, 1],

[3, 2],

[4, 3]

]

items = np.array(["Cake", "Cheese", "Beef", "Salad"])

data = coo_matrix(data)

#create model

model = LightFM(loss='warp')

#train model

model.fit(data, epochs=30, num_threads=2)

// Print training data

print(data)

def sample_recommendation(model, data, user_ids):

#number of users and movies in training data

n_users, n_items = data.shape

#generate recommendations for each user we input

for user_id in user_ids:

#movies our model predicts they will like

scores = model.predict(user_id, np.arange(n_items))

#rank them in order of most liked to least

top_items = items[np.argsort(-scores)]

print(top_items)

sample_recommendation(model, data, [1,2])

Answer

```
data = coo_matrix(data)
```

probably isn't what you want; it's an exact replica of `data`

. Not particularly sparse.

What does `data`

represent?

I'm going to guess that you really want a matrix with mostly 0s, and 1s at the coordinates represented by `data`

.

```
In [20]: data = [
...: [1, 0],
...: [2, 1],
...: [3, 2],
...: [4, 3]
...: ]
```

probably not what you want:

```
In [21]: ds = sparse.coo_matrix(data)
In [22]: ds.A
Out[22]:
array([[1, 0],
[2, 1],
[3, 2],
[4, 3]])
```

try again:

```
In [23]: data=np.array(data)
In [24]: ds=sparse.coo_matrix((np.ones(4,int),(data[:,0],data[:,1])))
In [25]: ds
Out[25]:
<5x4 sparse matrix of type '<class 'numpy.int32'>'
with 4 stored elements in COOrdinate format>
In [26]: ds.A
Out[26]:
array([[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
```

That's more typical of what goes into learning functions.