Disco Rust
:fire: Recommendations for Rust using collaborative filtering
- Supports user-based and item-based recommendations
- Works with explicit and implicit feedback
- Uses high-performance matrix factorization
Installation
Add this line to your application’s Cargo.toml under [dependencies]:
= "0.1"
Getting Started
Prep your data in the format user_id, item_id, value
use ;
let mut data = new;
data.push;
data.push;
data.push;
IDs can be integers, strings, or any other hashable data type
data.push;
If users rate items directly, this is known as explicit feedback. Fit the recommender with:
let recommender = fit_explicit;
If users don’t rate items directly (for instance, they’re purchasing items or reading posts), this is known as implicit feedback. Use 1.0 or a value like number of purchases or page views for the dataset, and fit the recommender with:
let recommender = fit_implicit;
Get user-based recommendations - “users like you also liked”
recommender.user_recs;
Get item-based recommendations - “users who liked this item also liked”
recommender.item_recs;
Get predicted ratings for a specific user and item
recommender.predict;
Get similar users
recommender.similar_users;
Example
Download the MovieLens 100K dataset.
Add these lines to your application’s Cargo.toml under [dependencies]:
= "1"
= { = "1", = ["derive"] }
And use:
use ReaderBuilder;
use ;
use Deserialize;
use File;
Storing Recommendations
Save recommendations to your database. Alternatively, you can store only the factors and use a library like pgvector-rust.
Algorithms
Disco uses high-performance matrix factorization.
- For explicit feedback, it uses the stochastic gradient method with twin learners
- For implicit feedback, it uses the conjugate gradient method
Specify the number of factors and iterations
new
.factors
.iterations
.fit_explicit;
Progress
Pass a callback to show progress
new
.callback
.fit_explicit;
Note: train_loss and valid_loss are not available for implicit feedback
Validation
Pass a validation set with explicit feedback
new
.callback
.fit_eval_explicit;
The loss function is RMSE
Reference
Get ids
recommender.user_ids;
recommender.item_ids;
Get the global mean
recommender.global_mean;
Get factors
recommender.user_factors;
recommender.item_factors;
References
- A Learning-rate Schedule for Stochastic Gradient Methods to Matrix Factorization
- Faster Implicit Matrix Factorization
History
View the changelog
Contributing
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: