Changeset 18027 for branches/3026_IntegrationIntoSymSpace/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelFull.cs
- Timestamp:
- 07/20/21 18:13:55 (3 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Algorithms.DataAnalysis merged: 17931,17934,17942,17979
- Property svn:mergeinfo changed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Algorithms.DataAnalysis/3.4
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Algorithms.DataAnalysis/3.4 merged: 17931,17934,17942,17979
- Property svn:mergeinfo changed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelFull.cs
r17180 r18027 31 31 32 32 namespace HeuristicLab.Algorithms.DataAnalysis { 33 [StorableType(" 9C797DF0-1169-4381-A732-6DAB90802839")]33 [StorableType("55412E08-DAD4-4C2E-9181-C142E7EA9474")] 34 34 [Item("RandomForestModelFull", "Represents a random forest for regression and classification.")] 35 35 public sealed class RandomForestModelFull : ClassificationModel, IRandomForestModel { … … 42 42 private double[] classValues; 43 43 44 public int NumClasses => classValues == null ? 0 : classValues.Length; 45 44 46 [Storable] 45 47 private string[] inputVariables; 46 48 49 [Storable] 47 50 public int NumberOfTrees { 48 get { return RandomForestNTrees; }51 get; private set; 49 52 } 50 53 … … 53 56 54 57 [Storable] 55 private int RandomForestBufSize { 56 get { return randomForest.innerobj.bufsize; } 57 set { randomForest.innerobj.bufsize = value; } 58 } 59 [Storable] 60 private int RandomForestNClasses { 61 get { return randomForest.innerobj.nclasses; } 62 set { randomForest.innerobj.nclasses = value; } 63 } 64 [Storable] 65 private int RandomForestNTrees { 66 get { return randomForest.innerobj.ntrees; } 67 set { randomForest.innerobj.ntrees = value; } 68 } 69 [Storable] 70 private int RandomForestNVars { 71 get { return randomForest.innerobj.nvars; } 72 set { randomForest.innerobj.nvars = value; } 73 } 74 [Storable] 75 private double[] RandomForestTrees { 76 get { return randomForest.innerobj.trees; } 77 set { randomForest.innerobj.trees = value; } 58 private string RandomForestSerialized { 59 get { alglib.dfserialize(randomForest, out var serialized); return serialized; } 60 set { if (value != null) alglib.dfunserialize(value, out randomForest); } 78 61 } 79 62 80 63 [StorableConstructor] 81 private RandomForestModelFull(StorableConstructorFlag _) : base(_) { 82 randomForest = new alglib.decisionforest(); 83 } 64 private RandomForestModelFull(StorableConstructorFlag _) : base(_) { } 84 65 85 66 private RandomForestModelFull(RandomForestModelFull original, Cloner cloner) : base(original, cloner) { 86 randomForest = new alglib.decisionforest(); 87 randomForest.innerobj.bufsize = original.randomForest.innerobj.bufsize; 88 randomForest.innerobj.nclasses = original.randomForest.innerobj.nclasses; 89 randomForest.innerobj.ntrees = original.randomForest.innerobj.ntrees; 90 randomForest.innerobj.nvars = original.randomForest.innerobj.nvars; 91 randomForest.innerobj.trees = (double[])original.randomForest.innerobj.trees.Clone(); 67 if (original.randomForest != null) 68 randomForest = (alglib.decisionforest)original.randomForest.make_copy(); 69 NumberOfTrees = original.NumberOfTrees; 92 70 93 71 // following fields are immutable so we don't need to clone them … … 99 77 } 100 78 101 public RandomForestModelFull(alglib.decisionforest decisionForest, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<double> classValues = null) : base(targetVariable) {79 public RandomForestModelFull(alglib.decisionforest decisionForest, int nTrees, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<double> classValues = null) : base(targetVariable) { 102 80 this.name = ItemName; 103 81 this.description = ItemDescription; 104 82 105 randomForest = decisionForest; 83 randomForest = (alglib.decisionforest)decisionForest.make_copy(); 84 NumberOfTrees = nTrees; 106 85 107 86 this.inputVariables = inputVariables.ToArray(); … … 147 126 double[] y = new double[1]; 148 127 128 alglib.dfcreatebuffer(randomForest, out var buf); 149 129 for (int row = 0; row < n; row++) { 150 130 for (int column = 0; column < columns; column++) { 151 131 x[column] = inputData[row, column]; 152 132 } 153 alglib.df process(randomForest, x, ref y);133 alglib.dftsprocess(randomForest, buf, x, ref y); // thread-safe process (as long as separate buffers are used) 154 134 yield return y[0]; 155 135 } … … 168 148 x[column] = inputData[row, column]; 169 149 } 170 alglib.dforest.dfprocessraw(randomForest.innerobj, x, ref ys); 150 lock (randomForest) 151 alglib.dforest.dfprocessraw(randomForest.innerobj, x, ref ys, null); 171 152 yield return ys.VariancePop(); 172 153 } … … 180 161 int columns = inputData.GetLength(1); 181 162 double[] x = new double[columns]; 182 double[] y = new double[randomForest.innerobj.nclasses]; 183 163 double[] y = new double[NumClasses]; 164 165 alglib.dfcreatebuffer(randomForest, out var buf); 184 166 for (int row = 0; row < n; row++) { 185 167 for (int column = 0; column < columns; column++) { 186 168 x[column] = inputData[row, column]; 187 169 } 188 alglib.df process(randomForest, x, ref y);170 alglib.dftsprocess(randomForest, buf, x, ref y); 189 171 // find class for with the largest probability value 190 172 int maxProbClassIndex = 0;
Note: See TracChangeset
for help on using the changeset viewer.