예제 #1
0
        /// <summary>
        /// Calculates the f1 score from a confusion matrix
        /// </summary>
        /// <param name="cm"> confusion matrix metrics</param>
        /// <returns> the f1 result</returns>
        private static double F1(this ConfusionMatrix cm)
        {
            var precision   = cm.Precision();
            var recall      = cm.Recall();
            var denominator = precision + recall;

            return(Math.Abs(denominator) > double.Epsilon ? 2 * (precision * recall) / denominator : 0);
        }
예제 #2
0
        /// <summary>
        /// Calculates the F<sub>β</sub> score from a confusion matrix.
        /// </summary>
        /// <param name="matrix">Confusion matrix.</param>
        /// <param name="beta">F-measure value.</param>
        /// <returns>F<sub>β</sub> score.</returns>
        internal static double FScore(this ConfusionMatrix matrix, double beta)
        {
            var precision   = matrix.Precision();
            var recall      = matrix.Recall();
            var denominator = (Math.Pow(beta, 2) * precision) + recall;

            return(Math.Abs(denominator) > double.Epsilon ? (1 + Math.Pow(beta, 2)) * (precision * recall) / denominator : 0);
        }
        /// <summary>
        /// Calculates the F<sub>1</sub> score from a confusion matrix.
        /// </summary>
        /// <param name="matrix">Confusion matrix.</param>
        /// <returns>F<sub>1</sub> score.</returns>
        internal static double F1(this ConfusionMatrix matrix)
        {
            var precision   = matrix.Precision();
            var recall      = matrix.Recall();
            var denominator = precision + recall;

            return(Math.Abs(denominator) > double.Epsilon ? 2 * (precision * recall) / denominator : 0);
        }
예제 #4
0
        /// <summary>
        /// Calculates Precision, Recall and F1 Score
        /// </summary>
        /// <param name="cm">confusion matrix metrics</param>
        /// <param name="roundingplace"> Specifies the number of digits after the decimal point</param>
        /// <returns>List that contains the results calculated</returns>
        public static List <double> CalcAccuracy(this ConfusionMatrix cm, int roundingplace = 4)
        {
            var metrics = new List <double>
            {
                Math.Round(cm.Precision(), roundingplace),
                Math.Round(cm.Recall(), roundingplace),
                Math.Round(cm.F1(), roundingplace)
            };

            return(metrics);
        }
예제 #5
0
        /// <summary>
        /// Gets the metric value for the given confusion matrix.
        /// </summary>
        /// <param name="matrix">Confusion matrix.</param>
        /// <param name="metric">Metric to compute.</param>
        /// <returns>Metric value.</returns>
        internal static double GetMetric(this ConfusionMatrix matrix, string metric)
        {
            if (metric == null)
            {
                return(matrix.F1());
            }
            else if (metric == "precision")
            {
                return(matrix.Precision());
            }
            else if (metric == "recall")
            {
                return(matrix.Recall());
            }
            else if (metric.StartsWith("f", StringComparison.OrdinalIgnoreCase) && double.TryParse(metric.Substring(1), out var beta))
            {
                return(matrix.FScore(beta));
            }

            throw new ArgumentOutOfRangeException(nameof(metric), $"Invalid metric value provided.");
        }