Exemplo n.º 1
0
        public void testDecisonListWithNoTestsReturnsDefaultValue()
        {
            DecisionList dlist = new DecisionList("Yes", "No");
            DataSet      ds    = DataSetFactory.getRestaurantDataSet();

            Assert.AreEqual("No", dlist.predict(ds.getExample(0)));
        }
Exemplo n.º 2
0
        static void ensembleLearningDemo()
        {
            try
            {
                DataSet ds = DataSetFactory.getRestaurantDataSet();
                ICollection <DecisionTree> stumps   = DecisionTree.getStumpsFor(ds, "Yes", "No");
                ICollection <ILearner>     learners = CollectionFactory.CreateQueue <ILearner>();

                System.Console.WriteLine("\nStump Learners vote to decide in this algorithm");
                foreach (object stump in stumps)
                {
                    DecisionTree sl           = (DecisionTree)stump;
                    StumpLearner stumpLearner = new StumpLearner(sl, "No");
                    learners.Add(stumpLearner);
                }
                AdaBoostLearner learner = new AdaBoostLearner(learners, ds);
                learner.Train(ds);
                var   answer = learner.Predict(ds.getExample(0));
                int[] result = learner.Test(ds);
                System.Console.WriteLine("\nThis Ensemble Learner  classifies the data set with "
                                         + result[0]
                                         + " successes"
                                         + " and "
                                         + result[1]
                                         + " failures");
                System.Console.WriteLine("\n");
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 3
0
        static void decisionListDemo()
        {
            try
            {
                DataSet             ds      = DataSetFactory.getRestaurantDataSet();
                DecisionListLearner learner = new DecisionListLearner("Yes", "No",
                                                                      new DecisionListTestFactory());
                learner.Train(ds);
                System.Console.WriteLine("The Induced DecisionList is");
                System.Console.WriteLine(learner.getDecisionList());
                int[] result = learner.Test(ds);

                System.Console.WriteLine("\nThis Decision List classifies the data set with "
                                         + result[0]
                                         + " successes"
                                         + " and "
                                         + result[1]
                                         + " failures");
                System.Console.WriteLine("\n");
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Decision ListDemo Failed");
                throw e;
            }
        }
Exemplo n.º 4
0
        public void testDecisionList()
        {
            DataSet       ds      = DataSetFactory.getRestaurantDataSet();
            List <DLTest> dlTests = new DLTestFactory()
                                    .createDLTestsWithAttributeCount(ds, 1);

            Assert.AreEqual(26, dlTests.Count);
        }
Exemplo n.º 5
0
        public void testStumpCreationForDataSet()
        {
            DataSet             ds = DataSetFactory.getRestaurantDataSet();
            List <DecisionTree> dt = DecisionTree.getStumpsFor(ds, YES,
                                                               "Unable to classify");

            Assert.AreEqual(26, dt.Count);
        }
Exemplo n.º 6
0
        public void testDecisionList()
        {
            DataSet ds = DataSetFactory.getRestaurantDataSet();
            ICollection <aima.net.learning.inductive.DecisionListTest> dlTests = new DecisionListTestFactory()
                                                                                 .createDLTestsWithAttributeCount(ds, 1);

            Assert.AreEqual(26, dlTests.Size());
        }
Exemplo n.º 7
0
        public void testNonDestructiveRemoveExample()
        {
            DataSet ds1 = DataSetFactory.getRestaurantDataSet();
            DataSet ds2 = ds1.removeExample(ds1.getExample(0));

            Assert.AreEqual(12, ds1.size());
            Assert.AreEqual(11, ds2.size());
        }
Exemplo n.º 8
0
        public void testDLTestMatchesEvenOnMismatchedTargetAttributeValue()
        {
            DataSet ds   = DataSetFactory.getRestaurantDataSet();
            Example e    = ds.getExample(0);
            DLTest  test = new DLTest();

            test.add("type", "French");
            Assert.IsTrue(test.matches(e));
        }
Exemplo n.º 9
0
        public void testDLTestMatchFailsOnMismatchedExample()
        {
            DataSet ds   = DataSetFactory.getRestaurantDataSet();
            Example e    = ds.getExample(0);
            DLTest  test = new DLTest();

            test.add("type", "Thai");
            Assert.IsFalse(test.matches(e));
        }
Exemplo n.º 10
0
        public void testDLTestMatchSucceedsWithMatchedExample()
        {
            DataSet ds   = DataSetFactory.getRestaurantDataSet();
            Example e    = ds.getExample(0);
            DLTest  test = new DLTest();

            test.add("type", "French");
            Assert.IsTrue(test.matches(e));
        }
Exemplo n.º 11
0
        public void testDLTestMatchFailsOnMismatchedExample()
        {
            DataSet ds = DataSetFactory.getRestaurantDataSet();
            Example e  = ds.getExample(0);

            aima.net.learning.inductive.DecisionListTest test = new aima.net.learning.inductive.DecisionListTest();
            test.add("type", "Thai");
            Assert.IsFalse(test.matches(e));
        }
Exemplo n.º 12
0
        public void testDLTestMatchSucceedsWithMatchedExample()
        {
            DataSet ds = DataSetFactory.getRestaurantDataSet();
            Example e  = ds.getExample(0);

            aima.net.learning.inductive.DecisionListTest test = new aima.net.learning.inductive.DecisionListTest();
            test.add("type", "French");
            Assert.IsTrue(test.matches(e));
        }
Exemplo n.º 13
0
        public void testGainCalculation()
        {
            DataSet ds   = DataSetFactory.getRestaurantDataSet();
            double  gain = ds.calculateGainFor("patrons");

            Assert.AreEqual(0.541, gain, 0.001);
            gain = ds.calculateGainFor("type");
            Assert.AreEqual(0.0, gain, 0.001);
        }
Exemplo n.º 14
0
        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]);
        }
