Fork me on GitHub

MyMediaLite: How to use a recommender in C#

News

MyMediaLite 3.11 has been released.


Complex examples

The two command line programs shipped with MyMediaLite also demonstrate how to use the recommenders. You may have a look at their source code to see how they use the MyMediaLite recommenders:

Simpler examples

In the following, we show how to first set up recommenders in C#, and then use them to make predictions.

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

Rating Prediction

using System;
using MyMediaLite.Data;
using MyMediaLite.Eval;
using MyMediaLite.IO;
using MyMediaLite.RatingPrediction;

public class RatingPrediction
{
	public static void Main(string[] args)
	{
		// load the data
		var training_data = RatingData.Read(args[0]);
		var test_data = RatingData.Read(args[1]);

		// set up the recommender
		var recommender = new UserItemBaseline();
		recommender.Ratings = training_data;
		recommender.Train();

		// measure the accuracy on the test data set
		var results = recommender.Evaluate(test_data);
		Console.WriteLine("RMSE={0} MAE={1}", results["RMSE"], results["MAE"]);
		Console.WriteLine(results);

		// make a prediction for a certain user and item
		Console.WriteLine(recommender.Predict(1, 1));
		
		var bmf = new BiasedMatrixFactorization {Ratings = training_data};
		Console.WriteLine(bmf.DoCrossValidation());
	}
}

Item Prediction from Positive-Only Feedback

using System;
using MyMediaLite.Data;
using MyMediaLite.Eval;
using MyMediaLite.IO;
using MyMediaLite.ItemRecommendation;

public class ItemPrediction
{
	public static void Main(string[] args)
	{
		// load the data
		var training_data = ItemData.Read(args[0]);
		var test_data = ItemData.Read(args[1]);

		// set up the recommender
		var recommender = new MostPopular();
		recommender.Feedback = training_data;
		recommender.Train();

		// measure the accuracy on the test data set
		var results = recommender.Evaluate(test_data, training_data);
		foreach (var key in results.Keys)
			Console.WriteLine("{0}={1}", key, results[key]);
		Console.WriteLine(results);

		// make a score prediction for a certain user and item
		Console.WriteLine(recommender.Predict(1, 1));
	}
}

ContactFollow us on Twitter