/// <summary> /// Calculates absolute error between prediction and ground truth /// </summary> /// <param name="testSource">A source of ratings made by users</param> /// <param name="mapping">A mapping to convert source ratings into experiment specific ones</param> /// <param name="predictions">Ratings which are predicted by the recommender</param> /// <returns></returns> private static Dictionary <Movie, List <double> > PredictionError( SplitInstanceSource <string> testSource, IStarRatingRecommenderMapping <SplitInstanceSource <string>, RatingTriple, string, Movie, int, NoFeatureSource, Vector> mapping, IDictionary <string, IDictionary <Movie, IDictionary <int, double> > > predictions) { var evMapping = mapping.ForEvaluation(); var allErrors = new Dictionary <Movie, List <double> >(); foreach (var userWithPredictionList in predictions) { foreach (var itemPrediction in userWithPredictionList.Value) { var prediction = itemPrediction.Value; var groundTruth = evMapping.GetRating(testSource, userWithPredictionList.Key, itemPrediction.Key); var predictedRating = prediction.Aggregate((l, r) => l.Value > r.Value ? l : r).Key; var error = Math.Abs(groundTruth - predictedRating); if (allErrors.TryGetValue(itemPrediction.Key, out var values)) { values.Add(error); } else { allErrors.Add(itemPrediction.Key, new List <double>() { error }); } } } return(allErrors); }
/// <summary> /// Takes test data from data source and represents it in the form of jagged array. /// The first dimension represents users, the second dimension represents movies. /// </summary> /// <param name="mapping">A mapping to convert ratings to a scale used exactly in the current experiment. </param> /// <returns></returns> public double[][] GetGroundTruth( IStarRatingRecommenderMapping <SplitInstanceSource <string>, RatingTriple, string, Movie, int, NoFeatureSource, Vector> mapping ) { Rand.Restart(RandomSeed); var testSource = SplitInstanceSource.Test(RatingsPath); var mappingForEvaluation = mapping.ForEvaluation(); var users = mappingForEvaluation.GetUsers(testSource); var ratings = users.Select(u => mappingForEvaluation.GetItemsRatedByUser(testSource, u) .Select(m => (double)mappingForEvaluation.GetRating(testSource, u, m))); var groundTruthArray = GetJaggedDoubles(ratings); return(groundTruthArray); }