public static void UseModelForSinglePrediction(MLContext mlContext, ITransformer model) { Console.WriteLine("=============== Making a prediction ==============="); var predictionEngine = mlContext.Model.CreatePredictionEngine <CourseRatingMl, RatingPrediction>(model); var testInput = new CourseRatingMl() { UserId = "74c5626f-d9fd-3e60-58f0-de76324067cc", CourseId = 10 }; var courseRatingPrediction = predictionEngine.Predict(testInput); if (Math.Round(courseRatingPrediction.Score, 1) > 3.5) { Console.WriteLine("Course " + testInput.CourseId + " is recommended for user " + testInput.UserId); } else { Console.WriteLine("Course " + testInput.CourseId + " is not recommended for user " + testInput.UserId); } }
public static void PredictAll(MLContext mlContext, ITransformer model) { Console.WriteLine("=============== Making a prediction ==============="); var predictionEngine = mlContext.Model.CreatePredictionEngine <CourseRatingMl, RatingPrediction>(model); var pedro = "34b357c6-4f3b-4eb6-af4e-e2a5367ad58e"; var som = "74c5626f-d9fd-3e60-58f0-de76324067cc"; List <CourseWithRatingPrediction> tmp = new List <CourseWithRatingPrediction>(); for (int i = 0; i < 52; i++) { var testInput = new CourseRatingMl() { UserId = pedro, CourseId = i }; var courseRatingPrediction = predictionEngine.Predict(testInput); var label = Math.Round(courseRatingPrediction.Rating, 1); var score = Math.Round(courseRatingPrediction.Score, 1); tmp.Add(new CourseWithRatingPrediction { CourseId = i, RatingPrediction = new RatingPrediction { Rating = (float)label, Score = (float)score } }); } tmp = tmp.OrderByDescending(c => c.RatingPrediction.Score).ToList(); foreach (var courseWithRatingPrediction in tmp) { Console.WriteLine( $"{courseWithRatingPrediction.CourseId} -- {courseWithRatingPrediction.RatingPrediction.Rating} --- {courseWithRatingPrediction.RatingPrediction.Score}"); } Console.WriteLine($"Done"); }