Exemple #1
0
        public double[] calculatePIList(FuzzyAttributeLabel G, List <int> rows)
        {
            var labels = this.table.getClassAttribute().Labels;
            var PIList = new double[labels.Length];

            for (int i = 0; i < labels.Length; i++)
            {
                PIList[i] = calculateTruthRate(G, labels[i], rows);
            }
            Array.Sort(PIList);
            Array.Reverse(PIList);
            var max = PIList[0];

            for (int i = 0; i < PIList.Length; i++)
            {
                PIList[i] = PIList[i] / max;
            }
            Array.Resize(ref PIList, PIList.Length + 1);
            PIList[PIList.Length - 1] = 0;

            for (int i = 0; i < PIList.Length; i++)
            {
                if (Double.IsNaN(PIList[i]))
                {
                    PIList[i] = 0;
                }
            }
            return(PIList);
        }
Exemple #2
0
        public double calculateSubAttributeCardinality(FuzzyAttributeLabel label, List <int> rows)
        {
            double value = 0;

            foreach (int index in rows)
            {
                value += (double)this.table.GetTable().Rows[index][label.Id];
            }
            return(value);
        }
Exemple #3
0
        private FuzzyAttributeLabel getMaxLabelForC(int p, string name)
        {
            FuzzyAttributeLabel labelMax = null;
            double labelMaxValue         = -1;

            foreach (var label in this.table.getClassAttribute().Labels)
            {
                if (labelMaxValue < (double)this.table.GetTable().Rows[p][label.Id.ToString()])
                {
                    labelMaxValue = (double)this.table.GetTable().Rows[p][label.Id.ToString()];
                    labelMax      = label;
                }
            }
            return(labelMax);
        }
Exemple #4
0
        public double calculateSubN(FuzzyAttributeLabel subB, List <int> rows)
        {
            if (this.table.getClassAttribute().Labels.Length == 0)
            {
                return(0);
            }
            double value        = 0;
            var    sortedPIList = calculatePIList(subB, rows);

            for (int i = 2; i <= this.table.getClassAttribute().Labels.Length; i++)
            {
                // (PI[i] - PI[i+1]) * ln i
                value += (sortedPIList[i - 1] - sortedPIList[i]) * Math.Log(i);
            }
            return(value);
        }
Exemple #5
0
        public double calculateTruthRate(FuzzyAttributeLabel G, FuzzyAttributeLabel subC, List <int> rows)
        {
            double top    = 0;
            double bottom = 0;

            foreach (int index in rows)
            {
                top += minimumTNorm((double)this.table.GetTable().Rows[index][G.Id.ToString()],
                                    (double)this.table.GetTable().Rows[index][subC.Id.ToString()]);
                bottom += minimumTNorm((double)this.table.GetTable().Rows[index][G.Id.ToString()], 1);
            }
            if (Double.IsNaN(top / bottom))
            {
                return(0);
            }
            return(top / bottom);
        }
Exemple #6
0
        private double getNumeratorValueForSum(Dictionary <string, LabelValue> pLabelOrder, Dictionary <string, LabelValue> qLabelOrder, FuzzyAttributeLabel label)
        {
            var    a   = pLabelOrder[label.Id].IndexValue;
            var    b   = qLabelOrder[label.Id].IndexValue;
            double val = Math.Abs(pLabelOrder[label.Id].IndexValue - qLabelOrder[label.Id].IndexValue) + 1;

            return(val * Math.Abs(pLabelOrder[label.Id].Value - qLabelOrder[label.Id].Value));
        }
 public double getData(FuzzyTable fuzzyTable, FuzzyAttributeLabel attributeLabel, int row)
 {
     return((double)fuzzyTable.getDataByAttribute(attributeLabel, row));
 }
        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);
        }