public Dictionary <string, double> Classify(FuzzyTable table, int rowId, List <Rule> fuzzyRules) { var classAttribs = table.getClassAttribute(); // K2 Ak min {1.0, 0.9} Potom C is c1, find min var Ei = new Dictionary <Rule, double>(); foreach (var rule in fuzzyRules) { var membershipDegree = double.MaxValue; foreach (var attr in rule.Items) { var value = table.getData(attr.Id, rowId); if (value < membershipDegree) { membershipDegree = value; } } if (!Ei.ContainsKey(rule)) { Ei.Add(rule, membershipDegree); } } //K3 var gcj = new Dictionary <string, List <Rule> >(); for (var i = 0; i < table.getClassAttribute().Labels.Length; i++) { gcj[table.getClassAttribute().Labels[i].Id.ToString()] = new List <Rule>(); } foreach (var rule in fuzzyRules) { gcj[rule.C.Id].Add(rule); } //K4 Ak max{0.9, 0, 0, 0.4} Potom C is c1 var returnValue = new Dictionary <string, double>(); foreach (var classAttr in classAttribs.Labels) { returnValue.Add(classAttr.Id.ToString(), double.MinValue); } foreach (var cj in gcj.Keys) { foreach (var rule in gcj[cj]) { var value = Ei[rule]; if (value > returnValue[rule.C.Id]) { returnValue[rule.C.Id] = value; } } } return(returnValue); }
private bool ExistsAtLeastOneRuleForEachClassAttribute(FuzzyTable table, List <Rule> rules) { foreach (var classAttr in table.getClassAttribute().Labels) { var classAttrExistsInRules = false; foreach (var rule in rules) { if (rule.C.Id.Equals(classAttr.Id.ToString())) { classAttrExistsInRules = true; break; } } if (!classAttrExistsInRules) { return(false); } } return(true); }
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); }