Пример #1
0
        public void Statistics_Metrics_Test()
        {
            var values = AdvancedStatistics.SE(xActual, yPredicted);

            Assert.Equal(4.443698, values.Sum(), 4);

            var value = AdvancedStatistics.MSE(xActual, yPredicted);

            Assert.Equal(0.1481236, value, 4);

            values = AdvancedStatistics.AE(xActual, yPredicted);
            Assert.Equal(9.810468245, values.Sum(), 4);

            value = AdvancedStatistics.MAE(xActual, yPredicted);
            Assert.Equal(0.327015608, value, 4);

            values = AdvancedStatistics.APE(xActual, yPredicted);
            Assert.Equal(36.67386, values.Sum(), 4);

            value = AdvancedStatistics.MAPE(xActual, yPredicted);
            Assert.Equal(1.222462, value, 4);

            value = AdvancedStatistics.SMAPE(xActual, yPredicted);
            Assert.Equal(0.6992009, value, 4);

            //
            values = AdvancedStatistics.SLE(xActual, yPredicted);
            Assert.Equal(2.104749, values.Sum(), 4);

            value = AdvancedStatistics.MSLE(xActual, yPredicted);
            Assert.Equal(0.0701583, value, 4);

            value = AdvancedStatistics.RMSLE(xActual, yPredicted);
            Assert.Equal(0.2648741, value, 4);

            value = AdvancedStatistics.RSE(xActual, yPredicted);
            Assert.Equal(1.762048, value, 4);

            value = AdvancedStatistics.RRSE(xActual, yPredicted);
            Assert.Equal(1.327422, value, 4);

            value = AdvancedStatistics.RAE(xActual, yPredicted);
            Assert.Equal(1.265375, value, 4);


            value = AdvancedStatistics.MASE(xActual, yPredicted, 1);
            Assert.Equal(1.139131, value, 4);

            value = AdvancedStatistics.MASE(xActual, yPredicted, 3);
            Assert.Equal(1.055599, value, 4);

            value = AdvancedStatistics.MASE(xActual, yPredicted, 5);
            Assert.Equal(0.9490496, value, 4);

            value = AdvancedStatistics.MASE(xActual, yPredicted, 7);
            Assert.Equal(0.8738611, value, 4);

            value = AdvancedStatistics.MASE(xActual, yPredicted, 9);
            Assert.Equal(1.024809, value, 4);
        }
Пример #2
0
        public double[] CalculateOutput(IChromosome chromosome, IParameters param, bool isTrainingData)
        {
            var ch   = ((Chromosome)chromosome);
            var node = ch.expressionTree;

            if (TData == null && isTrainingData == false)//there is no test data
            {
                return(null);
            }

            var rowCount = isTrainingData ? Data.Length : TData.Length;
            var y        = new double[rowCount];

            //
            for (int i = 0; i < rowCount; i++)
            {
                var rowInputData = isTrainingData ? GetDataInputRow(i) : GetTestDataInputRow(i);

                // evaluate the function against each rowData
                if (param.IsMultipleOutput && ch.ExtraData != null)
                {
                    var oneHotVector = node.Evaluate2(rowInputData, param);

                    //calculate Mahanalobis distance for each training sample
                    double minMD            = double.MaxValue;
                    int    predictedCluster = 0;
                    foreach (var c in ch.ExtraData.Keys)
                    {
                        var md = AdvancedStatistics.MD(oneHotVector, ch.ExtraData[c].Item1, ch.ExtraData[c].Item2);

                        //map result with class
                        if (minMD > md)
                        {
                            predictedCluster = c;
                            minMD            = md;
                        }
                    }
                    //map actual and predicted class value
                    y[i] = predictedCluster;
                }
                else// evaluate the function against each rowData
                {
                    y[i] = node.Evaluate(rowInputData, param);
                }
            }

            return(y);
        }
