Exemplo n.º 1
0
        public void TrainTest1()
        {
            const int    LearnCount    = 100;
            const int    TestCount     = 1000;
            const int    Length        = 300;
            const double PositiveRatio = 0.1;

            // create samples
            List <(float[] x, bool y, float weight)> samples = SupportVectorMachineTest.GenerateSamples(LearnCount + TestCount, Length, PositiveRatio)
                                                               .Select(x => (x.x.Select(w => (float)w).ToArray(), x.y, (float)x.weight))
                                                               .ToList();

            // learn
            SequentualMinimalOptimization smo = new SequentualMinimalOptimization(new ChiSquare())
            {
                Algorithm = SMOAlgorithm.LibSVM,
                Tolerance = 0.01f,
            };

            SupportVectorMachine machine = SupportVectorMachine.Learn(
                smo,
                samples.Take(LearnCount).Select(x => x.x).ToList(),
                samples.Take(LearnCount).Select(x => x.y).ToList(),
                samples.Take(LearnCount).Select(x => x.weight).ToList(),
                CancellationToken.None);

            // test
            List <ClassificationResult <bool?> > results = samples
                                                           .Skip(LearnCount)
                                                           .Select(x => new ClassificationResult <bool?>(null, machine.Classify(x.x) > 0.5f, x.y, 1.0f, true))
                                                           .ToList();

            ClassificationReport <bool?> report = new ClassificationReport <bool?>(results);
        }