Beispiel #1
0
        private static void linearSvm(double[][] inputs, int[] outputs)
        {
            // Create a L2-regularized L2-loss optimization algorithm for
            // the dual form of the learning problem. This is *exactly* the
            // same method used by LIBLINEAR when specifying -s 1 in the
            // command line (i.e. L2R_L2LOSS_SVC_DUAL).
            //
            var teacher = new LinearCoordinateDescent();

            // Teach the vector machine
            var svm = teacher.Learn(inputs, outputs);

            // Classify the samples using the model
            bool[] answers = svm.Decide(inputs);

            // Convert to Int32 so we can plot:
            int[] zeroOneAnswers = answers.ToZeroOne();

            // Plot the results
            ScatterplotBox.Show("Expected results", inputs, outputs);
            ScatterplotBox.Show("LinearSVM results", inputs, zeroOneAnswers);

            // Grab the index of multipliers higher than 0
            int[] idx = teacher.Lagrange.Find(x => x > 0);

            // Select the input vectors for those
            double[][] sv = inputs.Get(idx);

            // Plot the support vectors selected by the machine
            ScatterplotBox.Show("Support vectors", sv).Hold();
        }
        public void ComputeTest5()
        {
            var dataset = SequentialMinimalOptimizationTest.GetYingYang();

            double[][] inputs = dataset.Submatrix(null, 0, 1).ToJagged();
            int[]      labels = dataset.GetColumn(2).ToInt32();

            var kernel = new Polynomial(2, 1);

            Accord.Math.Tools.SetupGenerator(0);
            var projection = inputs.Apply(kernel.Transform);
            var machine    = new SupportVectorMachine(projection[0].Length);
            var smo        = new LinearCoordinateDescent(machine, projection, labels)
            {
                Complexity = 1000000,
                Tolerance  = 1e-15
            };

            double error = smo.Run();

            Assert.AreEqual(1000000.0, smo.Complexity, 1e-15);

            int[] actual = new int[labels.Length];
            for (int i = 0; i < actual.Length; i++)
            {
                actual[i] = Math.Sign(machine.Compute(projection[i]));
            }

            ConfusionMatrix matrix = new ConfusionMatrix(actual, labels);

            Assert.AreEqual(6, matrix.FalseNegatives);
            Assert.AreEqual(7, matrix.FalsePositives);
            Assert.AreEqual(44, matrix.TruePositives);
            Assert.AreEqual(43, matrix.TrueNegatives);
        }
Beispiel #3
0
        private static void linearSvm(double[][] inputs, int[] outputs)
        {
            // Create a linear binary machine with 2 inputs
            var svm = new SupportVectorMachine(inputs: 2);

            // Create a L2-regularized L2-loss optimization algorithm for
            // the dual form of the learning problem. This is *exactly* the
            // same method used by LIBLINEAR when specifying -s 1 in the
            // command line (i.e. L2R_L2LOSS_SVC_DUAL).
            //
            var teacher = new LinearCoordinateDescent(svm, inputs, outputs);

            // Teach the vector machine
            double error = teacher.Run();

            // Classify the samples using the model
            int[] answers = inputs.Apply(svm.Compute).Apply(System.Math.Sign);

            // Plot the results
            ScatterplotBox.Show("Expected results", inputs, outputs);
            ScatterplotBox.Show("LinearSVM results", inputs, answers);

            // Grab the index of multipliers higher than 0
            int[] idx = teacher.Lagrange.Find(x => x > 0);

            // Select the input vectors for those
            double[][] sv = inputs.Submatrix(idx);

            // Plot the support vectors selected by the machine
            ScatterplotBox.Show("Support vectors", sv).Hold();
        }
        public void multithread_test()
        {
            Accord.Math.Random.Generator.Seed = 0;

            var dataset = SequentialMinimalOptimizationTest.GetYingYang();

            double[][] inputs = dataset.Submatrix(null, 0, 1).ToJagged();
            int[]      labels = dataset.GetColumn(2).ToInt32();

            var kernel     = new Polynomial(2, 1);
            var projection = kernel.Transform(inputs);

            var t    = new Thread[100];
            var svms = new SupportVectorMachine <Linear> [t.Length];

            for (int i = 0; i < t.Length; i++)
            {
                t[i] = new Thread((object obj) =>
                {
                    int j = (int)obj;

                    var teacher = new LinearCoordinateDescent <Linear>()
                    {
                        Complexity = 1000000,
                        Tolerance  = 1e-15
                    };

                    svms[j] = teacher.Learn(projection, labels);
                });

                t[i].Start(i);
            }

            for (int i = 0; i < t.Length; i++)
            {
                t[i].Join();
            }

            double[][] sv = svms[0].SupportVectors;
            double[]   w  = svms[0].Weights;
            for (int i = 0; i < svms.Length; i++)
            {
                Assert.IsTrue(sv.IsEqual(svms[i].SupportVectors));
                Assert.IsTrue(w.IsEqual(svms[i].Weights));

                int[] actual = svms[i].Decide(projection).ToMinusOnePlusOne();

                var matrix = new ConfusionMatrix(actual, labels);
                Assert.AreEqual(6, matrix.FalseNegatives);
                Assert.AreEqual(7, matrix.FalsePositives);
                Assert.AreEqual(44, matrix.TruePositives);
                Assert.AreEqual(43, matrix.TrueNegatives);
            }
        }
