Changeset 15127 for stable/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs
- Timestamp:
- 07/06/17 09:22:48 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
stable/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs
r14822 r15127 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 using HeuristicLab.Problems.DataAnalysis; 30 using HeuristicLab.Problems.DataAnalysis.Symbolic; 29 31 30 32 namespace HeuristicLab.Algorithms.DataAnalysis { … … 49 51 } 50 52 53 public int NumberOfTrees { 54 get { return nTrees; } 55 } 51 56 52 57 // instead of storing the data of the model itself … … 64 69 [Storable] 65 70 private double m; 66 67 71 68 72 [StorableConstructor] … … 197 201 } 198 202 203 public ISymbolicExpressionTree ExtractTree(int treeIdx) { 204 var rf = RandomForest; 205 // hoping that the internal representation of alglib is stable 206 207 // TREE FORMAT 208 // W[Offs] - size of sub-array (for the tree) 209 // node info: 210 // W[K+0] - variable number (-1 for leaf mode) 211 // W[K+1] - threshold (class/value for leaf node) 212 // W[K+2] - ">=" branch index (absent for leaf node) 213 214 // skip irrelevant trees 215 int offset = 0; 216 for (int i = 0; i < treeIdx - 1; i++) { 217 offset = offset + (int)Math.Round(rf.innerobj.trees[offset]); 218 } 219 220 var constSy = new Constant(); 221 var varCondSy = new VariableCondition() { IgnoreSlope = true }; 222 223 var node = CreateRegressionTreeRec(rf.innerobj.trees, offset, offset + 1, constSy, varCondSy); 224 225 var startNode = new StartSymbol().CreateTreeNode(); 226 startNode.AddSubtree(node); 227 var root = new ProgramRootSymbol().CreateTreeNode(); 228 root.AddSubtree(startNode); 229 return new SymbolicExpressionTree(root); 230 } 231 232 private ISymbolicExpressionTreeNode CreateRegressionTreeRec(double[] trees, int offset, int k, Constant constSy, VariableCondition varCondSy) { 233 234 // alglib source for evaluation of one tree (dfprocessinternal) 235 // offs = 0 236 // 237 // Set pointer to the root 238 // 239 // k = offs + 1; 240 // 241 // // 242 // // Navigate through the tree 243 // // 244 // while (true) { 245 // if ((double)(df.trees[k]) == (double)(-1)) { 246 // if (df.nclasses == 1) { 247 // y[0] = y[0] + df.trees[k + 1]; 248 // } else { 249 // idx = (int)Math.Round(df.trees[k + 1]); 250 // y[idx] = y[idx] + 1; 251 // } 252 // break; 253 // } 254 // if ((double)(x[(int)Math.Round(df.trees[k])]) < (double)(df.trees[k + 1])) { 255 // k = k + innernodewidth; 256 // } else { 257 // k = offs + (int)Math.Round(df.trees[k + 2]); 258 // } 259 // } 260 261 if ((double)(trees[k]) == (double)(-1)) { 262 var constNode = (ConstantTreeNode)constSy.CreateTreeNode(); 263 constNode.Value = trees[k + 1]; 264 return constNode; 265 } else { 266 var condNode = (VariableConditionTreeNode)varCondSy.CreateTreeNode(); 267 condNode.VariableName = AllowedInputVariables[(int)Math.Round(trees[k])]; 268 condNode.Threshold = trees[k + 1]; 269 condNode.Slope = double.PositiveInfinity; 270 271 var left = CreateRegressionTreeRec(trees, offset, k + 3, constSy, varCondSy); 272 var right = CreateRegressionTreeRec(trees, offset, offset + (int)Math.Round(trees[k + 2]), constSy, varCondSy); 273 274 condNode.AddSubtree(left); // not 100% correct because interpreter uses: if(x <= thres) left() else right() and RF uses if(x < thres) left() else right() (see above) 275 condNode.AddSubtree(right); 276 return condNode; 277 } 278 } 279 199 280 200 281 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
Note: See TracChangeset
for help on using the changeset viewer.