Skip to content
Branch: master
Find file History
bamurtaugh and CESARDELATORRE Various anomaly detection, readme, and display cleanups (#595)
* Added credit card anomaly detection to readme

* Delineate between movie recommend apps

* Updated to binary from two-class

* Update from two-class to binary

* Update to binary

* Update to binary

* Clean up readme

Make title shorter/more consistent with other readme titles, change from two-class to binary

* Cleanup readme

Shorten title, change from two-class to binary

* Remove unnecessary API paragraph

* Remove API paragraph

* Correct scenario vs task vs algorithm

* Correct scenario vs task vs algorithm

* Update names in readme

Match them to names of people in the app

* Link to AnomalyDetection_CreditCardFraudDetection

* Update title

Delineate this sample from AnomalyDetection_CreditCardFraudDetection

* Cleanup readme

Link to binary classification fraud example, shorten title

* Changed anomaly detection label

Should be area under ROC curve, not just area under curve

* Update code in readme

Match changes made to actual code in sample

* Improve code formatting

* Update testing title

* Update algorithm mentioned in readme
Latest commit 3b33be4 Aug 10, 2019

Movie Recommender

ML.NET version API type Status App Type Data sources Scenario ML Task Algorithms
v1.3.1 Dynamic API up-to-date End-End app .csv Movie Recommendation Recommendation Field Aware Factorization Machines

Alt Text


MovieRecommender is a simple application which both builds and consumes a recommendation model.

This is an end-end sample on how you can enhance your existing ASP.NET apps with recommendations.

The sample takes insipiration from the popular Netflix application and even though this sample focuses on movie recommendations, learnings can be easily applied to any style of product recommendations.


  • Wep app

    • This is an end-end ASP.NET app which presents three user profiles 'Ankit', 'Cesar', 'Amy'. It then provides these three users recommendations using a ML.NET recommendation model.
  • Recommendation Model

    • The application builds a recommendation model using the MovieLens dataset. The model training code shows uses colloborative filtering based recommendation approach.

How does it work?

Model Training

Movie Recommender uses Colloborative Filtering for recommendations.

The underlying assumption with Colloborative filtering is that if a person A (e.g. Amy) has the same opinion as a person B (e.g. Cesar) on an issue, A (Amy) is more likely to have B’s (Cesar) opinion on a different issue than that of a random person.

For this sample we make use of the dataset.

The model training code can be found in the MovieRecommender_Model.

Model training follows the following four steps for building the model. You can traverse the code and follow along.

Build -> Train -> Evaluate -> Consume

Model Consumption

The trained model is consumed in the Controller using the following steps.

1. Create the ML.NET environment and load the already trained model

   // 1. Create the ML.NET environment and load the already trained model
   MLContext mlContext = new MLContext();
   ITransformer trainedModel;
   using (var stream = new FileStream(_movieService.GetModelPath(), FileMode.Open, FileAccess.Read, FileShare.Read))
    trainedModel = mlContext.Model.Load(stream);

2. Create a prediction function to predict a set of movie recommendations

   //2. Create a prediction function
   var predictionEngine = mlContext.Model.CreatePredictionEngine<MovieRating, MovieRatingPrediction>(trainedModel);
   List<(int movieId, float normalizedScore)> ratings = new List<(int movieId, float normalizedScore)>();
   var MovieRatings = _profileService.GetProfileWatchedMovies(id);
   List<Movie> WatchedMovies = new List<Movie>();

   foreach ((int movieId, int movieRating) in MovieRatings)
   MovieRatingPrediction prediction = null;
   foreach (var movie in _movieService.GetTrendingMovies)
       //Call the Rating Prediction for each movie prediction
        prediction = predictionEngine.Predict(new MovieRating
            userId = id.ToString(),
            movieId = movie.MovieID.ToString()
       //Normalize the prediction scores for the "ratings" b/w 0 - 100
       float normalizedscore = Sigmoid(prediction.Score);
       //Add the score for recommendation of each movie in the trending movie list
        ratings.Add((movie.MovieID, normalizedscore));

3. Provide rating predictions to the view to be displayed

  ViewData["watchedmovies"] = WatchedMovies;
  ViewData["ratings"] = ratings;
  ViewData["trendingmovies"] = _movieService.GetTrendingMovies;
  return View(activeprofile);

Alternate Approaches

This sample shows one of many recommendation approaches that can be used with ML.NET. Depending upon your specific scenario you can choose any of the following approaches which best fit your usecase.

Scenario Algorithm Link To Sample
You want to use attributes (features) like UserId, ProductId, Ratings, Product Description, Product Price etc. for your recommendation engine. In such a scenario Field Aware Factorization Machine is a generalized approach you can use to build your recommendation engine Field Aware Factorization Machines This sample
You have UserId, ProductId and Ratings available to you for what users bought and rated. For this scenario you should use the Matrix Factorization approach Matrix Factorization Matrix Factorization - Recommendation
You only have UserId and ProductId's the user bought available to you but not ratings. This is common in datasets from online stores where you might only have access to purchase history for your customers. With this style of recommendation you can build a recommendation engine which recommends frequently bought items. One Class Matrix Factorization Product Recommender
You can’t perform that action at this time.