Changeset 14345 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis
- Timestamp:
- 10/21/16 17:59:36 (8 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesAlgorithm.cs
r14185 r14345 269 269 } else { 270 270 // otherwise we produce a regression solution 271 Results.Add(new Result("Solution", new RegressionSolution(model, problemData)));271 Results.Add(new Result("Solution", new GradientBoostedTreesSolution(model, problemData))); 272 272 } 273 273 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesSolution.cs
r14185 r14345 20 20 #endregion 21 21 22 using System.Collections.Generic;23 using System.Linq;24 22 using HeuristicLab.Common; 25 23 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/RegressionTreeModel.cs
r14185 r14345 28 28 using HeuristicLab.Common; 29 29 using HeuristicLab.Core; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 32 using HeuristicLab.Problems.DataAnalysis; 33 using HeuristicLab.Problems.DataAnalysis.Symbolic; 34 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 32 35 33 36 namespace HeuristicLab.Algorithms.DataAnalysis { … … 210 213 } 211 214 215 /// <summary> 216 /// Transforms the tree model to a symbolic regression solution 217 /// </summary> 218 /// <param name="problemData"></param> 219 /// <returns>A new symbolic regression solution which matches the tree model</returns> 220 public ISymbolicRegressionSolution CreateSymbolicRegressionSolution(IRegressionProblemData problemData) { 221 var rootSy = new ProgramRootSymbol(); 222 var startSy = new StartSymbol(); 223 var varCondSy = new VariableCondition() { IgnoreSlope = true }; 224 var constSy = new Constant(); 225 226 var startNode = startSy.CreateTreeNode(); 227 startNode.AddSubtree(CreateSymbolicRegressionTreeRecursive(tree, 0, varCondSy, constSy)); 228 var rootNode = rootSy.CreateTreeNode(); 229 rootNode.AddSubtree(startNode); 230 var model = new SymbolicRegressionModel(TargetVariable, new SymbolicExpressionTree(rootNode), new SymbolicDataAnalysisExpressionTreeLinearInterpreter()); 231 return model.CreateRegressionSolution(problemData); 232 } 233 234 private ISymbolicExpressionTreeNode CreateSymbolicRegressionTreeRecursive(TreeNode[] treeNodes, int nodeIdx, VariableCondition varCondSy, Constant constSy) { 235 var curNode = treeNodes[nodeIdx]; 236 if (curNode.VarName == TreeNode.NO_VARIABLE) { 237 var node = (ConstantTreeNode)constSy.CreateTreeNode(); 238 node.Value = curNode.Val; 239 return node; 240 } else { 241 var node = (VariableConditionTreeNode)varCondSy.CreateTreeNode(); 242 node.VariableName = curNode.VarName; 243 node.Threshold = curNode.Val; 244 245 var left = CreateSymbolicRegressionTreeRecursive(treeNodes, curNode.LeftIdx, varCondSy, constSy); 246 var right = CreateSymbolicRegressionTreeRecursive(treeNodes, curNode.RightIdx, varCondSy, constSy); 247 node.AddSubtree(left); 248 node.AddSubtree(right); 249 return node; 250 } 251 } 252 253 212 254 private string TreeToString(int idx, string part) { 213 255 var n = tree[idx]; -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IRandomForestClassificationSolution.cs
r14185 r14345 30 30 public interface IRandomForestClassificationSolution : IClassificationSolution { 31 31 new IRandomForestModel Model { get; } 32 int NumberOfTrees { get; } 32 33 } 33 34 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IRandomForestModel.cs
r14185 r14345 20 20 #endregion 21 21 22 using HeuristicLab. Optimization;22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 23 23 using HeuristicLab.Problems.DataAnalysis; 24 using HeuristicLab.Core; 25 using System.Collections.Generic; 24 26 25 27 26 namespace HeuristicLab.Algorithms.DataAnalysis { … … 30 29 /// </summary> 31 30 public interface IRandomForestModel : IConfidenceRegressionModel, IClassificationModel { 31 int NumberOfTrees { get; } 32 ISymbolicExpressionTree ExtractTree(int treeIdx); // returns a specific tree from the random forest as a ISymbolicRegressionModel 32 33 } 33 34 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IRandomForestRegressionSolution.cs
r14185 r14345 20 20 #endregion 21 21 22 using HeuristicLab.Optimization;23 22 using HeuristicLab.Problems.DataAnalysis; 24 using HeuristicLab. Core;23 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 25 24 26 25 namespace HeuristicLab.Algorithms.DataAnalysis { … … 30 29 public interface IRandomForestRegressionSolution : IConfidenceRegressionSolution { 31 30 new IRandomForestModel Model { get; } 31 int NumberOfTrees { get; } 32 32 } 33 33 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassificationSolution.cs
r14185 r14345 38 38 } 39 39 40 public int NumberOfTrees { 41 get { return Model.NumberOfTrees; } 42 } 43 40 44 [StorableConstructor] 41 45 private RandomForestClassificationSolution(bool deserializing) : base(deserializing) { } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs
r14230 r14345 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 // hoping that the internal representation of alglib is stable 205 206 // TREE FORMAT 207 // W[Offs] - size of sub-array (for the tree) 208 // node info: 209 // W[K+0] - variable number (-1 for leaf mode) 210 // W[K+1] - threshold (class/value for leaf node) 211 // W[K+2] - ">=" branch index (absent for leaf node) 212 213 // skip irrelevant trees 214 int offset = 0; 215 for (int i = 0; i < treeIdx - 1; i++) { 216 offset = offset + (int)Math.Round(randomForest.innerobj.trees[offset]); 217 } 218 219 var constSy = new Constant(); 220 var varCondSy = new VariableCondition() { IgnoreSlope = true }; 221 222 var node = CreateRegressionTreeRec(randomForest.innerobj.trees, offset, offset + 1, constSy, varCondSy); 223 224 var startNode = new StartSymbol().CreateTreeNode(); 225 startNode.AddSubtree(node); 226 var root = new ProgramRootSymbol().CreateTreeNode(); 227 root.AddSubtree(startNode); 228 return new SymbolicExpressionTree(root); 229 } 230 231 private ISymbolicExpressionTreeNode CreateRegressionTreeRec(double[] trees, int offset, int k, Constant constSy, VariableCondition varCondSy) { 232 233 // alglib source for evaluation of one tree (dfprocessinternal) 234 // offs = 0 235 // 236 // Set pointer to the root 237 // 238 // k = offs + 1; 239 // 240 // // 241 // // Navigate through the tree 242 // // 243 // while (true) { 244 // if ((double)(df.trees[k]) == (double)(-1)) { 245 // if (df.nclasses == 1) { 246 // y[0] = y[0] + df.trees[k + 1]; 247 // } else { 248 // idx = (int)Math.Round(df.trees[k + 1]); 249 // y[idx] = y[idx] + 1; 250 // } 251 // break; 252 // } 253 // if ((double)(x[(int)Math.Round(df.trees[k])]) < (double)(df.trees[k + 1])) { 254 // k = k + innernodewidth; 255 // } else { 256 // k = offs + (int)Math.Round(df.trees[k + 2]); 257 // } 258 // } 259 260 if ((double)(trees[k]) == (double)(-1)) { 261 var constNode = (ConstantTreeNode)constSy.CreateTreeNode(); 262 constNode.Value = trees[k + 1]; 263 return constNode; 264 } else { 265 var condNode = (VariableConditionTreeNode)varCondSy.CreateTreeNode(); 266 condNode.VariableName = AllowedInputVariables[(int)Math.Round(trees[k])]; 267 condNode.Threshold = trees[k + 1]; 268 condNode.Slope = double.PositiveInfinity; 269 270 var left = CreateRegressionTreeRec(trees, offset, k + 3, constSy, varCondSy); 271 var right = CreateRegressionTreeRec(trees, offset, offset + (int)Math.Round(trees[k + 2]), constSy, varCondSy); 272 273 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) 274 condNode.AddSubtree(right); 275 return condNode; 276 } 277 } 278 199 279 200 280 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegressionSolution.cs
r14185 r14345 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 26 using HeuristicLab.Problems.DataAnalysis; 27 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 26 28 27 29 namespace HeuristicLab.Algorithms.DataAnalysis { … … 36 38 get { return (IRandomForestModel)base.Model; } 37 39 set { base.Model = value; } 40 } 41 42 public int NumberOfTrees { 43 get { return Model.NumberOfTrees; } 38 44 } 39 45
Note: See TracChangeset
for help on using the changeset viewer.