Changeset 17246 for branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs
- Timestamp:
- 09/11/19 14:06:25 (5 years ago)
- Location:
- branches/2925_AutoDiffForDynamicalModels
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2925_AutoDiffForDynamicalModels
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4
- Property svn:mergeinfo changed
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs
r16662 r17246 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2019Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 20 20 #endregion 21 21 22 using System.Collections.Generic; 23 using System.Linq; 22 24 using System.Threading; 25 using HEAL.Attic; 26 using HeuristicLab.Algorithms.DataAnalysis.RandomForest; 23 27 using HeuristicLab.Common; 24 28 using HeuristicLab.Core; … … 26 30 using HeuristicLab.Optimization; 27 31 using HeuristicLab.Parameters; 28 using HEAL.Attic;29 32 using HeuristicLab.Problems.DataAnalysis; 30 33 … … 43 46 private const string SeedParameterName = "Seed"; 44 47 private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; 45 private const string CreateSolutionParameterName = "CreateSolution";48 private const string ModelCreationParameterName = "ModelCreation"; 46 49 47 50 #region parameter properties … … 61 64 get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; } 62 65 } 63 p ublic IFixedValueParameter<BoolValue> CreateSolutionParameter {64 get { return (IFixedValueParameter< BoolValue>)Parameters[CreateSolutionParameterName]; }66 private IFixedValueParameter<EnumValue<ModelCreation>> ModelCreationParameter { 67 get { return (IFixedValueParameter<EnumValue<ModelCreation>>)Parameters[ModelCreationParameterName]; } 65 68 } 66 69 #endregion … … 86 89 set { SetSeedRandomlyParameter.Value.Value = value; } 87 90 } 88 public bool CreateSolution {89 get { return CreateSolutionParameter.Value.Value; }90 set { CreateSolutionParameter.Value.Value = value; }91 public ModelCreation ModelCreation { 92 get { return ModelCreationParameter.Value.Value; } 93 set { ModelCreationParameter.Value.Value = value; } 91 94 } 92 95 #endregion … … 105 108 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 106 109 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 107 Parameters.Add(new FixedValueParameter< BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));108 Parameters[ CreateSolutionParameterName].Hidden = true;110 Parameters.Add(new FixedValueParameter<EnumValue<ModelCreation>>(ModelCreationParameterName, "Defines the results produced at the end of the run (Surrogate => Less disk space, lazy recalculation of model)", new EnumValue<ModelCreation>(ModelCreation.Model))); 111 Parameters[ModelCreationParameterName].Hidden = true; 109 112 110 113 Problem = new ClassificationProblem(); … … 121 124 if (!Parameters.ContainsKey((SetSeedRandomlyParameterName))) 122 125 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 123 if (!Parameters.ContainsKey(CreateSolutionParameterName)) { 124 Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true))); 125 Parameters[CreateSolutionParameterName].Hidden = true; 126 127 // parameter type has been changed 128 if (Parameters.ContainsKey("CreateSolution")) { 129 var createSolutionParam = Parameters["CreateSolution"] as FixedValueParameter<BoolValue>; 130 Parameters.Remove(createSolutionParam); 131 132 ModelCreation value = createSolutionParam.Value.Value ? ModelCreation.Model : ModelCreation.QualityOnly; 133 Parameters.Add(new FixedValueParameter<EnumValue<ModelCreation>>(ModelCreationParameterName, "Defines the results produced at the end of the run (Surrogate => Less disk space, lazy recalculation of model)", new EnumValue<ModelCreation>(value))); 134 Parameters[ModelCreationParameterName].Hidden = true; 135 } else if (!Parameters.ContainsKey(ModelCreationParameterName)) { 136 // very old version contains neither ModelCreationParameter nor CreateSolutionParameter 137 Parameters.Add(new FixedValueParameter<EnumValue<ModelCreation>>(ModelCreationParameterName, "Defines the results produced at the end of the run (Surrogate => Less disk space, lazy recalculation of model)", new EnumValue<ModelCreation>(ModelCreation.Model))); 138 Parameters[ModelCreationParameterName].Hidden = true; 126 139 } 127 140 #endregion … … 138 151 139 152 var model = CreateRandomForestClassificationModel(Problem.ProblemData, NumberOfTrees, R, M, Seed, out rmsError, out relClassificationError, out outOfBagRmsError, out outOfBagRelClassificationError); 153 140 154 Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the random forest regression solution on the training set.", new DoubleValue(rmsError))); 141 155 Results.Add(new Result("Relative classification error", "Relative classification error of the random forest regression solution on the training set.", new PercentValue(relClassificationError))); … … 143 157 Results.Add(new Result("Relative classification error (out-of-bag)", "The out-of-bag relative classification error of the random forest regression solution.", new PercentValue(outOfBagRelClassificationError))); 144 158 145 if (CreateSolution) { 146 var solution = new RandomForestClassificationSolution(model, (IClassificationProblemData)Problem.ProblemData.Clone()); 159 160 IClassificationSolution solution = null; 161 if (ModelCreation == ModelCreation.Model) { 162 solution = model.CreateClassificationSolution(Problem.ProblemData); 163 } else if (ModelCreation == ModelCreation.SurrogateModel) { 164 var problemData = Problem.ProblemData; 165 var surrogateModel = new RandomForestModelSurrogate(model, problemData.TargetVariable, problemData, Seed, NumberOfTrees, R, M, problemData.ClassValues.ToArray()); 166 167 solution = surrogateModel.CreateClassificationSolution(problemData); 168 } 169 170 if (solution != null) { 147 171 Results.Add(new Result(RandomForestClassificationModelResultName, "The random forest classification solution.", solution)); 148 172 } … … 157 181 } 158 182 159 public static RandomForestModel CreateRandomForestClassificationModel(IClassificationProblemData problemData, int nTrees, double r, double m, int seed, 183 public static RandomForestModelFull CreateRandomForestClassificationModel(IClassificationProblemData problemData, int nTrees, double r, double m, int seed, 184 out double rmsError, out double avgRelError, out double outOfBagRmsError, out double outOfBagAvgRelError) { 185 var model = CreateRandomForestClassificationModel(problemData, problemData.TrainingIndices, nTrees, r, m, seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError); 186 return model; 187 } 188 189 public static RandomForestModelFull CreateRandomForestClassificationModel(IClassificationProblemData problemData, IEnumerable<int> trainingIndices, int nTrees, double r, double m, int seed, 160 190 out double rmsError, out double relClassificationError, out double outOfBagRmsError, out double outOfBagRelClassificationError) { 161 return RandomForestModel.CreateClassificationModel(problemData, nTrees, r, m, seed, 162 rmsError: out rmsError, relClassificationError: out relClassificationError, outOfBagRmsError: out outOfBagRmsError, outOfBagRelClassificationError: out outOfBagRelClassificationError); 191 192 var variables = problemData.AllowedInputVariables.Concat(new string[] { problemData.TargetVariable }); 193 double[,] inputMatrix = problemData.Dataset.ToArray(variables, trainingIndices); 194 195 var classValues = problemData.ClassValues.ToArray(); 196 int nClasses = classValues.Length; 197 198 // map original class values to values [0..nClasses-1] 199 var classIndices = new Dictionary<double, double>(); 200 for (int i = 0; i < nClasses; i++) { 201 classIndices[classValues[i]] = i; 202 } 203 204 int nRows = inputMatrix.GetLength(0); 205 int nColumns = inputMatrix.GetLength(1); 206 for (int row = 0; row < nRows; row++) { 207 inputMatrix[row, nColumns - 1] = classIndices[inputMatrix[row, nColumns - 1]]; 208 } 209 210 alglib.dfreport rep; 211 var dForest = RandomForestUtil.CreateRandomForestModel(seed, inputMatrix, nTrees, r, m, nClasses, out rep); 212 213 rmsError = rep.rmserror; 214 outOfBagRmsError = rep.oobrmserror; 215 relClassificationError = rep.relclserror; 216 outOfBagRelClassificationError = rep.oobrelclserror; 217 218 return new RandomForestModelFull(dForest, problemData.TargetVariable, problemData.AllowedInputVariables, classValues); 163 219 } 164 220 #endregion
Note: See TracChangeset
for help on using the changeset viewer.