Exemplo n.º 15
0
        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]);
        }
Exemplo n.º 16
0
        public void testBasicDataSetInformationCalculation()
        {
            DataSet ds = DataSetFactory.getRestaurantDataSet();
            double  infoForTargetAttribute = ds.getInformationFor();// this should

            // be the
            // generic
            // distribution
            Assert.AreEqual(1.0, infoForTargetAttribute, 0.001);
        }
Exemplo n.º 17
0
        public void testMajorityLearner()
        {
            MajorityLearner learner = new MajorityLearner();
            DataSet         ds      = DataSetFactory.getRestaurantDataSet();

            learner.train(ds);
            int[] result = learner.test(ds);
            Assert.AreEqual(6, result[0]);
            Assert.AreEqual(6, result[1]);
        }
Exemplo n.º 18
0
        public void testDLTestMatchesEvenOnMismatchedTargetAttributeValue()


        {
            DataSet ds = DataSetFactory.getRestaurantDataSet();
            Example e  = ds.getExample(0);

            aima.net.learning.inductive.DecisionListTest test = new aima.net.learning.inductive.DecisionListTest();
            test.add("type", "French");
            Assert.IsTrue(test.matches(e));
        }
Exemplo n.º 19
0
        public void testStumpCreationForSpecifiedAttributeValuePair()
        {
            DataSet       ds = DataSetFactory.getRestaurantDataSet();
            List <String> unmatchedValues = new List <String>();

            unmatchedValues.Add(NO);
            DecisionTree dt = DecisionTree.getStumpFor(ds, "alternate", YES, YES,
                                                       unmatchedValues, NO);

            Assert.IsNotNull(dt);
        }
Exemplo n.º 20
0
        public void testCurrentBestLearnerOnRestaurantDataSet()
        {
            DataSet            ds      = DataSetFactory.getRestaurantDataSet();
            CurrentBestLearner learner = new CurrentBestLearner("Yes");

            learner.train(ds);

            int[] result = learner.test(ds);
            Assert.AreEqual(12, result[0]);
            Assert.AreEqual(0, result[1]);
        }
Exemplo n.º 21
0
        public void testDecisionListLearnerReturnsFailureWhenTestsEmpty()
        {
            // tests second base case of DL Learner
            DecisionListLearner learner = new DecisionListLearner("Yes", "No",
                                                                  new MockDLTestFactory(new List <DLTest>()));
            DataSet ds = DataSetFactory.getRestaurantDataSet();

            learner.train(ds);
            Assert.AreEqual(DecisionListLearner.FAILURE, learner.predict(ds
                                                                         .getExample(0)));
        }