Пример #3
0
        public float Evaluate(IChromosome ch, IParameters param, int it)
        {
            var iData = GetData();

            var    chr        = (Chromosome)ch;
            var    expTree    = chr.expressionTree;
            double fitness    = 0;
            double rowFitness = 0.0;

            double[] y;

            var evalData    = new List <(int, double[])>();
            var dic         = new Dictionary <int, List <double[]> >();
            var preCalcData = new Dictionary <int, (double[], double[][])>();
            var result      = new List <(int, int)>();

            //for each trainign sample
            for (int i = 0; i < iData.RowCount; i++)
            {
                var rowInputData = iData.GetDataInputRow(i);
                // evaluate the function against each rowData
                y = expTree.Evaluate2(rowInputData, param);
                if (y == null)
                {
                    return(float.NaN);
                }
                //get output Value
                var yo = (int)iData.GetRowOutput(i);

                //add result in to list
                evalData.Add((yo, y));

                //map result with class
                if (dic.ContainsKey(yo))
                {
                    dic[yo].Add(y);
                }
                else
                {
                    dic.Add(yo, new List <double[]>()
                    {
                        y
                    });
                }
            }
            //calculate centroids and Covariance matrix for each cluster
            for (int i = 0; i < dic.Keys.Count; i++)
            {
                //class value
                var c    = dic.Keys.ElementAt(i);
                var vals = dic.Values.ElementAt(i).ToArray();
                //predicted values for the class
                var cData = AdvancedStatistics.ToColumnVector <double>(vals);

                //calculated centroid for the cData
                var cen = cData.Select(x => x.MeanOf()).ToArray();

                //calculate CovarianceMatrix
                var cov = BasicStatistics.Covariance(cData);
                if (!isValid(cov))
                {
                    return(float.NaN);
                }
                preCalcData.Add(c, (cen, cov));
            }

            //for each training sample
            //go through all samples
            foreach (var item in evalData)
            {
                //calculate Mahanalobis distance for each training sample
                double minMD            = double.MaxValue;
                int    predictedCluster = 0;
                foreach (var c in dic.Keys)
                {
                    var md = AdvancedStatistics.MD(item.Item2, preCalcData[c].Item1, preCalcData[c].Item2);

                    //map result with class
                    if (minMD > md)
                    {
                        predictedCluster = c;
                        minMD            = md;
                    }
                }
                //map actual and predicted class value
                result.Add((item.Item1, predictedCluster));
            }
            float corectedPredicted = result.Where(x => x.Item1 == x.Item2).Count();
            var   rawFitness        = corectedPredicted / (float)result.Count;



            if (double.IsNaN(rawFitness) || double.IsInfinity(rawFitness))
            {
                fitness = float.NaN;
            }
            else//calculate adjusted fitness
            {
                fitness = (rawFitness * 1000.0);
            }
            //store extra data into chromosome
            chr.ExtraData = preCalcData;

            return((float)System.Math.Round(fitness, 2));
        }
