- Timestamp:
- 07/06/17 10:19:37 (7 years ago)
- Location:
- stable
- Files:
-
- 5 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Algorithms.DataAnalysis
-
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/OneR.cs
r15061 r15131 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 65 66 66 67 public static IClassificationSolution CreateOneRSolution(IClassificationProblemData problemData, int minBucketSize = 6) { 68 var classValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices); 69 var model1 = FindBestDoubleVariableModel(problemData, minBucketSize); 70 var model2 = FindBestFactorModel(problemData); 71 72 if (model1 == null && model2 == null) throw new InvalidProgramException("Could not create OneR solution"); 73 else if (model1 == null) return new OneFactorClassificationSolution(model2, (IClassificationProblemData)problemData.Clone()); 74 else if (model2 == null) return new OneRClassificationSolution(model1, (IClassificationProblemData)problemData.Clone()); 75 else { 76 var model1EstimatedValues = model1.GetEstimatedClassValues(problemData.Dataset, problemData.TrainingIndices); 77 var model1NumCorrect = classValues.Zip(model1EstimatedValues, (a, b) => a.IsAlmost(b)).Count(e => e); 78 79 var model2EstimatedValues = model2.GetEstimatedClassValues(problemData.Dataset, problemData.TrainingIndices); 80 var model2NumCorrect = classValues.Zip(model2EstimatedValues, (a, b) => a.IsAlmost(b)).Count(e => e); 81 82 if (model1NumCorrect > model2NumCorrect) { 83 return new OneRClassificationSolution(model1, (IClassificationProblemData)problemData.Clone()); 84 } else { 85 return new OneFactorClassificationSolution(model2, (IClassificationProblemData)problemData.Clone()); 86 } 87 } 88 } 89 90 private static OneRClassificationModel FindBestDoubleVariableModel(IClassificationProblemData problemData, int minBucketSize = 6) { 67 91 var bestClassified = 0; 68 92 List<Split> bestSplits = null; … … 71 95 var classValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices); 72 96 73 foreach (var variable in problemData.AllowedInputVariables) { 97 var allowedInputVariables = problemData.AllowedInputVariables.Where(problemData.Dataset.VariableHasType<double>); 98 99 if (!allowedInputVariables.Any()) return null; 100 101 foreach (var variable in allowedInputVariables) { 74 102 var inputValues = problemData.Dataset.GetDoubleValues(variable, problemData.TrainingIndices); 75 103 var samples = inputValues.Zip(classValues, (i, v) => new Sample(i, v)).OrderBy(s => s.inputValue); 76 104 77 var missingValuesDistribution = samples.Where(s => double.IsNaN(s.inputValue)).GroupBy(s => s.classValue).ToDictionary(s => s.Key, s => s.Count()).MaxItems(s => s.Value).FirstOrDefault(); 105 var missingValuesDistribution = samples 106 .Where(s => double.IsNaN(s.inputValue)).GroupBy(s => s.classValue) 107 .ToDictionary(s => s.Key, s => s.Count()) 108 .MaxItems(s => s.Value) 109 .FirstOrDefault(); 78 110 79 111 //calculate class distributions for all distinct inputValues … … 120 152 while (sample.inputValue >= splits[splitIndex].thresholdValue) 121 153 splitIndex++; 122 correctClassified += sample.classValue == splits[splitIndex].classValue? 1 : 0;154 correctClassified += sample.classValue.IsAlmost(splits[splitIndex].classValue) ? 1 : 0; 123 155 } 124 156 correctClassified += missingValuesDistribution.Value; … … 134 166 //remove neighboring splits with the same class value 135 167 for (int i = 0; i < bestSplits.Count - 1; i++) { 136 if (bestSplits[i].classValue == bestSplits[i + 1].classValue) {168 if (bestSplits[i].classValue.IsAlmost(bestSplits[i + 1].classValue)) { 137 169 bestSplits.Remove(bestSplits[i]); 138 170 i--; … … 140 172 } 141 173 142 var model = new OneRClassificationModel(problemData.TargetVariable, bestVariable, bestSplits.Select(s => s.thresholdValue).ToArray(), bestSplits.Select(s => s.classValue).ToArray(), bestMissingValuesClass); 143 var solution = new OneRClassificationSolution(model, (IClassificationProblemData)problemData.Clone()); 144 145 return solution; 174 var model = new OneRClassificationModel(problemData.TargetVariable, bestVariable, 175 bestSplits.Select(s => s.thresholdValue).ToArray(), 176 bestSplits.Select(s => s.classValue).ToArray(), bestMissingValuesClass); 177 178 return model; 179 } 180 private static OneFactorClassificationModel FindBestFactorModel(IClassificationProblemData problemData) { 181 var classValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices); 182 var defaultClass = FindMostFrequentClassValue(classValues); 183 // only select string variables 184 var allowedInputVariables = problemData.AllowedInputVariables.Where(problemData.Dataset.VariableHasType<string>); 185 186 if (!allowedInputVariables.Any()) return null; 187 188 OneFactorClassificationModel bestModel = null; 189 var bestModelNumCorrect = 0; 190 191 foreach (var variable in allowedInputVariables) { 192 var variableValues = problemData.Dataset.GetStringValues(variable, problemData.TrainingIndices); 193 var groupedClassValues = variableValues 194 .Zip(classValues, (v, c) => new KeyValuePair<string, double>(v, c)) 195 .GroupBy(kvp => kvp.Key) 196 .ToDictionary(g => g.Key, g => FindMostFrequentClassValue(g.Select(kvp => kvp.Value))); 197 198 var model = new OneFactorClassificationModel(problemData.TargetVariable, variable, 199 groupedClassValues.Select(kvp => kvp.Key).ToArray(), groupedClassValues.Select(kvp => kvp.Value).ToArray(), defaultClass); 200 201 var modelEstimatedValues = model.GetEstimatedClassValues(problemData.Dataset, problemData.TrainingIndices); 202 var modelNumCorrect = classValues.Zip(modelEstimatedValues, (a, b) => a.IsAlmost(b)).Count(e => e); 203 if (modelNumCorrect > bestModelNumCorrect) { 204 bestModelNumCorrect = modelNumCorrect; 205 bestModel = model; 206 } 207 } 208 209 return bestModel; 210 } 211 212 private static double FindMostFrequentClassValue(IEnumerable<double> classValues) { 213 return classValues.GroupBy(c => c).OrderByDescending(g => g.Count()).Select(g => g.Key).First(); 146 214 } 147 215 -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/OneRClassificationModel.cs
r14186 r15131 31 31 [StorableClass] 32 32 [Item("OneR Classification Model", "A model that uses intervals for one variable to determine the class.")] 33 public class OneRClassificationModel : ClassificationModel {33 public sealed class OneRClassificationModel : ClassificationModel { 34 34 public override IEnumerable<string> VariablesUsedForPrediction { 35 35 get { return new[] { Variable }; } … … 37 37 38 38 [Storable] 39 pr otectedstring variable;39 private string variable; 40 40 public string Variable { 41 41 get { return variable; } … … 43 43 44 44 [Storable] 45 pr otecteddouble[] splits;45 private double[] splits; 46 46 public double[] Splits { 47 47 get { return splits; } … … 49 49 50 50 [Storable] 51 pr otecteddouble[] classes;51 private double[] classes; 52 52 public double[] Classes { 53 53 get { return classes; } … … 55 55 56 56 [Storable] 57 pr otecteddouble missingValuesClass;57 private double missingValuesClass; 58 58 public double MissingValuesClass { 59 59 get { return missingValuesClass; } … … 61 61 62 62 [StorableConstructor] 63 pr otectedOneRClassificationModel(bool deserializing) : base(deserializing) { }64 pr otectedOneRClassificationModel(OneRClassificationModel original, Cloner cloner)63 private OneRClassificationModel(bool deserializing) : base(deserializing) { } 64 private OneRClassificationModel(OneRClassificationModel original, Cloner cloner) 65 65 : base(original, cloner) { 66 66 this.variable = (string)original.variable; 67 67 this.splits = (double[])original.splits.Clone(); 68 68 this.classes = (double[])original.classes.Clone(); 69 this.missingValuesClass = original.missingValuesClass; 69 70 } 70 71 public override IDeepCloneable Clone(Cloner cloner) { return new OneRClassificationModel(this, cloner); } -
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/OneRClassificationSolution.cs
r14186 r15131 28 28 [StorableClass] 29 29 [Item(Name = "OneR Classification Solution", Description = "Represents a OneR classification solution which uses only a single feature with potentially multiple thresholds for class prediction.")] 30 public class OneRClassificationSolution : ClassificationSolution {30 public sealed class OneRClassificationSolution : ClassificationSolution { 31 31 public new OneRClassificationModel Model { 32 32 get { return (OneRClassificationModel)base.Model; } … … 35 35 36 36 [StorableConstructor] 37 pr otectedOneRClassificationSolution(bool deserializing) : base(deserializing) { }38 pr otectedOneRClassificationSolution(OneRClassificationSolution original, Cloner cloner) : base(original, cloner) { }37 private OneRClassificationSolution(bool deserializing) : base(deserializing) { } 38 private OneRClassificationSolution(OneRClassificationSolution original, Cloner cloner) : base(original, cloner) { } 39 39 public OneRClassificationSolution(OneRClassificationModel model, IClassificationProblemData problemData) 40 40 : base(model, problemData) {
Note: See TracChangeset
for help on using the changeset viewer.