[Test()] public void TestComputeCorrelations2() { // load data from disk var user_mapping = new EntityMapping(); var item_mapping = new EntityMapping(); var ratings = RatingPrediction.Read("../../../../data/ml100k/u1.base", user_mapping, item_mapping); Assert.AreEqual(-0.02855815f, Pearson.ComputeCorrelation(ratings, EntityType.ITEM, 45, 311, 200f), 0.00001); }
public ActionResult Recommend(int id) { Profile activeprofile = _profileService.GetProfileByID(id); // 1. Create the local environment var ctx = new MLContext(); //2. Load the MoviesRecommendation Model ITransformer loadedModel; using (var stream = new FileStream(_movieService.GetModelPath(), FileMode.Open, FileAccess.Read, FileShare.Read)) { loadedModel = ctx.Model.Load(stream); } //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)); } // 3. Create an Rating Prediction Output Class RatingPrediction prediction = null; foreach (var movie in _movieService._trendingMovies) { //4. Call the Rating Prediction for each movie prediction prediction = predictionfunction.Predict(new RatingData { userId = id.ToString(), movieId = movie.MovieID.ToString() }); //5. Normalize the prediction scores for the "ratings" b/w 0 - 100 var normalizedscore = Sigmoid(prediction.Score); //6. Add the score for recommendation of each movie in the trending movie list ratings.Add(Tuple.Create(movie.MovieID, normalizedscore)); } //5. Provide ratings to the view to be displayed ViewData["watchedmovies"] = WatchedMovies; ViewData["ratings"] = ratings; ViewData["trendingmovies"] = _movieService._trendingMovies; return(View(activeprofile)); }
private void CreateRecommender() { BiasedMatrixFactorization recommender = new BiasedMatrixFactorization(); Console.Error.Write("Reading in ratings ... "); TimeSpan time = Utils.MeasureTime(delegate() { recommender.Ratings = RatingPrediction.Read(ratings_file, user_mapping, item_mapping); }); Console.Error.WriteLine("done ({0,0:0.##}).", time.TotalSeconds.ToString(CultureInfo.InvariantCulture)); //Console.Error.Write("Reading in additional ratings ... "); //string[] rating_files = Directory.GetFiles("../../saved_data/", "user-ratings-*"); //Console.Error.WriteLine("done."); foreach (var indices_for_item in recommender.Ratings.ByItem) { if (indices_for_item.Count > 0) { movies_by_frequency.Add(new WeightedItem(recommender.Ratings.Items[indices_for_item[0]], indices_for_item.Count)); } } movies_by_frequency.Sort(); movies_by_frequency.Reverse(); for (int i = 0; i < n_movies; i++) { top_n_movies.Add(movies_by_frequency[i].item_id); } Console.Error.Write("Loading prediction model ... "); recommender.UpdateUsers = true; recommender.UpdateItems = false; recommender.BiasReg = 0.001; recommender.Regularization = 0.045; recommender.NumIter = 60; time = Utils.MeasureTime(delegate() { recommender.LoadModel(model_file); }); Console.Error.WriteLine("done ({0,0:0.##}).", time.TotalSeconds.ToString(CultureInfo.InvariantCulture)); rating_predictor = recommender; current_user_id = user_mapping.ToInternalID(current_user_external_id); //rating_predictor.AddUser(current_user_id); // add movies that were not in the training set //rating_predictor.AddItem( item_mapping.InternalIDs.Count - 1 ); PredictAllRatings(); }
void LoadRatings(string name) { // assumption: ratings, training data and model were reloaded ratings.Clear(); using (var reader = new StreamReader("../../../../saved_data/user-ratings-" + name)) { IRatings user_ratings = RatingPrediction.Read(reader, user_mapping, item_mapping); for (int i = 0; i < user_ratings.Count; i++) { ratings[user_ratings.Items[i]] = user_ratings[i]; current_user_id = user_ratings.Users[i]; // TODO check whether user ID is the same for all ratings } } }
/// <summary>Find the the parameters resulting in the minimal results for a given evaluation measure (1D)</summary> /// <remarks>The recommender will be set to the best parameter value after calling this method.</remarks> /// <param name="evaluation_measure">the name of the evaluation measure</param> /// <param name="hp_name">the name of the hyperparameter to optimize</param> /// <param name="hp_values">the logarithms of the values of the hyperparameter to try out</param> /// <param name="basis">the basis to use for the logarithms</param> /// <param name="recommender">the recommender</param> /// <param name="split">the dataset split to use</param> /// <returns>the best (lowest) average value for the hyperparameter</returns> public static double FindMinimumExponential( string evaluation_measure, string hp_name, double[] hp_values, double basis, RatingPrediction.RatingPredictor recommender, ISplit<IRatings> split) { var new_hp_values = new double[hp_values.Length]; for (int i = 0; i < hp_values.Length; i++) new_hp_values[i] = Math.Pow(basis, hp_values[i]); return FindMinimum(evaluation_measure, hp_name, new_hp_values, recommender, split); }
/// <summary>Find the the parameters resulting in the minimal results for a given evaluation measure using k-fold cross-validation</summary> /// <remarks>The recommender will be set to the best parameter value after calling this method.</remarks> /// <param name="evaluation_measure">the name of the evaluation measure</param> /// <param name="hyperparameter_name">the name of the hyperparameter to optimize</param> /// <param name="hyperparameter_values">the values of the hyperparameter to try out</param> /// <param name="recommender">the recommender</param> /// <param name="k">the number of folds to be used for cross-validation</param> /// <returns>the best (lowest) average value for the hyperparameter</returns> public static double FindMinimum( string evaluation_measure, string hyperparameter_name, double[] hyperparameter_values, RatingPrediction.RatingPredictor recommender, uint k) { var data = recommender.Ratings; var split = new RatingCrossValidationSplit(data, k); double result = FindMinimum(evaluation_measure, hyperparameter_name, hyperparameter_values, recommender, split); recommender.Ratings = data; return result; }
static void Main(string[] args) { var program = new RatingPrediction(); program.Run(args); }