Пример #1
0
        public void sample_ratio_less_than_1()
        {
            // https://github.com/accord-net/framework/issues/576

            string localPath = Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, "gh576");

            Accord.Math.Random.Generator.Seed = 1;

            var nursery = new DataSets.Nursery(localPath);

            int[][] inputs  = nursery.Instances;
            int[]   outputs = nursery.ClassLabels;

            var teacher = new RandomForestLearning(nursery.VariableNames)
            {
                NumberOfTrees = 1,
                SampleRatio   = 0.5
            };

            teacher.ParallelOptions.MaxDegreeOfParallelism = 1;

            var forest = teacher.Learn(inputs, outputs);

            forest.ParallelOptions.MaxDegreeOfParallelism = 1;

            int[] predicted = forest.Decide(inputs);

            double error = new ZeroOneLoss(outputs).Loss(forest.Decide(inputs));

            Assert.AreEqual(0.0023148148148148147d, error, 1e-10);
        }
        public void LargeRunTest()
        {
            string localPath = Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, "rf");

            #region doc_nursery
            // Fix random seed for reproducibility
            Accord.Math.Random.Generator.Seed = 1;

            // This example uses the Nursery Database available from the University of
            // California Irvine repository of machine learning databases, available at
            //
            //   http://archive.ics.uci.edu/ml/machine-learning-databases/nursery/nursery.names
            //
            // The description paragraph is listed as follows.
            //
            //   Nursery Database was derived from a hierarchical decision model
            //   originally developed to rank applications for nursery schools. It
            //   was used during several years in 1980's when there was excessive
            //   enrollment to these schools in Ljubljana, Slovenia, and the
            //   rejected applications frequently needed an objective
            //   explanation. The final decision depended on three subproblems:
            //   occupation of parents and child's nursery, family structure and
            //   financial standing, and social and health picture of the family.
            //   The model was developed within expert system shell for decision
            //   making DEX (M. Bohanec, V. Rajkovic: Expert system for decision
            //   making. Sistemica 1(1), pp. 145-157, 1990.).
            //

            // Let's begin by loading the raw data. This string variable contains
            // the contents of the nursery.data file as a single, continuous text.
            //
            var     nursery = new DataSets.Nursery(path: localPath);
            int[][] inputs  = nursery.Instances;
            int[]   outputs = nursery.ClassLabels;

            // Now, let's create the forest learning algorithm
            var teacher = new RandomForestLearning(nursery.VariableNames)
            {
                NumberOfTrees = 1,
                SampleRatio   = 1.0
            };

            // Finally, learn a random forest from data
            var forest = teacher.Learn(inputs, outputs);

            // We can estimate class labels using
            int[] predicted = forest.Decide(inputs);

            // And the classification error (0) can be computed as
            double error = new ZeroOneLoss(outputs).Loss(forest.Decide(inputs));
            #endregion

            Assert.AreEqual(0, error, 1e-10);
            Assert.IsTrue(outputs.IsEqual(predicted));

            Assert.AreEqual(0, error);

            for (int i = 0; i < inputs.Length; i++)
            {
                int expected = outputs[i];
                int actual   = forest.Compute(inputs[i].ToDouble());

                Assert.AreEqual(expected, actual);
            }
        }