Exemplo n.º 22
0
        public void testDecisionListLearnerReturnsFailureWhenTestsEmpty()


        {
            // tests second base case of DL Learner
            DecisionListLearner learner = new DecisionListLearner("Yes", "No", new MockDLTestFactory(CollectionFactory.CreateQueue <aima.net.learning.inductive.DecisionListTest>()));
            DataSet             ds      = DataSetFactory.getRestaurantDataSet();

            learner.Train(ds);
            Assert.AreEqual(DecisionListLearner.FAILURE,
                            learner.Predict(ds.getExample(0)));
        }
Exemplo n.º 23
0
        public void testDecisionListTestRunOnRestaurantDataSet()
        {
            DataSet             ds      = DataSetFactory.getRestaurantDataSet();
            DecisionListLearner learner = new DecisionListLearner("Yes", "No",
                                                                  new DLTestFactory());

            learner.train(ds);

            int[] result = learner.test(ds);
            Assert.AreEqual(12, result[0]);
            Assert.AreEqual(0, result[1]);
        }
Exemplo n.º 24
0
        public void testDecisionListWithSingleTestReturnsTestValueIfTestSuccessful()
        {
            DecisionList dlist = new DecisionList("Yes", "No");
            DataSet      ds    = DataSetFactory.getRestaurantDataSet();

            DLTest test = new DLTest();

            test.add("type", "French");

            dlist.add(test, "test1success");

            Assert.AreEqual("test1success", dlist.predict(ds.getExample(0)));
        }
Exemplo n.º 25
0
        public void testDecisionListLearnerReturnsNegativeDLWhenDataSetEmpty()
        {
            // tests first base case of DL Learner
            DecisionListLearner learner = new DecisionListLearner("Yes", "No",
                                                                  new MockDLTestFactory(null));
            DataSet ds    = DataSetFactory.getRestaurantDataSet();
            DataSet empty = ds.emptyDataSet();

            learner.train(empty);
            Assert.AreEqual("No", learner.predict(ds.getExample(0)));
            Assert.AreEqual("No", learner.predict(ds.getExample(1)));
            Assert.AreEqual("No", learner.predict(ds.getExample(2)));
        }
Exemplo n.º 26
0
        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)));
        }
Exemplo n.º 27
0
        public void testDLTestReturnsMatchedAndUnmatchedExamplesCorrectly()
        {
            DataSet ds   = DataSetFactory.getRestaurantDataSet();
            DLTest  test = new DLTest();

            test.add("type", "Burger");

            DataSet matched = test.matchedExamples(ds);

            Assert.AreEqual(4, matched.size());

            DataSet unmatched = test.unmatchedExamples(ds);

            Assert.AreEqual(8, unmatched.size());
        }
Exemplo n.º 28
0
        public void testLoadsDatasetFile()
        {
            DataSet ds = DataSetFactory.getRestaurantDataSet();

            Assert.AreEqual(12, ds.size());

            Example first = ds.getExample(0);

            Assert.AreEqual(YES, first.getAttributeValueAsString("alternate"));
            Assert.AreEqual("$$$", first.getAttributeValueAsString("price"));
            Assert.AreEqual("0-10",
                            first.getAttributeValueAsString("wait_estimate"));
            Assert.AreEqual(YES, first.getAttributeValueAsString("will_wait"));
            Assert.AreEqual(YES, first.targetValue());
        }
Exemplo n.º 29
0
        public void testDataSetSplit()
        {
            DataSet ds = DataSetFactory.getRestaurantDataSet();
            Dictionary <String, DataSet> hash = ds.splitByAttribute("patrons");// this

            // should
            // be
            // the
            // generic
            // distribution
            Assert.AreEqual(3, hash.Keys.Count);
            Assert.AreEqual(6, hash["Full"].size());
            Assert.AreEqual(2, hash["None"].size());
            Assert.AreEqual(4, hash["Some"].size());
        }
Exemplo n.º 30
0
        public void testDataSetSplit()
        {
            DataSet ds = DataSetFactory.getRestaurantDataSet();
            IMap <string, DataSet> hash = ds.splitByAttribute("patrons");// this

            // should
            // be
            // the
            // generic
            // distribution
            Assert.AreEqual(3, hash.GetKeys().Size());
            Assert.AreEqual(6, hash.Get("Full").size());
            Assert.AreEqual(2, hash.Get("None").size());
            Assert.AreEqual(4, hash.Get("Some").size());
        }