Fork me on GitHub

MyMediaLite: How to use a recommender in Python

News

MyMediaLite 3.10 has been released.


On this page, we show how to first set up recommenders in Python, and then use them to make predictions.

To use the examples on this page, download the MovieLens 100k ratings dataset from the GroupLens Research website and unzip it.
Of course you can also use your own data ;-)

IronPython

IronPython lets you run Python programs on the .NET platform. It also lets you use .NET libraries (.dlls) like MyMediaLite.

To run a program with IronPython, type ipy program.py in the command line, where program.py is your program.

You may also enter just ipy, so that you can use IronPython's REPL interactively.

Our examples do not work with IronPython 2.6. Use IronPython 3.0 instead.

Rating Prediction

#!/usr/bin/env ipy

import clr
clr.AddReference("MyMediaLite.dll")
from MyMediaLite import *

# load the data
train_data = IO.RatingData.Read("u1.base")
test_data  = IO.RatingData.Read("u1.test")

# set up the recommender
recommender = RatingPrediction.UserItemBaseline() # don't forget ()
recommender.Ratings = train_data
recommender.Train()

# measure the accuracy on the test data set
print Eval.Ratings.Evaluate(recommender, test_data)

# make a prediction for a certain user and item
print recommender.Predict(1, 1)

Item Prediction from Positive-Only Feedback

#!/usr/bin/env ipy

import clr
clr.AddReference("MyMediaLite.dll")
from MyMediaLite import *

# load the data
train_data = IO.ItemData.Read("u1.base")
test_data = IO.ItemData.Read("u1.test")

# set up the recommender
recommender = ItemRecommendation.UserKNN() # don't forget ()
recommender.K = 20
recommender.Feedback = train_data
recommender.Train()

# measure the accuracy on the test data set
print Eval.Items.Evaluate(recommender, test_data, train_data)

# make a prediction for a certain user and item
print recommender.Predict(1, 1)

ContactFollow us on Twitter