Recommendations for .NET using collaborative filtering
- Supports user-based and item-based recommendations
- Works with explicit and implicit feedback
- Uses high-performance matrix factorization
Run
dotnet add package DiscoRecImport the library
using DiscoRec;Prep your data in the format userId, itemId, value
var data = new Dataset<string, string>();
data.Add("user_a", "item_a", 5.0f);
data.Add("user_a", "item_b", 3.5f);
data.Add("user_b", "item_a", 4.0f);IDs can be integers or strings
data.Add(1, "item_a", 5.0f);If users rate items directly, this is known as explicit feedback. Fit the recommender with:
var recommender = Recommender<string, string>.FitExplicit(data);If users don’t rate items directly (for instance, they’re purchasing items or reading posts), this is known as implicit feedback. Leave out the rating.
var data = new Dataset<string, string>();
data.Add("user_a", "item_a");
data.Add("user_a", "item_b");
data.Add("user_b", "item_a");
var recommender = Recommender<string, string>.FitImplicit(data);Each
userId/itemIdcombination should only appear once
Get user-based recommendations - “users like you also liked”
recommender.UserRecs(userId, 5);Get item-based recommendations - “users who liked this item also liked”
recommender.ItemRecs(itemId, 5);Get the predicted rating for a specific user and item
recommender.Predict(userId, itemId);Get similar users
recommender.SimilarUsers(userId, 5);Load the data
var data = await Data.LoadMovieLens();Create a recommender
var recommender = Recommender<int, string>.FitExplicit(data, new RecommenderOptions { Factors = 20 });Get similar movies
var recs = recommender.ItemRecs("Star Wars (1977)", 5);
foreach (var rec in recs)
Console.WriteLine("{0}: {1}", rec.Id, rec.Score);Save recommendations to your database.
Alternatively, you can store only the factors and use a library like pgvector-dotnet. See an example.
Disco uses high-performance matrix factorization.
- For explicit feedback, it uses stochastic gradient descent
- For implicit feedback, it uses coordinate descent
Specify the number of factors and iterations
var recommender = Recommender<string, string>.FitExplicit(data, new RecommenderOptions { Factors = 8, Iterations = 20 });Pass a validation set
var recommender = Recommender<string, string>.FitExplicit(trainSet, validSet);Collaborative filtering suffers from the cold start problem. It’s unable to make good recommendations without data on a user or item, which is problematic for new users and items.
recommender.UserRecs(newUserId, 5);There are a number of ways to deal with this, but here are some common ones:
- For user-based recommendations, show new users the most popular items
- For item-based recommendations, make content-based recommendations
Get ids
recommender.UserIds();
recommender.ItemIds();Get the global mean
recommender.GlobalMean();Get factors
recommender.UserFactors(userId);
recommender.ItemFactors(itemId);View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/ankane/disco-dotnet.git
cd disco-dotnet
dotnet test