protected OnlineAccuracyCalculator(OnlineAccuracyCalculator original, Cloner cloner)
     : base(original, cloner)
 {
     correctlyClassified = original.correctlyClassified;
     n          = original.n;
     errorState = original.errorState;
 }
Exemple #2
0
        public static double Calculate(IEnumerable <double> originalValues, IEnumerable <double> estimatedValues, out OnlineCalculatorError errorState)
        {
            IEnumerator <double>     originalEnumerator  = originalValues.GetEnumerator();
            IEnumerator <double>     estimatedEnumerator = estimatedValues.GetEnumerator();
            OnlineAccuracyCalculator accuracyCalculator  = new OnlineAccuracyCalculator();

            // always move forward both enumerators (do not use short-circuit evaluation!)
            while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext())
            {
                double original  = originalEnumerator.Current;
                double estimated = estimatedEnumerator.Current;
                accuracyCalculator.Add(original, estimated);
                if (accuracyCalculator.ErrorState != OnlineCalculatorError.None)
                {
                    break;
                }
            }

            // check if both enumerators are at the end to make sure both enumerations have the same length
            if (accuracyCalculator.ErrorState == OnlineCalculatorError.None &&
                (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()))
            {
                throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
            }
            else
            {
                errorState = accuracyCalculator.ErrorState;
                return(accuracyCalculator.Accuracy);
            }
        }
        public static double CalculateQuality(IEnumerable <double> targetValues, IEnumerable <double> estimatedClassValues)
        {
            OnlineCalculatorError errorState;
            var ret = OnlineAccuracyCalculator.Calculate(targetValues, estimatedClassValues, out errorState);

            if (errorState != OnlineCalculatorError.None)
            {
                throw new InvalidOperationException("Error during calculation with replaced inputs.");
            }
            return(ret);
        }
    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
      OnlineAccuracyCalculator accuracyCalculator = new OnlineAccuracyCalculator();

      // always move forward both enumerators (do not use short-circuit evaluation!)
      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
        double original = originalEnumerator.Current;
        double estimated = estimatedEnumerator.Current;
        accuracyCalculator.Add(original, estimated);
        if (accuracyCalculator.ErrorState != OnlineCalculatorError.None) break;
      }

      // check if both enumerators are at the end to make sure both enumerations have the same length
      if (accuracyCalculator.ErrorState == OnlineCalculatorError.None &&
          (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
      } else {
        errorState = accuracyCalculator.ErrorState;
        return accuracyCalculator.Accuracy;
      }
    }
        protected void CalculateClassificationResults()
        {
            double[] estimatedTrainingClassValues = EstimatedTrainingClassValues.ToArray(); // cache values
            double[] originalTrainingClassValues  = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToArray();

            double[] estimatedTestClassValues = EstimatedTestClassValues.ToArray(); // cache values
            double[] originalTestClassValues  = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToArray();

            var    positiveClassName  = ProblemData.PositiveClass;
            double positiveClassValue = ProblemData.GetClassValue(positiveClassName);
            ClassificationPerformanceMeasuresCalculator trainingPerformanceCalculator = new ClassificationPerformanceMeasuresCalculator(positiveClassName, positiveClassValue);
            ClassificationPerformanceMeasuresCalculator testPerformanceCalculator     = new ClassificationPerformanceMeasuresCalculator(positiveClassName, positiveClassValue);

            OnlineCalculatorError errorState;
            double trainingAccuracy = OnlineAccuracyCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);

            if (errorState != OnlineCalculatorError.None)
            {
                trainingAccuracy = double.NaN;
            }
            double testAccuracy = OnlineAccuracyCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);

            if (errorState != OnlineCalculatorError.None)
            {
                testAccuracy = double.NaN;
            }

            TrainingAccuracy = trainingAccuracy;
            TestAccuracy     = testAccuracy;

            double trainingNormalizedGini = NormalizedGiniCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);

            if (errorState != OnlineCalculatorError.None)
            {
                trainingNormalizedGini = double.NaN;
            }
            double testNormalizedGini = NormalizedGiniCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);

            if (errorState != OnlineCalculatorError.None)
            {
                testNormalizedGini = double.NaN;
            }

            TrainingNormalizedGiniCoefficient = trainingNormalizedGini;
            TestNormalizedGiniCoefficient     = testNormalizedGini;

            ClassificationPerformanceMeasures.Reset();

            trainingPerformanceCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues);
            if (trainingPerformanceCalculator.ErrorState == OnlineCalculatorError.None)
            {
                ClassificationPerformanceMeasures.SetTrainingResults(trainingPerformanceCalculator);
            }

            testPerformanceCalculator.Calculate(originalTestClassValues, estimatedTestClassValues);
            if (testPerformanceCalculator.ErrorState == OnlineCalculatorError.None)
            {
                ClassificationPerformanceMeasures.SetTestResults(testPerformanceCalculator);
            }

            if (ProblemData.Classes == 2)
            {
                var f1Training = FOneScoreCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
                if (errorState == OnlineCalculatorError.None)
                {
                    ClassificationPerformanceMeasures.TrainingF1Score = f1Training;
                }
                var f1Test = FOneScoreCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
                if (errorState == OnlineCalculatorError.None)
                {
                    ClassificationPerformanceMeasures.TestF1Score = f1Test;
                }
            }

            var mccTraining = MatthewsCorrelationCoefficientCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);

            if (errorState == OnlineCalculatorError.None)
            {
                ClassificationPerformanceMeasures.TrainingMatthewsCorrelation = mccTraining;
            }
            var mccTest = MatthewsCorrelationCoefficientCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);

            if (errorState == OnlineCalculatorError.None)
            {
                ClassificationPerformanceMeasures.TestMatthewsCorrelation = mccTest;
            }
        }