Beispiel #5
0
        public void  Train(List <TrainingValue> trainingData)
        {
            List <DecisionVariable> trainingVariables = new List <DecisionVariable>();

            for (int i = 0; i < featureSize; i++)
            {
                trainingVariables.Add(DecisionVariable.Continuous(i.ToString()));
            }

            tree = new DecisionTree(inputs: trainingVariables, classes: 2);


            double[][] featuresArray = new double[trainingData.Count][];
            int[]      labels        = new int[trainingData.Count];

            for (int i = 0; i < featuresArray.Length; i++)
            {
                featuresArray[i] = trainingData[i].Features;
                labels[i]        = Convert.ToInt32(trainingData[i].State);
            }

            switch (type)
            {
            case ClassifierType.DecisionTree:
                C45Learning teacher = new C45Learning(tree);
                teacher.Learn(featuresArray, labels);
                break;

            case ClassifierType.LDA:
                LinearDiscriminantAnalysis lda = new LinearDiscriminantAnalysis();
                pipeline = lda.Learn(featuresArray, labels);
                break;

            case ClassifierType.SVM:
                LinearCoordinateDescent svmLearner = new LinearCoordinateDescent();
                svm = svmLearner.Learn(featuresArray, labels);
                break;

            case ClassifierType.Bayes:
                NaiveBayesLearning <NormalDistribution> learner = new NaiveBayesLearning <NormalDistribution>();
                bayes = learner.Learn(featuresArray, labels);
                break;
            }

            Trained = true;
        }
