コード例 #1
0
        /// <summary>
        /// Computes the expected component-wise product of a confusion matrix and a loss matrix.
        /// </summary>
        /// <param name="instanceSource">The instance source providing the ground truth (used to compute the expected confusion matrix).</param>
        /// <param name="predictions">
        /// A sparse users-by-items matrix of predicted rating distributions (used to compute the expected confusion matrix).
        /// </param>
        /// <param name="lossMatrix">The loss matrix.</param>
        /// <param name="aggregationMethod">
        /// A method specifying how metrics are aggregated over all instances (used to compute the expected confusion matrix).</param>
        /// <returns>The computed expected weighted confusion.</returns>
        public double ExpectedWeightedConfusion(
            TInstanceSource instanceSource,
            IDictionary <TUser, IDictionary <TItem, Discrete> > predictions,
            RatingMatrix lossMatrix,
            RecommenderMetricAggregationMethod aggregationMethod = RecommenderMetricAggregationMethod.Default)
        {
            RatingMatrix confusionMatrix = this.ExpectedConfusionMatrix(instanceSource, predictions, aggregationMethod);

            return(RatingMatrix.ComponentwiseProduct(confusionMatrix, lossMatrix));
        }
コード例 #2
0
        /// <summary>
        /// Generates a loss matrix from a given loss function.
        /// </summary>
        /// <param name="minRating">The minimum rating.</param>
        /// <param name="maxRating">The maximum rating.</param>
        /// <param name="lossFunc">The loss function, which operates on index pairs.</param>
        /// <returns>The generated loss matrix.</returns>
        private static RatingMatrix LossMatrix(int minRating, int maxRating, Func <int, int, double> lossFunc)
        {
            var result = new RatingMatrix(minRating, maxRating);

            for (int i = 1; i <= maxRating; ++i)
            {
                for (int j = 1; j <= maxRating; ++j)
                {
                    result[i, j] = lossFunc(i, j);
                }
            }

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Computes the component-wise products of two rating matrices.
        /// </summary>
        /// <param name="lhs">The left-hand side matrix.</param>
        /// <param name="rhs">The right-hand side matrix.</param>
        /// <returns>The sum of the products of the corresponding elements of the two matrices.</returns>
        public static double ComponentwiseProduct(RatingMatrix lhs, RatingMatrix rhs)
        {
            if (lhs.MinRating != rhs.MinRating || lhs.MaxRating != rhs.MaxRating)
            {
                throw new ArgumentException("The given matrices are incompatible!");
            }

            double result = 0;

            for (int i = lhs.MinRating; i <= lhs.MaxRating; ++i)
            {
                for (int j = lhs.MinRating; j <= lhs.MaxRating; ++j)
                {
                    result += lhs[i, j] * rhs[i, j];
                }
            }

            return(result);
        }
コード例 #4
0
        /// <summary>
        /// Computes the expected confusion matrix.
        /// </summary>
        /// <param name="instanceSource">The instance source providing the ground truth.</param>
        /// <param name="predictions">A sparse users-by-items matrix of predicted rating distributions.</param>
        /// <param name="aggregationMethod">A method specifying how metrics are aggregated over all instances.</param>
        /// <returns>The computed expected confusion matrix.</returns>
        public RatingMatrix ExpectedConfusionMatrix(
            TInstanceSource instanceSource,
            IDictionary <TUser, IDictionary <TItem, Discrete> > predictions,
            RecommenderMetricAggregationMethod aggregationMethod = RecommenderMetricAggregationMethod.Default)
        {
            IStarRatingInfo <TGroundTruthRating> starRatingInfo = this.mapping.GetRatingInfo(instanceSource);

            var result = new RatingMatrix(starRatingInfo.MinStarRating, starRatingInfo.MaxStarRating);

            for (int predictedRating = starRatingInfo.MinStarRating; predictedRating <= starRatingInfo.MaxStarRating; ++predictedRating)
            {
                for (int trueRating = starRatingInfo.MinStarRating; trueRating <= starRatingInfo.MaxStarRating; ++trueRating)
                {
                    result[predictedRating, trueRating] = this.ModelDomainRatingPredictionMetricExpectation(
                        instanceSource,
                        predictions,
                        (p, t) => p == predictedRating && t == trueRating ? 1.0 : 0.0,
                        aggregationMethod);
                }
            }

            return(result);
        }