Exemple #1
0
        private static void sparseMachine(Sparse <double>[] inputs, double[] doubleOutputs)
        {
            // The dataset has output labels as 4 and 2. We have to convert them
            // into negative and positive labels so they can be properly processed.
            //
            bool[] outputs = doubleOutputs.Apply(x => x == 2.0 ? false : true);

            // Create a learning algorithm for Sparse data. The first generic argument
            // of the learning algorithm below is the chosen kernel function, and the
            // second is the type of inputs the machine should accept. Note that, using
            // those interfaces, it is possible to define custom kernel functions that
            // operate directly on double[], string[], graphs, trees or any object:
            var teacher = new LinearDualCoordinateDescent <Linear, Sparse <double> >()
            {
                Loss       = Loss.L2,
                Complexity = 1000, // Create a hard-margin SVM
                Tolerance  = 1e-5
            };

            // Use the learning algorithm to Learn
            var svm = teacher.Learn(inputs, outputs);

            // Compute the machine's answers
            bool[] answers = svm.Decide(inputs);

            // Create a confusion matrix to show the machine's performance
            var m = new ConfusionMatrix(predicted: answers, expected: outputs);

            // Show it onscreen
            DataGridBox.Show(new ConfusionMatrixView(m)).Hold();
        }
Exemple #2
0
        private static void sparseMachineProbabilistic(Sparse <double>[] inputs, double[] doubleOutputs)
        {
            // The dataset has output labels as 4 and 2. We have to convert them
            // into negative and positive labels so they can be properly processed.
            //
            bool[] outputs = doubleOutputs.Apply(x => x == 2.0 ? false : true);

            // Create a learning algorithm for Sparse data. The first generic argument
            // of the learning algorithm below is the chosen kernel function, and the
            // second is the type of inputs the machine should accept. Note that, using
            // those interfaces, it is possible to define custom kernel functions that
            // operate directly on double[], string[], graphs, trees or any object:
            var teacher = new LinearDualCoordinateDescent <Linear, Sparse <double> >()
            {
                Loss       = Loss.L2,
                Complexity = 1000, // Create a hard-margin SVM
                Tolerance  = 1e-5
            };

            // Use the learning algorithm to Learn
            var svm = teacher.Learn(inputs, outputs);

            // Create a probabilistic calibration algorithm based on Platt's method:
            var calibration = new ProbabilisticOutputCalibration <Linear, Sparse <double> >()
            {
                Model = svm
            };

            // Let's say that instead of having our data as bool[], we would
            // have received it as double[] containing the actual probabilities
            // associated with each sample:
            doubleOutputs.Apply(x => x == 2.0 ? 0.05 : 0.87, result: doubleOutputs);

            // Calibrate the SVM using Platt's method
            svm = calibration.Learn(inputs, doubleOutputs);

            // Compute the machine's answers
            bool[] answers = svm.Decide(inputs);

            // Compute the machine's probabilities
            double[] prob = svm.Probability(inputs);

            // Create a confusion matrix to show the machine's performance
            var m = new ConfusionMatrix(predicted: answers, expected: outputs);

            // Show it onscreen
            DataGridBox.Show(new ConfusionMatrixView(m)).Hold();
        }
Exemple #3
0
        private static void cancer()
        {
            // Create a new LibSVM sparse format data reader
            // to read the Wisconsin's Breast Cancer dataset
            //
            var reader = new SparseReader("examples-sparse.txt");

            int[]      outputs; // Read the classification problem into dense memory
            double[][] inputs = reader.ReadToEnd(sparse: false, labels: out outputs);

            // The dataset has output labels as 4 and 2. We have to convert them
            // into negative and positive labels so they can be properly processed.
            //
            outputs = outputs.Apply(x => x == 2 ? -1 : +1);

            // Create a new linear-SVM for the problem dimensions
            var svm = new SupportVectorMachine(inputs: reader.Dimensions);

            // Create a learning algorithm for the problem's dimensions
            var teacher = new LinearDualCoordinateDescent(svm, inputs, outputs)
            {
                Loss       = Loss.L2,
                Complexity = 1000,
                Tolerance  = 1e-5
            };

            // Learn the classification
            double error = teacher.Run();

            // Compute the machine's answers for the learned inputs
            int[] answers = inputs.Apply(x => Math.Sign(svm.Compute(x)));

            // Create a confusion matrix to show the machine's performance
            var m = new ConfusionMatrix(predicted: answers, expected: outputs);

            // Show it onscreen
            DataGridBox.Show(new ConfusionMatrixView(m));
        }
        public void ComputeTest1()
        {
            double[,] data = Matrix.Identity(5);

            DataGridBox.Show(data).Hold();
        }