A decision tree generator.
상속: Generator
예제 #1
0
        public void ArbitraryPrediction_Test_With_Named_Iterator()
        {
            var data = ArbitraryPrediction.GetDataUsingNamedIterator();
            var description = Descriptor.Create<ArbitraryPrediction>();
            var generator = new DecisionTreeGenerator(50);
            var model = generator.Generate(description, data);

            ArbitraryPrediction minimumPredictionValue = new ArbitraryPrediction
            {
                FirstTestFeature = 1.0m,
                SecondTestFeature = 10.0m,
                ThirdTestFeature = 1.2m
            };

            ArbitraryPrediction maximumPredictionValue = new ArbitraryPrediction
            {
                FirstTestFeature = 1.0m,
                SecondTestFeature = 57.0m,
                ThirdTestFeature = 1.2m
            };

            var expectedMinimum = model.Predict<ArbitraryPrediction>(minimumPredictionValue).OutcomeLabel;
            var expectedMaximum = model.Predict<ArbitraryPrediction>(maximumPredictionValue).OutcomeLabel;

            Assert.AreEqual(ArbitraryPrediction.PredictionLabel.Minimum, expectedMinimum);
            Assert.AreEqual(ArbitraryPrediction.PredictionLabel.Maximum, expectedMaximum);
        }
        public void Save_And_Load_Iris_DT()
        {
            var data = Iris.Load();
            var description = Descriptor.Create<Iris>();
            var generator = new DecisionTreeGenerator(50);
            var model = generator.Generate(description, data) as DecisionTreeModel;

            Serialize(model);
            var lmodel = Deserialize<DecisionTreeModel>();
            Assert.AreEqual(model.Hint, lmodel.Hint);
            AreEqual(model.Tree, lmodel.Tree, false);
        }
        public void Save_And_Load_HouseDT()
        {
            var data = House.GetData();

            var description = Descriptor.Create<House>();
            var generator = new DecisionTreeGenerator { Depth = 50 };
            var model = generator.Generate(description, data) as DecisionTreeModel;

            Serialize(model);

            var lmodel = Deserialize<DecisionTreeModel>();
            Assert.AreEqual(model.Hint, lmodel.Hint);
            AreEqual(model.Tree, lmodel.Tree, false);
        }
예제 #4
0
 public void Predict()
 {
     Tennis[] data = Tennis.GetData();
     Descriptor descriptor = Descriptor.Create<Tennis>();
     var generator = new DecisionTreeGenerator(descriptor);
     generator.SetHint(0.5d);
     LearningModel model = Learner.Learn(data, 0.8, 1000, generator);
     var input = new Tennis
     {
         Outlook = Outlook.Overcast,
         Temperature = 20,
         Windy = false
     };
     Tennis predict = model.Model.Predict(input);
     Console.WriteLine(predict.Play);
     Console.WriteLine(model);
 }
예제 #5
0
        static void Main(string[] args)
        {
            var data = Tennis.GetData();
            var descriptor = Descriptor.Create<Tennis>();
            var treeGenerator = new DecisionTreeGenerator(descriptor);
            treeGenerator.SetHint(false);
            var model = Learner.Learn(data, 0.80, 1000, treeGenerator);
            System.Console.WriteLine(model);
            System.Console.ReadKey();

            var t = new Tennis
            {
                Outlook = Outlook.Overcast,
                Temperature = Temperature.Low,
                Windy = true
            };
            var result = model.Model.Predict(t);
            System.Console.WriteLine(result.Play);
        }
예제 #6
0
        /// <summary>
        /// Do the <see cref="SimpleNumlWorkflow" /> using a <c>DecisionTree</c> generator/model.
        /// </summary>
        static void DoSimple_DecisionTree(Outlook outlook = Outlook.Sunny, double tempF = 15.0d, bool windy = true,
            int depth = 5, int width = 2, int repeat = 5000)
        {
            SimpleNumlWorkflow.SimpleNumlWorkflowImpl(
                getData: () => SampleData_ComplexType.GetTennisData_ComplexType(predetermined: false),

                getDescriptor: () => Descriptor.Create<Tennis_Complex>(),

                getGenerator: (descriptor) =>
                {
                    var generator = new DecisionTreeGenerator(descriptor)
                    {
                        Depth = depth,
                        Width = width
                    };
                    generator.SetHint(false);
                    return generator;
                },

                getToPredict: () =>
                {
                    return new Tennis_Complex()
                    {
                        WeatherConditions = new WeatherConditions()
                        {
                            Outlook = outlook,
                            Temperature = tempF,
                            Windy = windy,
                        }
                    };
                },

                getPredictionDesc: (t) => "Play: " + t.Play,

                trainingPercentage: 0.8d,

                repeat: repeat
                );
        }
예제 #7
0
        public static void Go()
        {
            // Start with our data
            Tennis[] data = SharedData.GetTennisData();

            // Create the corresponding descriptor
            var descriptor = Descriptor.Create<Tennis>();

            // Choose our generator
            var generator = new DecisionTreeGenerator(descriptor);
            generator.SetHint(false);

            Console.WriteLine($"Using the {generator.GetType().Name}\n");

            // Create the model by learning from the data using the generator
            LearningModel learningModel = Learner.Learn(data, 0.80, 1000, generator);

            Console.WriteLine(learningModel);

            // Now we could predict using the learning info's Model.
            var toPredict = new Tennis()
            {
                Outlook = Outlook.Rainy,
                Temperature = Temperature.Low,
                Windy = true,
                // Play = ? - This is what we will predict
            };

            var prediction = learningModel.Model.Predict(toPredict);

            // And we're spent...
            Console.WriteLine($"ToPredict: {toPredict}");
            Console.WriteLine($"Prediction: Play = {prediction.Play}\n");

            Console.WriteLine("Press any key...\n");
            Console.ReadKey();
        }
예제 #8
0
        public void Decision_Tree_Study_Category_Test()
        {
            var categoryA = Guid.NewGuid();
            var categoryB = Guid.NewGuid();

            var data = new[] {
                new { Study = 2.0, Category = categoryA, Passed = false},
                new { Study = 3.0, Category = categoryA, Passed = false},
                new { Study = 1.0, Category = categoryB, Passed = false},
                new { Study = 4.0, Category = categoryA, Passed = false},
                new { Study = 6.0, Category = categoryA, Passed = true},
                new { Study = 8.0, Category = categoryB, Passed = true},
                new { Study = 12.0, Category = categoryA, Passed = true},
                new { Study = 3.0, Category = categoryB, Passed = true}
            };

            var descriptor = Descriptor.New("Student")
                .With("Study").As(typeof(double))
                .With("Category").AsGuid()
                .Learn("Passed").As(typeof(bool));

            DecisionTreeGenerator generator = new DecisionTreeGenerator()
            {
                Descriptor = descriptor,
                NormalizeFeatures = true
            };

            var model = Learner.Learn(data, 0.8, 10, generator).Model;

            var test = model.PredictValue(new { Study = 7.0, Category = categoryA, Passed = false });

            Assert.Equal(true, test);
        }