Exemplo n.º 1
0
        public ConfusionMatrix Validate(int numberOfFolds, FuzzyTable fuzzyTable, IProcessable algorithm, double tolerance = .5)
        {
            int instancesSize = fuzzyTable.GetTable().Rows.Count;

            ArrayList[] foldsInstances = new ArrayList[numberOfFolds];
            for (int i = 0; i < numberOfFolds; i++)
            {
                foldsInstances[i] = new ArrayList();
            }
            int numberOfClassValues = fuzzyTable.getClassAttribute().Labels.Length;

            FuzzyAttributeLabel[] classValues = new FuzzyAttributeLabel[numberOfClassValues];
            for (int i = 0; i < numberOfClassValues; i++)
            {
                classValues[i] = fuzzyTable.getClassAttribute().Labels[i];
            }
            double[] countClass = getClassValuesNumber(classValues, numberOfClassValues, fuzzyTable);
            int      foldSize   = instancesSize / numberOfFolds;

            double[] foldClassSize = new double[countClass.Length];

            for (int i = 0; i < foldClassSize.Length; i++)
            {
                double perc = countClass[i] / (double)instancesSize;
                foldClassSize[i] = (foldSize * perc);
            }


            var dataCountInOneReplication = fuzzyTable.DataCount() / numberOfFolds; // the size of the fold
            var confusionMatrix           = new ConfusionMatrix();
            var noDataTable = fuzzyTable.CloneNoData();
            var rngIndexes  = getRNGIndexes(instancesSize);

            for (int i = 0; i < numberOfFolds; i++)
            {
                var      instancesAdded     = new ArrayList(foldSize); // what will be deleted
                double[] foldClassSizeAdded = new double[foldClassSize.Length];
                foreach (int index in rngIndexes)
                {
                    var label1Value = this.getData(fuzzyTable, classValues[0], index); // e.g c1= 0.8
                    var label2Value = this.getData(fuzzyTable, classValues[1], index); // e.g c2= 0.2
                    if (label1Value > 0.5)                                             // c1 > 0.5
                    {
                        if (foldClassSizeAdded[0] < foldClassSize[0])
                        {
                            foldClassSizeAdded[0] += label1Value;
                            foldsInstances[i].Add(index); // add the index to the fold
                            instancesAdded.Add(index);
                        }
                    }
                    else     // c2 > 0.5
                    {
                        if (foldClassSizeAdded[1] < foldClassSize[1])
                        {
                            foldClassSizeAdded[1] += label2Value;
                            foldsInstances[i].Add(index); // add the index to the fold
                            instancesAdded.Add(index);
                        }
                    }

                    if (foldClassSizeAdded[0] >= foldClassSize[0] && foldClassSizeAdded[1] >= foldClassSize[1])
                    {
                        break;
                    }
                }
                // remove indexes that were used in this fold
                foreach (var item in instancesAdded)
                {
                    rngIndexes.Remove(item);
                }
            }
            // now i have the folds
            for (var fold = 0; fold < numberOfFolds; fold++)
            {
                var table         = (FuzzyTable)fuzzyTable.CloneNoData();
                var testDataTable = (FuzzyTable)table.CloneNoData();
                addFoldsDataToTableAndTestTable(table, testDataTable, foldsInstances, fold, numberOfFolds, fuzzyTable);
                algorithm.init(table);
                var rules = algorithm.process();
                if (!ExistsAtLeastOneRuleForEachClassAttribute(table, rules))
                {
                    return(null);
                }
                CalculateResultForRules(testDataTable, rules, confusionMatrix, tolerance);
            }

            // Console.WriteLine("Accuracy: "+confusionMatrix.Accuracy());
            // Console.WriteLine("Sensitivity: "+confusionMatrix.Sensitivity());
            // Console.WriteLine("Specificity: "+confusionMatrix.Specificity());
            // Console.WriteLine("Precision: "+confusionMatrix.Precision());
            // Console.WriteLine("Krit: "+confusionMatrix.Criteria());
            // Console.WriteLine("Kriteria: "+(confusionMatrix.Sensitivity() + confusionMatrix.Specificity()) / 2);

            confusionMatrix.CalculatePercentNumbers();
            return(confusionMatrix);
        }