Using your Own Data#

In this example, we will use our own data to train a model. We will also demonstrate on how we could get the predictions given your own data.

My First Data Prediction#

First, we will load the data. In this example, we will use our own data consisting of 3 columns: user, item, rating. The user and item columns are the IDs of the users and items respectively. The rating column is the rating given by the user to the item.

User

Item

Rating

1

1

5

1

2

1

2

2

3

2

3

3

3

4

3

3

5

5

4

1

5

1. Defining data structure#

The data structure is a list of tuples. Each tuple represents a row in the data. The first element of the tuple is the user ID, the second element is the item ID, and the third element is the rating.

First, create a file called my_data.py and add the following code:

import cornac

# Define the data as a list of UIR (user, item, rating) tuples
data = [
    (1, 1, 5),
    (1, 2, 1),
    (2, 2, 3),
    (2, 3, 3),
    (3, 4, 3),
    (3, 5, 5),
    (4, 1, 5)
]

2. Creating Dataset object#

Next, we will create a dataset object from the data. The dataset object will be used to train the model.

from cornac.data import Dataset

# Load the data into a dataset object
dataset = cornac.data.Dataset.from_uir(data)

3. Create and train model#

We will then create a model object and train it using the dataset object.

from cornac.models import PMF

# Instantiate the PMF model
model_pmf = PMF(k=10, max_iter=100, learning_rate=0.001, lambda_reg=0.001, seed=123)

# Use the fit() function to train the model
model_pmf.fit(dataset)

4. Getting recommendations#

Finally, we will use the model to get item recommendations for the users. The recommend() function will recommend the top rated items from the model for the user provided.

For example, we will get all recommendations for user 4.

# Get recommendations for user id 4
user_id = 4
recommended_items = model_pmf.recommend(user_id)

# Print the recommended items
print(recommended_items)
output#
[1, 4, 3, 5, 2]

The output is a list of item IDs. The first item in the list is the most recommended item for the user, followed by the second item, and so on.

View codes at this point
my_data.py#
 1import cornac
 2from cornac.models import PMF
 3from cornac.data import Dataset
 4
 5# Define the data as a list of UIR (user, item, rating) tuples
 6data = [
 7    (1, 1, 5),
 8    (1, 2, 1),
 9    (2, 2, 3),
10    (2, 3, 3),
11    (3, 4, 3),
12    (3, 5, 5),
13    (4, 1, 5)
14]
15
16# Load the data into a dataset object
17dataset = Dataset.from_uir(data)
18
19# Instantiate the PMF model
20model_pmf = PMF(k=10, max_iter=100, learning_rate=0.001, lambda_reg=0.001, seed=123)
21
22# Use the fit() function to train the model
23model_pmf.fit(dataset)
24
25# Get recommendations for user id 10
26user_id = 4
27recommended_items = model_pmf.recommend(user_id)
28
29# Print the recommended items
30print(recommended_items)

Loading data from CSV#

In this example, we will load the data from a CSV file. The CSV file consists of 3 columns: user, item, rating. The user and item columns are the IDs of the users and items respectively. The rating column is the rating given by the user to the item.

User

Item

Rating

1

1

5

1

2

1

2

2

3

2

3

3

3

4

3

3

5

5

4

1

5

1. Loading the data#

First, create a file called data.csv and add the following code:

data.csv#
1,1,5
1,2,1
2,2,3
2,3,3
3,4,3
3,5,5
4,1,5

In this file, the data is separated by commas. The first column is the user ID, the second column is the item ID, and the third column is the rating.

Next, we have to load the data from the CSV file. We will use Reader provided by Cornac to read our CSV file.

from cornac.data import Reader

data = Reader().read('data.csv', sep=',')
print(data)
  1. Creating the Dataset Object

Next, we will create a dataset object from the data. The dataset object will be used to train the model.

from cornac.data import Dataset

# Load the data into a dataset object
dataset = Dataset.from_uir(data, sep=',', skip_lines=1)
  1. Create and train model

We will then create a model object and train it using the dataset object.

from cornac.models import PMF

# Instantiate the PMF model
model_pmf = PMF(k=10, max_iter=100, learning_rate=0.001, lambda_reg=0.001, seed=123)

# Use the fit() function to train the model
model_pmf.fit(dataset)
  1. Getting recommendations

Finally, we will use the model to get item recommendations for the users. The recommend() function will recommend the top rated items from the model for the user provided.

For example, we will get all recommendations for user 4.

# Get recommendations for user id 4
user_id = 4
recommended_items = model_pmf.recommend(user_id)

# Print the recommended items
print(recommended_items)
output#
[1, 4, 3, 5, 2]

The output is a list of item IDs. The first item in the list is the most recommended item for the user, followed by the second item, and so on.

View codes at this point
data_csv.py#
 1import csv
 2import cornac
 3from cornac.models import PMF
 4from cornac.data import Dataset
 5
 6# Load the data from the CSV file
 7with open('data.csv', 'r') as f:
 8    reader = csv.reader(f)
 9    data = list(reader)
10    print(data)
11
12# Load the data into a dataset object
13dataset = Dataset.from_uir(data, sep=',', skip_lines=1)
14
15# Instantiate the PMF model
16model_pmf = PMF(k=10, max_iter=100, learning_rate=0.001, lambda_reg=0.001, seed=123)
17
18# Use the fit() function to train the model
19model_pmf.fit(dataset)
20
21# Get recommendations for user id 4
22user_id = 4
23recommended_items = model_pmf.recommend(user_id)
24
25# Print the recommended items
26print(recommended_items)