/// <summary> /// Initializes a new instance of the <see cref="MyRegressionTree"/> class. /// </summary> /// <param name="exampleData">The example data.</param> public MyRegressionTree(ITreeExampleData exampleData) { this.existingFeatures = new List <string>(); this.leaves = new List <Node>(); // For every row in the example data foreach (var exampleRow in exampleData.ExampleRows) { foreach (var example in exampleRow.Items) { // only get as many possible Features as are defined in the dimensions if (this.existingFeatures.Count >= exampleData.Dimensions) { break; } // if feature is not in the list -> add it if (!this.existingFeatures.Contains(example.RelatedFeature.Name)) { this.existingFeatures.Add(example.RelatedFeature.Name); } } } // Init the rootnode as concrete node this.rootNode = new RegressionTreeNode(this, exampleData.ExampleRows, null); }
/// <summary> /// Gets the predicition accuracy. /// </summary> /// <param name="testSet">The test set.</param> /// <returns> /// The calculated prediction accuracy. /// </returns> public override double GetPredicitionAccuracy(ITreeExampleData testSet) { double testExampleCount = testSet.ExampleRows.Count; double deviationCount = 0; foreach (var example in testSet.ExampleRows) { if (example.Classification.ClassifiedAs != example.Class) { deviationCount++; } } return(1 - deviationCount / testExampleCount); }
/// <summary> /// Prunes the specified mytree. /// </summary> /// <param name="mytree">The mytree.</param> /// <param name="testSet">The test set.</param> /// <returns></returns> public Tree Prune(Tree mytree, ITreeExampleData testSet) { // At the beginning -> Validate Set // Then calculate some kind of prediction accuracy factor bool changed = false; do { tree.ValidateTestSet(testSet); double startAccuracy = this.GetPredicitionAccuracy(testSet); changed = false; foreach (Node leaf in mytree.GetLeaves()) { if (leaf.Parent != null) { Node leftTemp = leaf.Parent.LeftNode; Node rightTemp = leaf.Parent.RightNode; leaf.Parent.LeftNode = null; leaf.Parent.RightNode = null; mytree.ValidateTestSet(testSet); double tempAccuracy = this.GetPredicitionAccuracy(testSet); if (tempAccuracy < startAccuracy) { // reverse the change leaf.Parent.LeftNode = leftTemp; leaf.Parent.RightNode = rightTemp; } else { // leave it as it is and continue leaf.Parent.OutgoingSplit = null; changed = true; } } } } while (changed); return(mytree); }
/// <summary> /// Splits the specified tree. /// </summary> /// <param name="tree">The tree.</param> /// <param name="gardener">The gardener.</param> /// <param name="testdata">The testdata.</param> private void Split(DecisionTree.Implementation.Tree tree, IGardener gardener, ITreeExampleData testdata) { tree.Split(); if (this.PruneBox.IsChecked == true) { gardener.Prune(tree, testdata); } DecisionTreeWPFRenderer renderer = new DecisionTreeWPFRenderer(tree, this.TreeCanvas); renderer.Visualize(); }
/// <summary> /// Gets the predicition accuracy. /// </summary> /// <param name="testSet">The test set.</param> /// <returns>The calculated prediction accuracy.</returns> public abstract double GetPredicitionAccuracy(ITreeExampleData testSet);