Ejemplo n.º 1
0
        /// <summary>Recommend top N items, based on a user description by ratings</summary>
        /// <returns>a list of item IDs with scores</returns>
        /// <param name='recommender'>the IFoldInRatingPredictor recommender</param>
        /// <param name='rated_items'>a list of item IDs and ratings describing the user</param>
        /// <param name='n'>the number of items to recommend</param>
        public static IList <Tuple <int, float> > RecommendItems(this IFoldInRatingPredictor recommender, IList <Tuple <int, float> > rated_items, int n)
        {
            var candidate_items = Enumerable.Range(0, ((RatingPredictor)recommender).Ratings.MaxItemID - 1).ToArray();
            var scored_items    = recommender.ScoreItems(rated_items, candidate_items);

            return(scored_items.OrderByDescending(x => x.Item2).Take(n).ToArray());
        }
        /// <summary>Performs user-wise fold-in evaluation</summary>
        /// <returns>the evaluation results</returns>
        /// <param name='recommender'>a rating predictor capable of performing a user fold-in</param>
        /// <param name='update_data'>the rating data used to represent the users</param>
        /// <param name='eval_data'>the evaluation data</param>
        static public RatingPredictionEvaluationResults EvaluateFoldIn(this IFoldInRatingPredictor recommender, IRatings update_data, IRatings eval_data)
        {
            double rmse = 0;
            double mae  = 0;
            double cbd  = 0;

            int rating_count = 0;

            foreach (int user_id in update_data.AllUsers)
            {
                if (eval_data.AllUsers.Contains(user_id))
                {
                    var known_ratings = (
                        from index in update_data.ByUser[user_id]
                        select Tuple.Create(update_data.Items[index], update_data[index])
                        ).ToArray();
                    var items_to_rate     = (from index in eval_data.ByUser[user_id] select eval_data.Items[index]).ToArray();
                    var predicted_ratings = recommender.ScoreItems(known_ratings, items_to_rate);

                    foreach (var pred in predicted_ratings)
                    {
                        float prediction    = pred.Item2;
                        float actual_rating = eval_data.Get(user_id, pred.Item1, eval_data.ByUser[user_id]);
                        float error         = prediction - actual_rating;

                        rmse += error * error;
                        mae  += Math.Abs(error);
                        cbd  += Eval.Ratings.ComputeCBD(actual_rating, prediction, recommender.MinRating, recommender.MaxRating);
                        rating_count++;
                    }
                    Console.Error.Write(".");
                }
            }

            mae  = mae / rating_count;
            rmse = Math.Sqrt(rmse / rating_count);
            cbd  = cbd / rating_count;

            var result = new RatingPredictionEvaluationResults();

            result["RMSE"] = (float)rmse;
            result["MAE"]  = (float)mae;
            result["NMAE"] = (float)mae / (recommender.MaxRating - recommender.MinRating);
            result["CBD"]  = (float)cbd;
            return(result);
        }
        public void TestTopNWithoutCandidates()
        {
            var rated_items = new List <Tuple <int, float> >();

            rated_items.Add(Tuple.Create(1, 1.0f));
            rated_items.Add(Tuple.Create(2, 4.0f));
            rated_items.Add(Tuple.Create(3, 4.5f));

            IFoldInRatingPredictor recommender = CreateRecommender();

            var result = recommender.RecommendItems(rated_items, 3);

            Assert.GreaterOrEqual(result[0].Item2, result[1].Item2);
            Assert.GreaterOrEqual(result[0].Item2, result[2].Item2);
            Assert.GreaterOrEqual(result[1].Item2, result[2].Item2);
            Assert.AreEqual(3, result.Count);
        }
Ejemplo n.º 4
0
        /// <summary>Recommend top N items, based on a user description by ratings</summary>
        /// <returns>a list of item IDs with scores</returns>
        /// <param name='recommender'>the IFoldInRatingPredictor recommender</param>
        /// <param name='rated_items'>a list of item IDs and ratings describing the user</param>
        public static IList <Tuple <int, float> > ScoreItems(this IFoldInRatingPredictor recommender, IList <Tuple <int, float> > rated_items)
        {
            var candidate_items = Enumerable.Range(0, ((RatingPredictor)recommender).Ratings.MaxItemID - 1).ToArray();

            return(recommender.ScoreItems(rated_items, candidate_items));
        }
Ejemplo n.º 5
0
        /// <summary>Recommend top N items, based on a user description by ratings</summary>
        /// <returns>a list of item IDs with scores</returns>
        /// <param name='recommender'>the IFoldInRatingPredictor recommender</param>
        /// <param name='rated_items'>a list of item IDs and ratings describing the user</param>
        /// <param name='candidate_items'>the recommendation candidates</param>
        /// <param name='n'>the number of items to recommend</param>
        public static IList <Tuple <int, float> > RecommendItems(this IFoldInRatingPredictor recommender, IList <Tuple <int, float> > rated_items, IList <int> candidate_items, int n)
        {
            var scored_items = recommender.ScoreItems(rated_items, candidate_items);

            return(scored_items.OrderByDescending(x => x.Item2).Take(n).ToArray());
        }