Beispiel #6
0
        private void TrainAndTrade()
        {
            if (!_window.IsReady)
            {
                return;
            }

            // Convert the rolling window of rate of change into the Learn method
            var returns = new double[_inputSize];
            var targets = new double[_lookback];
            var inputs  = new double[_lookback][];

            // Use the sign of the returns to predict the direction
            for (var i = 0; i < _lookback; i++)
            {
                for (var j = 0; j < _inputSize; j++)
                {
                    returns[j] = Math.Sign(_window[i + j + 1]);
                }

                targets[i] = Math.Sign(_window[i]);
                inputs[i]  = returns;
            }

            // Train SupportVectorMachine using SetHoldings("SPY", percentage);
            var teacher = new LinearCoordinateDescent();

            teacher.Learn(inputs, targets);

            var svm = teacher.Model;

            // Compute the value for the last rate of change
            var last  = (double)Math.Sign(_window[0]);
            var value = svm.Compute(new[] { last });

            if (value.IsNaNOrZero())
            {
                return;
            }

            SetHoldings("SPY", Math.Sign(value));
        }
        public void LearnTest()
        {
            double[][] inputs =
            {
                new double[] { -1, -1 },
                new double[] { -1,  1 },
                new double[] {  1, -1 },
                new double[] {  1,  1 }
            };

            int[] xor =
            {
                -1,
                1,
                1,
                -1
            };

            var kernel = new Polynomial(2, 0.0);

            double[][] augmented = new double[inputs.Length][];
            for (int i = 0; i < inputs.Length; i++)
            {
                augmented[i] = kernel.Transform(inputs[i]);
            }

            SupportVectorMachine machine = new SupportVectorMachine(augmented[0].Length);

            // Create the Least Squares Support Vector Machine teacher
            var learn = new LinearCoordinateDescent(machine, augmented, xor);

            // Run the learning algorithm
            learn.Run();

            int[] output = augmented.Apply(p => Math.Sign(machine.Compute(p)));
            for (int i = 0; i < output.Length; i++)
            {
                Assert.AreEqual(System.Math.Sign(xor[i]), System.Math.Sign(output[i]));
            }
        }
        public void linear_regression_test()
        {
            #region doc_linreg
            // Declare some training data. This is exactly the same
            // data used in the MultipleLinearRegression documentation page

            // We will try to model a plane as an equation in the form
            // "ax + by + c = z". We have two input variables (x and y)
            // and we will be trying to find two parameters a and b and
            // an intercept term c.

            // Create the linear-SVM learning algorithm
            var teacher = new LinearCoordinateDescent()
            {
                Tolerance  = 1e-10,
                Complexity = 1e+10, // learn a hard-margin model
            };

            // Now suppose you have some points
            double[][] inputs =
            {
                new double[] { 1, 1 },
                new double[] { 0, 1 },
                new double[] { 1, 0 },
                new double[] { 0, 0 },
            };

            // located in the same Z (z = 1)
            double[] outputs = { 1, 1, 1, 1 };

            // Learn the support vector machine
            var svm = teacher.Learn(inputs, outputs);

            // Convert the svm to logistic regression
            var regression = (MultipleLinearRegression)svm;

            // As result, we will be given the following:
            double a = regression.Weights[0]; // a = 0
            double b = regression.Weights[1]; // b = 0
            double c = regression.Intercept;  // c = 1

            // This is the plane described by the equation
            // ax + by + c = z => 0x + 0y + 1 = z => 1 = z.

            // We can compute the predicted points using
            double[] predicted = regression.Transform(inputs);

            // And the squared error loss using
            double error = new SquareLoss(outputs).Loss(predicted);
            #endregion

            var rsvm = (SupportVectorMachine)regression;
            Assert.AreEqual(2, rsvm.NumberOfInputs);
            Assert.AreEqual(2, rsvm.NumberOfClasses);
            Assert.AreEqual(1, rsvm.NumberOfOutputs);

            Assert.AreEqual(2, regression.NumberOfInputs);
            Assert.AreEqual(1, regression.NumberOfOutputs);

            Assert.AreEqual(0.0, a, 1e-6);
            Assert.AreEqual(0.0, b, 1e-6);
            Assert.AreEqual(1.0, c, 1e-6);
            Assert.AreEqual(0.0, error, 1e-6);

            double[] expected = regression.Compute(inputs);
            double[] actual   = regression.Transform(inputs);
            Assert.IsTrue(expected.IsEqual(actual, 1e-10));

            double r = regression.CoefficientOfDetermination(inputs, outputs);
            Assert.AreEqual(1.0, r);
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                WriteLine("Usage:\n\n\ttrain.exe <folder path>");
                return;
            }

            WriteLine("Platform: " + IntPtr.Size);

            var stemmer = new SnowballStemmer();

            using (var zip = new ZipOutputStream(
                       OpenWrite(
                           Combine(
                               GetDirectoryName(args[0]),
                               GetFileName(args[0]) + ".cl"))))
            {
                WriteLine("Reading dictionary...");
                var dicPath = Combine(args[0], "dic.txt");
                var dicText = File.Exists(dicPath) ? ReadAllLines(dicPath) : new string[0];

                WriteLine("Building dictionary...");
                var text = ReadAllLines(Combine(args[0], "text.txt"));

                var dic = new Dictionary(dicText.Concat(text
                                                        .SelectMany(jd => jd.StemText(stemmer))
                                                        .ToLookup(w => w.Stemmed)
                                                        .Select(l => new { Stemmed = l.Key, Count = l.Count() })
                                                        .OrderByDescending(w => w.Count)
                                                        .Select(w => w.Stemmed))
                                         .Take(5000)
                                         .ToArray());

                zip.PutNextEntry(new ZipEntry("dic.txt"));
                using (var writer = new StreamWriter(zip, UTF8, 4096, true))
                    dic.WriteTo(writer);
                zip.CloseEntry();

                WriteLine("Vectorizing...");
                var input = new double[text.Length][];
                for (int i = 0; i < text.Length; i++)
                {
                    input[i] = dic.Vectorize(new Document(text[i], stemmer));
                }

                var classifiers = from f in GetFiles(args[0])
                                  where GetFileName(f) != "text.txt" && GetFileName(f) != "dic.txt"
                                  where GetExtension(f) == ".txt"
                                  let ll                                              = ReadAllLines(f)
                                                             let ld                   = ll.Distinct().ToArray()
                                                                               let li = ld
                                                                                        .Select((l, i) => new { l, i })
                                                                                        .ToDictionary(x => x.l, x => x.i)
                                                                                        select new
                {
                    Name   = GetFileNameWithoutExtension(f),
                    Labels = ld,
                    Output = ll.Select(l => li[l]).ToArray()
                };

                foreach (var classifier in classifiers)
                {
                    Write($"Training: {classifier.Name}");
                    IKernel kernel  = new Linear();
                    var     machine = new MulticlassSupportVectorMachine(input[0].Length, kernel, classifier.Labels.Length);
                    var     teacher = new MulticlassSupportVectorLearning(machine, input, classifier.Output);
                    teacher.Algorithm = (svm, classInputs, classOutputs, i, j) =>
                    {
                        //var sequentialMinimalOptimization = new SequentialMinimalOptimization(svm, classInputs, classOutputs);
                        //sequentialMinimalOptimization.Run();
                        var linearCoordinateDescent = new LinearCoordinateDescent(svm, classInputs, classOutputs);
                        linearCoordinateDescent.Run();

                        var probabilisticOutputLearning = new ProbabilisticOutputCalibration(svm, classInputs, classOutputs);
                        return(probabilisticOutputLearning);
                    };
                    WriteLine($", error = {teacher.Run()}");

                    zip.PutNextEntry(new ZipEntry(classifier.Name + ".svm"));
                    using (var stream = new MemoryStream())
                    {
                        machine.Save(stream);
                        stream.Position = 0;
                        stream.CopyTo(zip);
                    }
                    zip.CloseEntry();

                    zip.PutNextEntry(new ZipEntry(classifier.Name + ".txt"));
                    using (var writer = new StreamWriter(zip, UTF8, 4096, true))
                        foreach (var lable in classifier.Labels)
                        {
                            writer.WriteLine(lable);
                        }

                    zip.CloseEntry();
                }

                WriteLine("Done.");
            }
        }
