Changeset 15470 for branches/M5Regression/HeuristicLab.Algorithms.DataAnalysis/3.4/M5Regression/M5Utilities/M5Analyzer.cs
- Timestamp:
- 11/10/17 12:56:36 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/M5Regression/HeuristicLab.Algorithms.DataAnalysis/3.4/M5Regression/M5Utilities/M5Analyzer.cs
r15430 r15470 29 29 namespace HeuristicLab.Algorithms.DataAnalysis { 30 30 internal static class M5Analyzer { 31 private const string LeftResultName = "Left";32 private const string RightResultName = "Right";33 31 private const string ConditionResultName = "Condition"; 34 32 private const string CoverResultName = "Covered Instances"; 35 33 private const string CoverageDiagramResultName = "Coverage"; 36 private const string NodeModelResultName = "NodeModel";37 private const string NodeSizeResultName = "NodeSize";38 34 private const string RuleModelResultName = "RuleModel"; 39 35 … … 41 37 var res = ruleSetModel.VariablesUsedForPrediction.ToDictionary(x => x, x => 0); 42 38 foreach (var rule in ruleSetModel.Rules) 43 44 39 foreach (var att in rule.SplitAtts) 40 res[att]++; 45 41 return res; 46 42 } … … 57 53 var list = new List<int>(); 58 54 GetLeafDepths(treeModel.Root, 0, list); 59 var row = new DataRow("Depths", "", list.Select(x => (double) x)) {60 VisualProperties = { ChartType = DataRowVisualProperties.DataRowChartType.Histogram}55 var row = new DataRow("Depths", "", list.Select(x => (double) x)) { 56 VisualProperties = {ChartType = DataRowVisualProperties.DataRowChartType.Histogram} 61 57 }; 62 58 var hist = new DataTable("LeafDepths"); … … 65 61 } 66 62 67 public static Result CreateRulesResult(M5RuleSetModel ruleSetModel, IRegressionProblemData pd, string resultName, int maxDepth,bool displayModels) {63 public static Result CreateRulesResult(M5RuleSetModel ruleSetModel, IRegressionProblemData pd, string resultName, bool displayModels) { 68 64 var res = new ResultCollection(); 69 65 var i = 0; 70 66 foreach (var rule in ruleSetModel.Rules) 71 res.Add(new Result("Rule" + i++, CreateRulesResult(rule, pd, maxDepth,displayModels, out pd)));67 res.Add(new Result("Rule" + i++, CreateRulesResult(rule, pd, displayModels, out pd))); 72 68 return new Result(resultName, res); 73 69 } … … 101 97 } 102 98 103 private static ResultCollection CreateRulesResult(M5NodeModel nodeModel, IRegressionProblemData pd, IList<int> rows, int maxDepth, bool displayModels) { 104 var res = new ResultCollection(); 105 if (!nodeModel.IsLeaf) { 106 res.Add(new Result(ConditionResultName, new StringValue(nodeModel.SplitAttr + " <= " + nodeModel.SplitValue))); 107 var assignment = pd.Dataset.GetDoubleValues(nodeModel.SplitAttr, rows).Select(x => x <= nodeModel.SplitValue).ToArray(); 108 var leftRows = Enumerable.Range(0, assignment.Length).Where(i => assignment[i]).Select(i => rows[i]).ToList(); 109 var rightRows = Enumerable.Range(0, assignment.Length).Where(i => !assignment[i]).Select(i => rows[i]).ToList(); 110 if (nodeModel.Left != null && maxDepth > 0) res.Add(new Result(LeftResultName, CreateRulesResult(nodeModel.Left, pd, leftRows, maxDepth - 1, displayModels))); 111 if (nodeModel.Right != null && maxDepth > 0) res.Add(new Result(RightResultName, CreateRulesResult(nodeModel.Right, pd, rightRows, maxDepth - 1, displayModels))); 112 } 113 if (nodeModel.NodeModel != null && displayModels) res.Add(new Result(NodeModelResultName, nodeModel.NodeModel.CreateRegressionSolution(pd))); 114 res.Add(new Result(NodeSizeResultName, new IntValue(rows.Count))); 115 return res; 116 } 117 118 private static ResultCollection CreateRulesResult(M5RuleModel m5RuleModel, IRegressionProblemData pd, int maxDepth, bool displayModels, out IRegressionProblemData notCovered) { 119 99 private static ResultCollection CreateRulesResult(M5RuleModel m5RuleModel, IRegressionProblemData pd, bool displayModels, out IRegressionProblemData notCovered) { 120 100 var training = pd.TrainingIndices.Where(x => !m5RuleModel.Covers(pd.Dataset, x)).ToArray(); 121 101 var test = pd.TestIndices.Where(x => !m5RuleModel.Covers(pd.Dataset, x)).ToArray(); … … 134 114 var res = new ResultCollection { 135 115 new Result(ConditionResultName, new StringValue(m5RuleModel.ToCompactString())), 136 new Result(CoverResultName, new IntValue(pd.TrainingIndices.Count() -training.Length))116 new Result(CoverResultName, new IntValue(pd.TrainingIndices.Count() - training.Length)) 137 117 }; 138 118 if (displayModels) res.Add(new Result(RuleModelResultName, m5RuleModel.CreateRegressionSolution(covered))); 139 119 return res; 140 }141 142 private static IEnumerable<double> Cumulate(this IEnumerable<double> values) {143 double sum = 0.0;144 foreach (var value in values) {145 sum += value;146 yield return sum;147 }148 120 } 149 121 … … 161 133 } 162 134 } 163
Note: See TracChangeset
for help on using the changeset viewer.