/// <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); }
/// <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."); }