- Timestamp:
- 05/20/11 18:54:39 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest
- Files:
-
- 2 added
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs
r6240 r6241 33 33 namespace HeuristicLab.Algorithms.DataAnalysis { 34 34 /// <summary> 35 /// Represents a random forest regression model.35 /// Represents a random forest model for regression and classification 36 36 /// </summary> 37 37 [StorableClass] 38 [Item("RandomForest RegressionModel", "Represents a random forest regression model.")]39 public sealed class RandomForest RegressionModel : NamedItem, IRandomForestRegressionModel {38 [Item("RandomForestModel", "Represents a random forest for regression and classification.")] 39 public sealed class RandomForestModel : NamedItem, IRandomForestModel { 40 40 41 41 private alglib.decisionforest randomForest; 42 /// <summary>43 /// Gets or sets the SVM model.44 /// </summary>45 42 public alglib.decisionforest RandomForest { 46 43 get { return randomForest; } … … 58 55 [Storable] 59 56 private string[] allowedInputVariables; 60 57 [Storable] 58 private double[] classValues; 61 59 [StorableConstructor] 62 private RandomForest RegressionModel(bool deserializing)60 private RandomForestModel(bool deserializing) 63 61 : base(deserializing) { 64 62 if (deserializing) 65 63 randomForest = new alglib.decisionforest(); 66 64 } 67 private RandomForest RegressionModel(RandomForestRegressionModel original, Cloner cloner)65 private RandomForestModel(RandomForestModel original, Cloner cloner) 68 66 : base(original, cloner) { 69 67 randomForest = new alglib.decisionforest(); … … 75 73 targetVariable = original.targetVariable; 76 74 allowedInputVariables = (string[])original.allowedInputVariables.Clone(); 75 if (original.classValues != null) 76 this.classValues = (double[])original.classValues.Clone(); 77 77 } 78 public RandomForest RegressionModel(alglib.decisionforest randomForest, string targetVariable, IEnumerable<string> allowedInputVariables)78 public RandomForestModel(alglib.decisionforest randomForest, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null) 79 79 : base() { 80 80 this.name = ItemName; … … 83 83 this.targetVariable = targetVariable; 84 84 this.allowedInputVariables = allowedInputVariables.ToArray(); 85 if (classValues != null) 86 this.classValues = (double[])classValues.Clone(); 85 87 } 86 88 87 89 public override IDeepCloneable Clone(Cloner cloner) { 88 return new RandomForest RegressionModel(this, cloner);90 return new RandomForestModel(this, cloner); 89 91 } 90 92 … … 103 105 alglib.dfprocess(randomForest, x, ref y); 104 106 yield return y[0]; 107 } 108 } 109 110 public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) { 111 double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows); 112 113 int n = inputData.GetLength(0); 114 int columns = inputData.GetLength(1); 115 double[] x = new double[columns]; 116 double[] y = new double[randomForest.innerobj.nclasses]; 117 118 for (int row = 0; row < n; row++) { 119 for (int column = 0; column < columns; column++) { 120 x[column] = inputData[row, column]; 121 } 122 alglib.dfprocess(randomForest, x, ref y); 123 // find class for with the largest probability value 124 int maxProbClassIndex = 0; 125 double maxProb = y[0]; 126 for (int i = 1; i < y.Length; i++) { 127 if (maxProb < y[i]) { 128 maxProb = y[i]; 129 maxProbClassIndex = i; 130 } 131 } 132 yield return classValues[maxProbClassIndex]; 105 133 } 106 134 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs
r6240 r6241 87 87 Results.Add(new Result(RandomForestRegressionModelResultName, "The random forest regression solution.", solution)); 88 88 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))); 89 Results.Add(new Result("Average relative error", "The average of relative errors of the random forest regression solution on the training set.", new DoubleValue(avgRelError)));90 Results.Add(new Result("Root mean square error (out-of-bag)", "The out-of-bag root of the mean of squared errors of the random forest regression solution on the training set.", new DoubleValue(outOfBagRmsError)));91 Results.Add(new Result("Average relative error (out-of-bag)", "The out-of-bag average of relative errors of the random forest regression solution on the training set.", new DoubleValue(outOfBagAvgRelError)));89 Results.Add(new Result("Average relative error", "The average of relative errors of the random forest regression solution on the training set.", new PercentValue(avgRelError))); 90 Results.Add(new Result("Root mean square error (out-of-bag)", "The out-of-bag root of the mean of squared errors of the random forest regression solution.", new DoubleValue(outOfBagRmsError))); 91 Results.Add(new Result("Average relative error (out-of-bag)", "The out-of-bag average of relative errors of the random forest regression solution.", new PercentValue(outOfBagAvgRelError))); 92 92 } 93 93 … … 116 116 outOfBagRmsError = rep.oobrmserror; 117 117 118 return new RandomForestRegressionSolution(problemData, new RandomForest RegressionModel(dforest, targetVariable, allowedInputVariables));118 return new RandomForestRegressionSolution(problemData, new RandomForestModel(dforest, targetVariable, allowedInputVariables)); 119 119 } 120 120 #endregion -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegressionSolution.cs
r6240 r6241 37 37 public sealed class RandomForestRegressionSolution : RegressionSolution, IRandomForestRegressionSolution { 38 38 39 public new IRandomForest RegressionModel Model {40 get { return (IRandomForest RegressionModel)base.Model; }39 public new IRandomForestModel Model { 40 get { return (IRandomForestModel)base.Model; } 41 41 set { base.Model = value; } 42 42 } … … 47 47 : base(original, cloner) { 48 48 } 49 public RandomForestRegressionSolution(IRegressionProblemData problemData, IRandomForest RegressionModel randomForestModel)49 public RandomForestRegressionSolution(IRegressionProblemData problemData, IRandomForestModel randomForestModel) 50 50 : base(randomForestModel, problemData) { 51 51 }
Note: See TracChangeset
for help on using the changeset viewer.