Пример #1
0
        protected void CalculateRegressionResults()
        {
            double[] estimatedTrainingValues = EstimatedTrainingValues.ToArray(); // cache values
            double[] originalTrainingValues  = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToArray();
            double[] estimatedTestValues     = EstimatedTestValues.ToArray();     // cache values
            double[] originalTestValues      = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToArray();

            OnlineCalculatorError errorState;
            double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);

            TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
            double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);

            TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;

            double trainingR = OnlinePearsonsRCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);

            TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR * trainingR : double.NaN;
            double testR = OnlinePearsonsRCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);

            TestRSquared = errorState == OnlineCalculatorError.None ? testR * testR : double.NaN;

            double trainingNormalizedGini = NormalizedGiniCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);

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

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

            TrainingNormalizedGiniCoefficient = trainingNormalizedGini;
            TestNormalizedGiniCoefficient     = testNormalizedGini;
        }
        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;
            }
        }