Movie Recommender
概述
MovieRecommender是一个简单的应用程序,它构建和使用推荐模型。
这是一个关于如何使用推荐来增强现有ASP.NET应用程序的终端示例。
本示例从流行的Netflix应用程序中汲取了灵感,并且尽管这个示例主要关注电影推荐,但是可以很容易地应用于任何类型的产品推荐。
特点
- Web应用程序
- 这是一个终端ASP.NET应用程序,它包含了三个用户'Ankit','Cesar','Gal'。然后,它使用ML.NET推荐模型给这三个用户提供建议。
- 推荐模型
- 应用程序使用MovieLens数据集构建推荐模型。模型训练代码使用基于协同过滤的推荐方法。
它如何工作?
训练模型
Movie Recommender 使用基于协同过滤的推荐方法。
协同过滤的基本假设是,如果A(例如Gal)在某个问题上与B(例如Cesar)具有相同的观点,则A(Gal)更有可能在另一个问题上具有和B(Cesar)相同的意见,而不是一个随机的人。
对于此示例,我们使用 http://files.grouplens.org/datasets/movielens/ml-latest-small.zip 数据集。
模型训练代码可以在MovieRecommender_Model中找到。
模型训练遵循以下四个步骤来构建模型。 您可以先跳过代码并继续。
使用模型
通过以下步骤在Controller中使用训练的模型。
1. 创建ML.NET环境并加载已经训练过的模型
// 1. Create the ML.NET environment and load the MoviesRecommendation Model
var ctx = new MLContext();
ITransformer loadedModel;
using (var stream = new FileStream(_movieService.GetModelPath(), FileMode.Open, FileAccess.Read, FileShare.Read))
{
loadedModel = ctx.Model.Load(stream);
}
2. 创建预测函数以预测一组电影推荐
//3. Create a prediction function
var predictionfunction = loadedModel.MakePredictionFunction<RatingData, RatingPrediction>(ctx);
List<Tuple<int, float>> ratings = new List<Tuple<int, float>>();
List<Tuple<int, int>> MovieRatings = _profileService.GetProfileWatchedMovies(id);
List<Movie> WatchedMovies = new List<Movie>();
foreach (Tuple<int, int> tuple in MovieRatings)
{
WatchedMovies.Add(_movieService.Get(tuple.Item1));
}
RatingPrediction prediction = null;
foreach (var movie in _movieService._trendingMovies)
{
// Call the Rating Prediction for each movie prediction
prediction = predictionfunction.Predict(new RatingData { userId = id.ToString(), movieId = movie.MovieID.ToString()});
// Normalize the prediction scores for the "ratings" b/w 0 - 100
var normalizedscore = Sigmoid(prediction.Score);
// Add the score for recommendation of each movie in the trending movie list
ratings.Add(Tuple.Create(movie.MovieID, normalizedscore));
}
3. 为要显示的视图提供评分预测
ViewData["watchedmovies"] = WatchedMovies;
ViewData["ratings"] = ratings;
ViewData["trendingmovies"] = _movieService._trendingMovies;
return View(activeprofile);
替代方法
这个示例显示了许多可以用于ML.NET的推荐方法之一。根据您的特定场景,您可以选择以下任何最适合您的用例的方法。