Beispiel #1
0
        public static double MeanAverageError(Matrix <int> ratingsMatrix, Matrix <float> weightsMatrix, RatingPredictor ratingPredictor)
        {
            int    ratings     = 0;
            double totalErrors = 0;
            double mae         = 0;

            int predictedRatings = 0;
            int totalRatings     = 0;

            foreach (int user in ratingsMatrix.Rows)
            {
                List <int> userRatedItems = ratingsMatrix[user].ToList <int>();
                foreach (int item in userRatedItems)
                {
                    int originalRating = ratingsMatrix[user, item];
                    ratingsMatrix.remove(user, item);
                    totalRatings++;

                    double predictedRating = Math.Round(ratingPredictor.PredictRating(ratingsMatrix, weightsMatrix, user, item, true));

                    if (predictedRating > 0)
                    {
                        if (predictedRating > 5)
                        {
                            predictedRating = 5;
                        }
                        else if (predictedRating < 1)
                        {
                            predictedRating = 1;
                        }

                        ratings++;
                        predictedRatings++;
                        totalErrors += Math.Abs(predictedRating - originalRating);
                    }

                    ratingsMatrix[user, item] = originalRating;
                }
            }

            mae = totalErrors / ratings;

            //double coverage = (double)predictedRatings / totalRatings;
            //Console.WriteLine($"Coverage: {coverage}");

            return(mae);
        }
        static void Main(string[] args)
        {
            //while (true)
            //{
            //Console.WriteLine("Choose your trust propagation metric");
            //Console.WriteLine("1 - Linear Trust Propagation");
            //Console.WriteLine("2 - Resource Allocation");

            //int metricInput = Convert.ToInt32(Console.ReadLine());

            //Console.WriteLine("Choose the propagation distance");

            //int propagationDistance = Convert.ToInt32(Console.ReadLine());

            //Console.WriteLine("Choose top N for trust propagation");
            //int topN = Convert.ToInt32(Console.ReadLine());

            //Console.WriteLine("Choose top K ecommendations");
            //int topK = Convert.ToInt32(Console.ReadLine());

            //EMetric metric = (EMetric)metricInput;

            //Console.WriteLine("New Execution!");
            //Console.WriteLine($"Metric: {metric.ToString()}");
            //Console.WriteLine($"TopN: { topN }");
            //Console.WriteLine($"TopK: {topK}");
            //Console.WriteLine($"Propagation Distance: {propagationDistance}");

            //Matrix<float> originalTrustMatrix = new Matrix<float>(Resources.trust_data_file);

            //Console.WriteLine("Building estimated trust matrix...");
            //Matrix<float> estimatedTrustMatrix = GetEstimatedTrustMatrix(originalTrustMatrix, metric, propagationDistance, topN);
            //Console.WriteLine("Estimated trust matrix built");

            //Matrix<int> ratingsMatrix = new Matrix<int>(Resources.rating_data_file);

            //Matrix<int> maxRatings = GetMaxRatings(ratingsMatrix);
            //maxRatings.WriteToFile("maxRatings");

            //RatingPredictor ratingPredictor = new RatingPredictor();

            //Dictionary<int, double> usersAverageRatings = GetUsersAverageRatings(ratingsMatrix, ratingPredictor);
            //ratingPredictor.setUsersAverageRatings(usersAverageRatings);

            //Stopwatch timer = new Stopwatch();
            //timer.Start();

            //Console.WriteLine("Running top k recommendation experiment...");
            //TopKRecommendation evaluator = new TopKRecommendation(topK);
            //evaluator.Evaluate(ratingsMatrix, estimatedTrustMatrix, maxRatings, ratingPredictor);

            //timer.Stop();
            //Console.WriteLine(timer.Elapsed);
            //Console.WriteLine();

            //Console.WriteLine($"Recall: {evaluator.Recall}");
            //Console.WriteLine($"Precision: {evaluator.Precision}");
            //Console.WriteLine();

            for (int propagationDistance = 3; propagationDistance <= 3; propagationDistance++)
            {
                for (int metricInput = 2; metricInput <= 2; metricInput++)
                {
                    for (int n = 1; n < topNValues.Length; n++)
                    {
                        for (int k = 0; k < topKValues.Length; k++)
                        {
                            int topN = topNValues[n];
                            int topK = topKValues[k];

                            //topN = 20;
                            //topK = 20;
                            //propagationDistance = 2;
                            //metricInput = 2;

                            EMetric metric = (EMetric)metricInput;

                            Console.WriteLine("New Execution!");
                            Console.WriteLine($"Metric: {metric.ToString()}");
                            Console.WriteLine($"TopN: { topN }");
                            Console.WriteLine($"TopK: {topK}");
                            Console.WriteLine($"Propagation Distance: {propagationDistance}");

                            Matrix <float> originalTrustMatrix = new Matrix <float>(Resources.trust_data_file);

                            Console.WriteLine("Building estimated trust matrix...");
                            Matrix <float> estimatedTrustMatrix = GetEstimatedTrustMatrix(originalTrustMatrix, metric, propagationDistance, topN);
                            Console.WriteLine("Estimated trust matrix built");

                            Matrix <int> originalRatingsMatrix = new Matrix <int>(Resources.rating_data_file);
                            Matrix <int> ratingsMatrix         = new Matrix <int>(Resources.rating_data_file);

                            Matrix <int> maxRatings = GetMaxRatings(ratingsMatrix, estimatedTrustMatrix);
                            maxRatings.WriteToFile("maxRatings");

                            RatingPredictor ratingPredictor = new RatingPredictor();

                            Dictionary <int, double> usersAverageRatings = GetUsersAverageRatings(ratingsMatrix, ratingPredictor);
                            ratingPredictor.setUsersAverageRatings(usersAverageRatings);

                            Stopwatch timer = new Stopwatch();
                            timer.Start();

                            Console.WriteLine("Running top k recommendation experiment...");
                            TopKRecommendation evaluator = new TopKRecommendation(topK);
                            evaluator.Evaluate(ratingsMatrix, estimatedTrustMatrix, maxRatings, ratingPredictor);

                            timer.Stop();
                            Console.WriteLine(timer.Elapsed);
                            Console.WriteLine();

                            Console.WriteLine($"Recall: {evaluator.Recall}");
                            Console.WriteLine($"Precision: {evaluator.Precision}");
                            Console.WriteLine($"Predicted Ratings: { evaluator.RatingsPredicted}");
                            Console.WriteLine($"Rated items predicted: { evaluator.ItemsPredicted }");
                            Console.WriteLine($"Coverage: {evaluator.Coverage}");
                            //Console.WriteLine($"Mean Average Error: {Experiment.MeanAverageError(originalRatingsMatrix, estimatedTrustMatrix, ratingPredictor)}");
                            Console.WriteLine();

                            using (StreamWriter writer = new StreamWriter(Resources.results, true))
                            {
                                writer.WriteLine("-------------------------------------");
                                writer.WriteLine($"Metric: {metric.ToString()}");
                                writer.WriteLine($"TopN: { topN }");
                                writer.WriteLine($"TopK: {topK}");
                                writer.WriteLine($"Propagation Distance: {propagationDistance}");
                                writer.WriteLine();
                                writer.WriteLine($"Recall: {evaluator.Recall}");
                                writer.WriteLine($"Precision: {evaluator.Precision}");
                                writer.WriteLine($"Predicted Ratings: { evaluator.RatingsPredicted}");
                                writer.WriteLine($"Rated items predicted: { evaluator.ItemsPredicted }");
                                writer.WriteLine($"Coverage: {evaluator.Coverage}");
                                writer.WriteLine($"Execution time: {timer.Elapsed}");
                                writer.WriteLine("----------------------------------------");
                                writer.WriteLine();
                            }
                        }
                    }
                }
            }
            //}
        }
