Пример #1
0
        public void FittingTest()
        {
            int[] folds = CrossValidation.Splittings(100, 10);

            int[] samples = Matrix.Indices(0, 100);

            CrossValidation val = new CrossValidation(folds, 10);

            val.RunInParallel = false;

            int current = 0;

            val.Fitting = (k, trainingSamples, validationSamples) =>
            {
                Assert.AreEqual(current, k);
                Assert.AreEqual(90, trainingSamples.Length);
                Assert.AreEqual(10, validationSamples.Length);

                int[] trainingSet   = samples.Submatrix(trainingSamples);
                int[] validationSet = samples.Submatrix(validationSamples);

                for (int i = 0; i < trainingSet.Length; i++)
                {
                    Assert.AreEqual(samples[trainingSamples[i]], trainingSet[i]);
                }

                for (int i = 0; i < validationSet.Length; i++)
                {
                    Assert.AreEqual(samples[validationSamples[i]], validationSet[i]);
                }

                current++;

                return(new CrossValidationValues(k, 2 * k));
            };

            var result = val.Compute();

            Assert.AreEqual(10, current);
            Assert.AreEqual(4.5, result.Training.Mean);
            Assert.AreEqual(9.0, result.Validation.Mean);
            Assert.AreEqual(
                2 * result.Training.StandardDeviation,
                result.Validation.StandardDeviation);

            Assert.AreEqual(val.Folds.Length, result.Training.Sizes.Length);
            Assert.AreEqual(val.Folds.Length, result.Validation.Sizes.Length);

            for (int i = 0; i < result.Training.Sizes.Length; i++)
            {
                Assert.AreEqual(90, result.Training.Sizes[i]);
            }

            for (int i = 0; i < result.Validation.Sizes.Length; i++)
            {
                Assert.AreEqual(10, result.Validation.Sizes[i]);
            }
        }
Пример #2
0
        public void SplittingsTest()
        {
            int[] folds = CrossValidation.Splittings(100, 10);

            for (int i = 0; i < 10; i++)
            {
                int actual   = folds.Count(x => x == i);
                int expected = 10;

                Assert.AreEqual(expected, actual);
            }
        }
Пример #3
0
        public override Task <GeneralConfusionMatrix> ComputeConfusionMatrixAsync(ClassificationModel classificationModel)
        {
            return(Task.Factory.StartNew(() =>
            {
                int numFeatures = classificationModel.FeatureVectors.Count;

                double[][] input = new double[numFeatures][];
                int[] responses = new int[numFeatures];

                for (int featureIndex = 0; featureIndex < classificationModel.FeatureVectors.Count; ++featureIndex)
                {
                    var featureVector = classificationModel.FeatureVectors[featureIndex];

                    input[featureIndex] = Array.ConvertAll(featureVector.FeatureVector.BandIntensities, s => (double)s / ushort.MaxValue);
                    responses[featureIndex] = featureVector.FeatureClass;
                }

                var folds = new int[input.Length][];
                var splittings = CrossValidation.Splittings(input.Length, 2);
                for (int i = 0; i < 2; ++i)
                {
                    folds[i] = splittings.Find(x => x == i);
                }

                int[] indicesTrain = folds[0];
                int[] indicesValidation = folds[1];

                // Lets now grab the training data:
                var trainingInputs = input.Get(indicesTrain);
                var trainingOutputs = responses.Get(indicesTrain);

                // And now the validation data:
                var validationInputs = input.Get(indicesValidation);
                var validationOutputs = responses.Get(indicesValidation);

                // Predict
                int[] prediction;
                switch (Kernel)
                {
                case Kernel.Gaussian:
                    var gaussianLearningKfold = new MulticlassSupportVectorLearning <Gaussian>
                    {
                        Kernel = Gaussian.FromGamma(Gamma),
                        Learner = p => new SequentialMinimalOptimization <Gaussian>
                        {
                            UseKernelEstimation = false,
                            UseComplexityHeuristic = false,
                            Complexity = Complexity,
                            Token = CancellationTokenSource.Token,
                            Tolerance = 0.01
                        }
                    };
                    var svmGaussian = gaussianLearningKfold.Learn(trainingInputs, trainingOutputs);
                    prediction = svmGaussian.Decide(validationInputs);
                    break;

                case Kernel.Linear:
                    var linearLearning = new MulticlassSupportVectorLearning <Linear>
                    {
                        Learner = p => new LinearDualCoordinateDescent <Linear>
                        {
                            Complexity = Complexity,
                            UseComplexityHeuristic = false,
                            Token = CancellationTokenSource.Token
                        }
                    };
                    var svmLinear = linearLearning.Learn(trainingInputs, trainingOutputs);
                    prediction = svmLinear.Decide(validationInputs);
                    break;

                default:
                    throw new NotImplementedException();
                }

                GeneralConfusionMatrix confusionMatrix = new GeneralConfusionMatrix(classificationModel.LandCoverTypes.Count, prediction, validationOutputs);

                return confusionMatrix;
            }));
        }