Beispiel #1
0
        public void DataTransformers()
        {
            PakiraDecisionTreeGenerator pakiraGenerator = PakiraGeneratorTests.CreatePakiraGeneratorInstance();
            TrainData trainData = new TrainData();

            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 2, 3 }), 42);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 120, 140 }), 54);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 190, 200 }), 42);

            PassThroughTransformer      passThroughTransformer      = new PassThroughTransformer();
            MeanDistanceDataTransformer meanDistanceDataTransformer = new MeanDistanceDataTransformer();

            Converter <IEnumerable <double>, IEnumerable <double> > dataTransformers = null;

            dataTransformers += passThroughTransformer.ConvertAll;
            dataTransformers += meanDistanceDataTransformer.ConvertAll;

            pakiraGenerator.CertaintyScore = 10.0;

            PakiraDecisionTreeModel pakiraDecisionTreeModel = new PakiraDecisionTreeModel(dataTransformers, trainData.Samples[0]);

            pakiraDecisionTreeModel = pakiraGenerator.Generate(pakiraDecisionTreeModel, trainData);

            pakiraDecisionTreeModel.Tree.Root.ShouldNotBeNull();

            pakiraDecisionTreeModel.PredictNode(trainData.Samples[0]).Value.ShouldBe(trainData.Labels[0]);
            pakiraDecisionTreeModel.PredictNode(trainData.Samples[1]).Value.ShouldBe(trainData.Labels[1]);
            pakiraDecisionTreeModel.PredictNode(trainData.Samples[2]).Value.ShouldBe(trainData.Labels[2]);

            // The data transformers should allow to produce a very shallow tree
            pakiraDecisionTreeModel.Tree.GetNodes().Count().ShouldBe(3);
        }
Beispiel #2
0
        public void CertaintyScore()
        {
            PakiraDecisionTreeGenerator pakiraGenerator = PakiraGeneratorTests.CreatePakiraGeneratorInstance();

            pakiraGenerator.CertaintyScore = 1.0;

            TrainData trainData = new TrainData();

            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 2, 90 }), 42);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 250, 140 }), 54);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 200, 100 }), 42);

            PakiraDecisionTreeModel pakiraDecisionTreeModel = new PakiraDecisionTreeModel(trainData.Samples[0]);

            pakiraDecisionTreeModel = pakiraGenerator.Generate(pakiraDecisionTreeModel, trainData);

            pakiraDecisionTreeModel.Tree.Root.ShouldNotBeNull();

            pakiraDecisionTreeModel.PredictNode(trainData.Samples[0]).Value.ShouldBe(trainData.Labels[0]);
            pakiraDecisionTreeModel.PredictNode(trainData.Samples[1]).Value.ShouldBe(trainData.Labels[1]);
            pakiraDecisionTreeModel.PredictNode(trainData.Samples[2]).Value.ShouldBe(trainData.Labels[2]);

            pakiraDecisionTreeModel.PredictNode(new SabotenCache(trainData.Samples[0])).PakiraLeaf.Value.ShouldBe(trainData.Labels[0]);
            pakiraDecisionTreeModel.PredictNode(new SabotenCache(trainData.Samples[1])).PakiraLeaf.Value.ShouldBe(trainData.Labels[1]);
            pakiraDecisionTreeModel.PredictNode(new SabotenCache(trainData.Samples[2])).PakiraLeaf.Value.ShouldBe(trainData.Labels[2]);
        }