Beispiel #3
0
        public static double MeanAbsoluteUserError(Matrix <int> ratingsMatrix, Matrix <float> weightsMatrix, RatingPredictor ratingPredictor)
        {
            double usersMAE = 0;

            foreach (int user in ratingsMatrix.Rows)
            {
                double     totalErrors    = 0;
                int        ratings        = 0;
                List <int> userRatedItems = ratingsMatrix[user].ToList <int>();

                foreach (int item in userRatedItems)
                {
                    int originalRating = ratingsMatrix[user, item];
                    ratingsMatrix.remove(user, item);

                    double predictedRating = Math.Round(ratingPredictor.PredictRating(ratingsMatrix, weightsMatrix, user, item, true));

                    if (predictedRating > 0)
                    {
                        if (predictedRating > 5)
                        {
                            predictedRating = 5;
                        }
                        else if (predictedRating < 1)
                        {
                            predictedRating = 1;
                        }

                        ratings++;
                        totalErrors += Math.Abs(predictedRating - originalRating);
                    }

                    ratingsMatrix[user, item] = originalRating;
                }

                if (ratings > 0)
                {
                    usersMAE += totalErrors / ratings;
                }
            }

            double maue = usersMAE / ratingsMatrix.Rows.Count();

            return(maue);
        }
        private static Dictionary <int, double> GetUsersAverageRatings(Matrix <int> ratingsMatrix, RatingPredictor ratingPredictor)
        {
            string averageRatingsFile = Resources.users_average_ratings;
            Dictionary <int, double> averageRatings;

            if (!File.Exists(averageRatingsFile))
            {
                averageRatings = ratingPredictor.CalculateUsersAverageRatings(ratingsMatrix);
                SaveDictionary(averageRatings, averageRatingsFile);
            }
            else
            {
                averageRatings = ReadDictionary(averageRatingsFile);
            }

            return(averageRatings);
        }