Пример #4
0
        public static ModelPerformance CalculatePerformance(EvaluationResult evalResult, string dataSetName = "Training set")
        {
            var mpt = new ModelPerformance();

            mpt.DatSetName = dataSetName;
            mpt.Classes    = evalResult.OutputClasses.ToArray();

            //define collections for the results
            var actual    = new double[evalResult.Actual.Count()];
            var predicted = new double[evalResult.Predicted.Count()];

            //extract result value
            for (int i = 0; i < evalResult.Actual.Count(); i++)
            {
                actual[i] = evalResult.Actual[i];
            }

            for (int i = 0; i < evalResult.Predicted.Count(); i++)
            {
                predicted[i] = evalResult.Predicted[i];
            }

            //regression
            if (evalResult.OutputClasses.Count == 1)
            {
                //Training data set
                mpt.SE   = (float)actual.SE(predicted);
                mpt.RMSE = (float)actual.RMSE(predicted);
                mpt.NSE  = (float)actual.NSE(predicted);
                mpt.PB   = (float)actual.PBIAS(predicted);
                mpt.CORR = (float)actual.R(predicted);
                mpt.DETC = (float)actual.R2(predicted);
            }
            if (evalResult.OutputClasses.Count < 2 && evalResult.ActualEx.First().Count > 1)
            {
                //Training data set
                var actualValues    = evalResult.ActualEx.Select(x => x.Select(xx => (double)xx).ToArray()).ToArray();
                var predictedValues = evalResult.PredictedEx.Select(x => x.Select(xx => (double)xx).ToArray()).ToArray();

                mpt.SE   = (float)AdvancedStatistics.SE(actualValues, predictedValues);
                mpt.RMSE = (float)AdvancedStatistics.RMSE(actualValues, predictedValues);
                mpt.NSE  = (float)AdvancedStatistics.NSE(actualValues, predictedValues);
                mpt.PB   = (float)AdvancedStatistics.PBIAS(actualValues, predictedValues);
                mpt.CORR = (float)AdvancedStatistics.R(actualValues, predictedValues);
                mpt.DETC = (float)AdvancedStatistics.R2(actualValues, predictedValues);
            }
            else if (evalResult.OutputClasses.Count > 1)
            {
                var retVal = TransformResult(evalResult.ActualEx, evalResult.PredictedEx);
                retVal.Add("Classes", mpt.Classes.ToList <object>());
                mpt.PerformanceData = retVal;

                if (evalResult.OutputClasses.Count == 2)
                {
                    //construct confusion matrix
                    var o  = retVal["obs_train"].Select(x => int.Parse(x.ToString())).ToArray();
                    var p  = retVal["prd_train"].Select(x => ((double)x) < 0.5 ? 0 : 1).ToArray();
                    var cm = new ConfusionMatrix(o, p, 2);
                    //get result
                    mpt.ER = (float)ConfusionMatrix.Error(cm.Matrix);
                    //mpt.AUC =

                    //confusion matrix for current threshold
                    mpt.Acc = (float)ConfusionMatrix.OAC(cm.Matrix);
                    mpt.ER  = (float)ConfusionMatrix.Error(cm.Matrix);

                    mpt.Precision = (float)ConfusionMatrix.Precision(cm.Matrix, 1);
                    mpt.Recall    = (float)ConfusionMatrix.Recall(cm.Matrix, 1);
                    mpt.F1Score   = (float)ConfusionMatrix.Fscore(cm.Matrix, 1);

                    mpt.HSS = (float)ConfusionMatrix.HSS(cm.Matrix, o.Length);
                    mpt.PSS = (float)ConfusionMatrix.PSS(cm.Matrix, o.Length);

                    //lab
                    //mpt.Label = m_Classes[1];// "TRUE";
                    //txNegativeLable.Text = m_Classes[0];// "FALSE";

                    //false and true positive negative
                    mpt.FN = (float)cm.Matrix[1][0]; //cm.FalseNegatives
                    mpt.FP = (float)cm.Matrix[0][1]; //cm.FalsePositives
                    mpt.TP = (float)cm.Matrix[1][1]; //cm.TruePositives
                    mpt.TN = (float)cm.Matrix[0][0]; //cm.TrueNegatives
                }
                else
                {
                    //construct confusion matrix
                    var o  = retVal["obs_train"].Select(x => int.Parse(x.ToString())).ToArray();
                    var p  = retVal["prd_train"].Select(x => int.Parse(x.ToString())).ToArray();
                    var cm = new ConfusionMatrix(o, p, evalResult.OutputClasses.Count);

                    //confusion matrix for current threshold
                    mpt.OAcc     = (float)ConfusionMatrix.OAC(cm.Matrix);
                    mpt.AAcc     = (float)ConfusionMatrix.AAC(cm.Matrix);
                    mpt.MacPrec  = (float)ConfusionMatrix.MacroPrecision(cm.Matrix);
                    mpt.MacRcall = (float)ConfusionMatrix.MacroRecall(cm.Matrix);
                    mpt.MicPrec  = (float)ConfusionMatrix.MicroPrecision(cm.Matrix);
                    mpt.MicRcall = (float)ConfusionMatrix.MicroRecall(cm.Matrix);
                    //mpt.MacFscore = (float)ConfusionMatrix.MacroFscore(cm.Matrix);
                    //mpt.MicFscore = (float)ConfusionMatrix.MicroFscore(cm.Matrix);

                    mpt.HSS = (float)ConfusionMatrix.HSS(cm.Matrix, o.Length);
                    mpt.PSS = (float)ConfusionMatrix.PSS(cm.Matrix, o.Length);
                }
            }

            return(mpt);
        }