public static void decisionTreeDemo() { try { DataSet ds = DataSetFactory.getRestaurantDataSet(); DecisionTreeLearner learner = new DecisionTreeLearner(); learner.Train(ds); System.Console.WriteLine("The Induced Decision Tree is "); System.Console.WriteLine(learner.getDecisionTree()); int[] result = learner.Test(ds); System.Console.WriteLine("\nThis Decision Tree classifies the data set with " + result[0] + " successes" + " and " + result[1] + " failures"); System.Console.WriteLine("\n"); } catch (Exception e) { System.Console.WriteLine("Decision Tree Demo Failed "); throw e; } }
public void testInducedDecisionTreeClassifiesRestaurantDataSetCorrectly() { DecisionTreeLearner learner = new DecisionTreeLearner( createInducedRestaurantDecisionTree(), "Unable to clasify"); int[] results = learner.test(DataSetFactory.getRestaurantDataSet()); Assert.AreEqual(12, results[0]); Assert.AreEqual(0, results[1]); }
public void testInducedTreeClassifiesDataSetCorrectly() { DataSet ds = DataSetFactory.getRestaurantDataSet(); DecisionTreeLearner learner = new DecisionTreeLearner(); learner.train(ds); int[] result = learner.test(ds); Assert.AreEqual(12, result[0]); Assert.AreEqual(0, result[1]); }
public void testDefaultUsedWhenTrainingDataSetHasNoExamples() { // tests RecursionBaseCase#1 DataSet ds = DataSetFactory.getRestaurantDataSet(); DecisionTreeLearner learner = new DecisionTreeLearner(); DataSet ds2 = ds.emptyDataSet(); Assert.AreEqual(0, ds2.size()); learner.train(ds2); Assert.AreEqual("Unable To Classify", learner.predict(ds .getExample(0))); }
public void testStumpPredictionForDataSet() { DataSet ds = DataSetFactory.getRestaurantDataSet(); List <String> unmatchedValues = new List <String>(); unmatchedValues.Add(NO); DecisionTree tree = DecisionTree.getStumpFor(ds, "hungry", YES, YES, unmatchedValues, "Unable to Classify"); DecisionTreeLearner learner = new DecisionTreeLearner(tree, "Unable to Classify"); int[] result = learner.test(ds); Assert.AreEqual(5, result[0]); Assert.AreEqual(7, result[1]); }
public void testClassificationReturnedWhenAllExamplesHaveTheSameClassification() { // tests RecursionBaseCase#2 DataSet ds = DataSetFactory.getRestaurantDataSet(); DecisionTreeLearner learner = new DecisionTreeLearner(); DataSet ds2 = ds.emptyDataSet(); // all 3 examples have the same classification (willWait = yes) ds2.add(ds.getExample(0)); ds2.add(ds.getExample(2)); ds2.add(ds.getExample(3)); learner.train(ds2); Assert.AreEqual("Yes", learner.predict(ds.getExample(0))); }
public void BestFirstTreeBuilder_Build_Leaf_Nodes_4() { var(observations, targets) = DataSetUtilities.LoadGlassDataSet(); var sut = new DecisionTreeLearner(new BestFirstTreeBuilder(2000, 4, observations.ColumnCount, 0.000001, 42, new OnlyUniqueThresholdsSplitSearcher(1), new GiniClassificationImpurityCalculator())); var model = new ClassificationDecisionTreeModel(sut.Learn(observations, targets)); var predictions = model.Predict(observations); var evaluator = new TotalErrorClassificationMetric <double>(); var actual = evaluator.Error(targets, predictions); Assert.AreEqual(0.37383177570093457, actual, 0.00001); }
public void testMajorityReturnedWhenAttributesToExamineIsEmpty() { // tests RecursionBaseCase#2 DataSet ds = DataSetFactory.getRestaurantDataSet(); DecisionTreeLearner learner = new DecisionTreeLearner(); DataSet ds2 = ds.emptyDataSet(); // 3 examples have classification = "yes" and one ,"no" ds2.add(ds.getExample(0)); ds2.add(ds.getExample(1));// "no" ds2.add(ds.getExample(2)); ds2.add(ds.getExample(3)); ds2.setSpecification(new MockDataSetSpecification("will_wait")); learner.train(ds2); Assert.AreEqual("Yes", learner.predict(ds.getExample(1))); }
public void BestFirstTreeBuilder_Build_Leaf_Nodes_4() { var parser = new CsvParser(() => new StringReader(Resources.Glass)); var observations = parser.EnumerateRows(v => v != "Target").ToF64Matrix(); var targets = parser.EnumerateRows("Target").ToF64Vector(); var rows = targets.Length; var sut = new DecisionTreeLearner(new BestFirstTreeBuilder(2000, 4, observations.ColumnCount, 0.000001, 42, new OnlyUniqueThresholdsSplitSearcher(1), new GiniClassificationImpurityCalculator())); var model = new ClassificationDecisionTreeModel(sut.Learn(observations, targets)); var predictions = model.Predict(observations); var evaluator = new TotalErrorClassificationMetric <double>(); var actual = evaluator.Error(targets, predictions); Assert.AreEqual(0.37383177570093457, actual, 0.00001); }
ClassificationDecisionTreeModel CreateTree(F64Matrix observations, double[] targets, int[] indices, Random random) { var learner = new DecisionTreeLearner( new DepthFirstTreeBuilder(m_maximumTreeDepth, m_featuresPrSplit, m_minimumInformationGain, m_random.Next(), new RandomSplitSearcher(m_minimumSplitSize, m_random.Next()), new GiniClasificationImpurityCalculator())); var treeIndicesLength = (int)Math.Round(m_subSampleRatio * (double)indices.Length); var treeIndices = new int[treeIndicesLength]; for (int j = 0; j < treeIndicesLength; j++) { treeIndices[j] = indices[random.Next(indices.Length)]; } var model = new ClassificationDecisionTreeModel(learner.Learn(observations, targets, treeIndices)); return(model); }