Beispiel #3
0
        public void DeepTree()
        {
            PakiraDecisionTreeGenerator pakiraGenerator = PakiraGeneratorTests.CreatePakiraGeneratorInstance();
            TrainData trainData = new TrainData();

            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 2, 3 }), 42);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 250, 254 }), 54);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 250, 255 }), 42);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 251, 253 }), 6);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 251, 254 }), 9);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 1, 2 }), 96);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 2, 1 }), 97);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 2, 2 }), 98);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 3, 2 }), 99);

            PakiraDecisionTreeModel pakiraDecisionTreeModel = new PakiraDecisionTreeModel(trainData.Samples[0]);

            pakiraGenerator.MinimumSampleCount = 100;

            pakiraDecisionTreeModel = pakiraGenerator.Generate(pakiraDecisionTreeModel, trainData);

            pakiraDecisionTreeModel.Tree.Root.ShouldNotBeNull();

            pakiraDecisionTreeModel.Tree.GetNodes().Count().ShouldBeGreaterThanOrEqualTo(15, "If the test fails because of this, the number can be reduced as long as it stays 'high'. Instead, the tree depth could also be validated.");
            pakiraDecisionTreeModel.PredictNode(trainData.Samples[0]).Value.ShouldBe(trainData.Labels[0]);
            pakiraDecisionTreeModel.PredictNode(trainData.Samples[1]).Value.ShouldBe(trainData.Labels[1]);
            pakiraDecisionTreeModel.PredictNode(trainData.Samples[2]).Value.ShouldBe(trainData.Labels[2]);
        }
Beispiel #4
0
        public void MinimumSampleCount()
        {
            PakiraDecisionTreeGenerator pakiraGenerator = PakiraGeneratorTests.CreatePakiraGeneratorInstance();
            TrainData trainData = new TrainData();

            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 2, 3 }), 42);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 20, 140 }), 54);
            trainData = trainData.AddSample(ImmutableList.CreateRange(new double[] { 33, 200 }), 42);

            PakiraDecisionTreeModel pakiraDecisionTreeModel = new PakiraDecisionTreeModel(trainData.Samples[0]);

            pakiraDecisionTreeModel = pakiraGenerator.Generate(pakiraDecisionTreeModel, trainData);

            pakiraDecisionTreeModel.Tree.Root.ShouldNotBeNull();

            pakiraDecisionTreeModel.PredictNode(trainData.Samples[0]).Value.ShouldBe(trainData.Labels[0]);
            pakiraDecisionTreeModel.PredictNode(trainData.Samples[1]).Value.ShouldBe(trainData.Labels[1]);
            pakiraDecisionTreeModel.PredictNode(trainData.Samples[2]).Value.ShouldBe(trainData.Labels[2]);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            const int featureWindowSize     = /*24*/ 16;
            const int halfFeatureWindowSize = featureWindowSize / 2;
            string    imageMainPath         = args[0];

            Image <L8> fullTextImage = Image.Load <L8>(imageMainPath + @"Images\FS18800114.2.11-a2-427w-c32.png");
            Image <L8> completeA     = Image.Load <L8>(imageMainPath + @"\Images\FS18800114.2.11-a2-427w-c32\a\CompleteA.png");

            PakiraDecisionTreeGenerator pakiraGenerator = new PakiraDecisionTreeGenerator();
            TrainData trainData = new TrainData();

            L8   whitePixel         = new L8(255);
            L8   blackPixel         = new L8(0);
            L8   dontCarePixel      = new L8(128);
            byte dontCarePixelValue = dontCarePixel.PackedValue;

            byte[] imageCropPixelsData = new byte[featureWindowSize * featureWindowSize * Unsafe.SizeOf <L8>()];

            completeA.ProcessPixelRows(accessor =>
            {
                Span <byte> imageCropPixels = new Span <byte>(imageCropPixelsData);

                for (int y = 0; y < accessor.Height; y++)
                {
                    Span <L8> pixelRow = accessor.GetRowSpan(y);

                    for (int x = 0; x < pixelRow.Length; x++)
                    {
                        // Get a reference to the pixel at position x
                        ref L8 pixel = ref pixelRow[x];

                        if (pixel != dontCarePixel)
                        {
                            Image <L8> whiteWindow = new Image <L8>(featureWindowSize, featureWindowSize, whitePixel);
                            Image <L8> imageCrop   = whiteWindow.Clone(clone => clone.DrawImage(fullTextImage, new Point(halfFeatureWindowSize - x, halfFeatureWindowSize - y), 1));

                            imageCrop.CopyPixelDataTo(imageCropPixels);

                            trainData = trainData.AddSample(imageCropPixelsData.Select <byte, double>(s => s), pixel.PackedValue);
                        }
                    }
                }
            });