Beispiel #10
0
        public static void train_one(Problem prob, Parameters param, out double[] w, double Cp, double Cn)
        {
            double[][] inputs = prob.Inputs;
            int[]      labels = prob.Outputs.Apply(x => x >= 0 ? 1 : -1);

            double eps = param.Tolerance;

            int pos = 0;

            for (int i = 0; i < labels.Length; i++)
            {
                if (labels[i] >= 0)
                {
                    pos++;
                }
            }
            int neg = prob.Outputs.Length - pos;

            double primal_solver_tol = eps * Math.Max(Math.Min(pos, neg), 1.0) / prob.Inputs.Length;

            SupportVectorMachine          svm     = new SupportVectorMachine(prob.Dimensions);
            ISupportVectorMachineLearning teacher = null;


            switch (param.Solver)
            {
            case LibSvmSolverType.L2RegularizedLogisticRegression:

                // l2r_lr_fun
                teacher = new ProbabilisticNewtonMethod(svm, inputs, labels)
                {
                    PositiveWeight = Cp,
                    NegativeWeight = Cn,
                    Tolerance      = primal_solver_tol
                }; break;


            case LibSvmSolverType.L2RegularizedL2LossSvc:

                // fun_obj=new l2r_l2_svc_fun(prob, C);
                teacher = new LinearNewtonMethod(svm, inputs, labels)
                {
                    PositiveWeight = Cp,
                    NegativeWeight = Cn,
                    Tolerance      = primal_solver_tol
                }; break;


            case LibSvmSolverType.L2RegularizedL2LossSvcDual:

                // solve_l2r_l1l2_svc(prob, w, eps, Cp, Cn, L2R_L2LOSS_SVC_DUAL);
                teacher = new LinearCoordinateDescent(svm, inputs, labels)
                {
                    Loss           = Loss.L2,
                    PositiveWeight = Cp,
                    NegativeWeight = Cn,
                }; break;


            case LibSvmSolverType.L2RegularizedL1LossSvcDual:

                // solve_l2r_l1l2_svc(prob, w, eps, Cp, Cn, L2R_L1LOSS_SVC_DUAL);
                teacher = new LinearCoordinateDescent(svm, inputs, labels)
                {
                    Loss           = Loss.L1,
                    PositiveWeight = Cp,
                    NegativeWeight = Cn,
                }; break;


            case LibSvmSolverType.L1RegularizedLogisticRegression:

                // solve_l1r_lr(&prob_col, w, primal_solver_tol, Cp, Cn);
                teacher = new ProbabilisticCoordinateDescent(svm, inputs, labels)
                {
                    PositiveWeight = Cp,
                    NegativeWeight = Cn,
                    Tolerance      = primal_solver_tol
                }; break;


            case LibSvmSolverType.L2RegularizedLogisticRegressionDual:

                // solve_l2r_lr_dual(prob, w, eps, Cp, Cn);
                teacher = new ProbabilisticDualCoordinateDescent(svm, inputs, labels)
                {
                    PositiveWeight = Cp,
                    NegativeWeight = Cn,
                    Tolerance      = primal_solver_tol,
                }; break;
            }


            Trace.WriteLine("Training " + param.Solver);

            // run the learning algorithm
            var    sw    = Stopwatch.StartNew();
            double error = teacher.Run();

            sw.Stop();

            // save the solution
            w = svm.ToWeights();

            Trace.WriteLine(String.Format("Finished {0}: {1} in {2}",
                                          param.Solver, error, sw.Elapsed));
        }
        public void ComputeTest5()
        {
            var dataset = SequentialMinimalOptimizationTest.yinyang;

            double[][] inputs = dataset.Submatrix(null, 0, 1).ToArray();
            int[]      labels = dataset.GetColumn(2).ToInt32();

            var kernel = new Polynomial(2, 0);

            {
                var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length);
                var smo     = new SequentialMinimalOptimization(machine, inputs, labels);
                smo.UseComplexityHeuristic = true;

                double error = smo.Run();

                Assert.AreEqual(0.11714451552090824, smo.Complexity);

                int[] actual = new int[labels.Length];
                for (int i = 0; i < actual.Length; i++)
                {
                    actual[i] = Math.Sign(machine.Compute(inputs[i]));
                }

                ConfusionMatrix matrix = new ConfusionMatrix(actual, labels);
                Assert.AreEqual(20, matrix.FalseNegatives);
                Assert.AreEqual(0, matrix.FalsePositives);
                Assert.AreEqual(30, matrix.TruePositives);
                Assert.AreEqual(50, matrix.TrueNegatives);
            }

            {
                Accord.Math.Tools.SetupGenerator(0);
                var projection = inputs.Apply(kernel.Transform);
                var machine    = new SupportVectorMachine(projection[0].Length);
                var smo        = new LinearCoordinateDescent(machine, projection, labels);
                smo.UseComplexityHeuristic = true;
                smo.Tolerance = 0.01;

                double error = smo.Run();

                Assert.AreEqual(0.11714451552090821, smo.Complexity, 1e-15);

                int[] actual = new int[labels.Length];
                for (int i = 0; i < actual.Length; i++)
                {
                    actual[i] = Math.Sign(machine.Compute(projection[i]));
                }

                ConfusionMatrix matrix = new ConfusionMatrix(actual, labels);
                Assert.AreEqual(17, matrix.FalseNegatives);
                Assert.AreEqual(1, matrix.FalsePositives);
                Assert.AreEqual(33, matrix.TruePositives);
                Assert.AreEqual(49, matrix.TrueNegatives);
            }

            {
                Accord.Math.Tools.SetupGenerator(0);
                var projection = inputs.Apply(kernel.Transform);
                var machine    = new SupportVectorMachine(projection[0].Length);
                var smo        = new LinearCoordinateDescent(machine, projection, labels);
                smo.UseComplexityHeuristic = true;
                smo.Loss = Loss.L1;

                double error = smo.Run();

                Assert.AreEqual(0.11714451552090821, smo.Complexity, 1e-15);

                int[] actual = new int[labels.Length];
                for (int i = 0; i < actual.Length; i++)
                {
                    actual[i] = Math.Sign(machine.Compute(kernel.Transform(inputs[i])));
                }

                ConfusionMatrix matrix = new ConfusionMatrix(actual, labels);
                Assert.AreEqual(20, matrix.FalseNegatives);
                Assert.AreEqual(0, matrix.FalsePositives);
                Assert.AreEqual(30, matrix.TruePositives);
                Assert.AreEqual(50, matrix.TrueNegatives);
            }
        }