Beispiel #1
0
        public void Learn()
        {
            if (Equals(LearningInputs, null))
            {
                return;
            }
            if (Equals(LearningOutputs, null))
            {
                return;
            }

            //Set kernal params :
            UseKernel = KernelEnum.Gaussian;
            InitilizeKernel();

            // Creates a new SMO for regression learning algorithm
            var teacher = new SequentialMinimalOptimizationRegression()
            {
                // Set learning parameters
                Complexity = Param_Complexity,
                Tolerance  = Param_Tolerance,
                Epsilon    = Param_Epsilon,
                Kernel     = kernel
            };

            // Use the teacher to create a machine
            svm = teacher.Learn(LearningInputs, LearningOutputs);

            // Check if we got support vectors
            if (svm.SupportVectors.Length == 0)
            {
                Console.WriteLine("Sorry, No SVMs.");
                return;
            }

            // Compute results for learning and testing data
            _Computed_LearningOutputs = svm.Score(LearningInputs);

            //foreach (double[] itm in TestingInputs)
            //{
            //    foreach (double value in itm)
            //    {
            //        Console.Write(value);
            //    }
            //    Console.WriteLine("");
            //}

            _Computed_TestingOutputs = svm.Score(TestingInputs);

            // foreach (double value in _Computed_TestingOutputs)
            //{
            //   Console.WriteLine(value);
            //}

            // Compute statistical results


            BestLearningScore = Statistics.Compute_DeterminationCoeff_R2(LearningOutputs, _Computed_LearningOutputs);
            BestTestingScore  = Statistics.Compute_DeterminationCoeff_R2(TestingOutputs, _Computed_TestingOutputs);
        }
Beispiel #2
0
        private static void SupportVectorMachineTraining(IEnumerable <MatchingPair> trainingData, IEnumerable <MatchingPair> testData, IDictionary <string, IndexableAttributeMetadata> actualMetadata)
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();

            var trainingInputs  = trainingData.Select(data => data.ToVectorArray(actualMetadata)).ToArray();
            var trainingOutputs = trainingData.Select(data => data.PercentMatch > 0).ToArray();
            var testInputs      = testData.Select(data => data.ToVectorArray(actualMetadata)).ToArray();
            var testOutputs     = testData.Select(data => data.PercentMatch > 0).ToArray();

            var learn = new SequentialMinimalOptimization <Gaussian>()
            {
                UseComplexityHeuristic = true,
                UseKernelEstimation    = true
            };

            SupportVectorMachine <Gaussian> svm = learn.Learn(trainingInputs, trainingOutputs);

            var inSampleScore    = svm.Score(trainingInputs);
            var outOfSampleScore = svm.Score(testInputs);

            Logger.InfoFormat("Result:\nIn-sample: {0}\nOut-of-sample:{1}", string.Join(", ", inSampleScore), string.Join(", ", outOfSampleScore));

            var results        = svm.Decide(trainingInputs);
            var inSampleErrors = trainingOutputs.Where((t, i) => results[i] != t).Count();

            results = svm.Decide(testInputs);
            var outOfSampleErrors = testOutputs.Where((t, i) => results[i] != t).Count();

            Logger.InfoFormat("Errors: In-sample: {0} Out-of-sample: {1}", inSampleErrors, outOfSampleErrors);

            stopWatch.Stop();
            Logger.InfoFormat("Regression Tree learning took {0}", stopWatch.Elapsed);
        }
        public void learn_test()
        {
            #region doc_learn
            Accord.Math.Random.Generator.Seed = 0;

            // Example regression problem. Suppose we are trying
            // to model the following equation: f(x, y) = 2x + y

            double[][] inputs =         // (x, y)
            {
                new double[] { 0,  1 }, // 2*0 + 1 =  1
                new double[] { 4,  3 }, // 2*4 + 3 = 11
                new double[] { 8, -8 }, // 2*8 - 8 =  8
                new double[] { 2,  2 }, // 2*2 + 2 =  6
                new double[] { 6,  1 }, // 2*6 + 1 = 13
                new double[] { 5,  4 }, // 2*5 + 4 = 14
                new double[] { 9,  1 }, // 2*9 + 1 = 19
                new double[] { 1,  6 }, // 2*1 + 6 =  8
            };

            double[] outputs = // f(x, y)
            {
                1, 11, 8, 6, 13, 14, 19, 8
            };

            // Create the sequential minimal optimization teacher
            var learn = new SequentialMinimalOptimizationRegression <Polynomial>()
            {
                Kernel     = new Polynomial(2), // Polynomial Kernel of 2nd degree
                Complexity = 100
            };

            // Run the learning algorithm
            SupportVectorMachine <Polynomial> svm = learn.Learn(inputs, outputs);

            // Compute the predicted scores
            double[] predicted = svm.Score(inputs);

            // Compute the error between the expected and predicted
            double error = new SquareLoss(outputs).Loss(predicted);

            // Compute the answer for one particular example
            double fxy = svm.Score(inputs[0]); // 1.0003849827673186
            #endregion

            Assert.AreEqual(1.0, fxy, 1e-2);
            for (int i = 0; i < outputs.Length; i++)
            {
                Assert.AreEqual(outputs[i], predicted[i], 1e-2);
            }
        }
        public static void SVM(Book newBook, List <Book> oldBooks, Author author)
        {
            double[][] inputs = oldBooks.Select(book => new double[] { book.AverageSentenceWordCount,
                                                                       book.PunctoationToWordRatio,
                                                                       book.NounToWordRatio,
                                                                       book.VerbToWordRatio,
                                                                       book.AdjectiveToWordRatio,
                                                                       book.AdverbToWordRatio }).ToArray();

            int[] outputs = oldBooks.Select(book => (book.Author.Gender.GenderID)).ToArray();

            var learn = new SequentialMinimalOptimization <Gaussian>()
            {
                UseComplexityHeuristic = true,
                UseKernelEstimation    = true
            };

            SupportVectorMachine <Gaussian> svm = learn.Learn(inputs, outputs);

            double[] newBookInput = new double[] { newBook.AverageSentenceWordCount,
                                                   newBook.PunctoationToWordRatio,
                                                   newBook.NounToWordRatio,
                                                   newBook.VerbToWordRatio,
                                                   newBook.AdjectiveToWordRatio,
                                                   newBook.AdverbToWordRatio };
            int newGender = (int)svm.Score(newBookInput);

            author.GenderID = newGender;
        }
Beispiel #5
0
        double[] GenerateDataFromData(double[] outputs)//扩充小样本
        {
            Accord.Math.Random.Generator.Seed = 0;
            double[][] inputs = new double[outputs.Count()][];
            for (int index = 0; index < outputs.Count(); index++)
            {
                inputs[index] = new double[] { (double)index / outputs.Count() };
            }

            var learn = new SequentialMinimalOptimizationRegression <Polynomial>()
            {
                Kernel     = new Polynomial(2), // Polynomial Kernel of 2nd degree
                Complexity = 100
            };

            // Run the learning algorithm
            SupportVectorMachine <Polynomial> svm = learn.Learn(inputs, outputs);

            // Compute the predicted scores


            int mCount = 1000;//输出样本数量

            double[][] tempinputs = new double[mCount][];
            for (int index = 0; index < mCount; index++)
            {
                tempinputs[index] = new double[] { (double)index / mCount };
            }
            double[] predicted = svm.Score(tempinputs);
            return(predicted);
        }
        public override List <Vector3> TransformGpsToWorld(List <Location> locations)
        {
            if (!TransformationAvailable)
            {
                throw new Exception("Transformation is not available yet.");
            }

            if (locations.Count <= 0)
            {
                throw new ArgumentException("The input array of locations is empty");
            }

            double[][] in_XY = new double[records.Count][];
            int        idx   = 0;

            foreach (Location loc in locations)
            {
                in_XY[idx] = new double[] { loc.Longitude, loc.Latitude };
                idx++;
            }
            double[]       out_x  = svm_x.Score(in_XY);
            double[]       out_y  = svm_y.Score(in_XY);
            List <Vector3> output = new List <Vector3>();

            for (int i = 0; i < locations.Count; i++)
            {
                output.Add(new Vector3((float)out_x[i], (float)out_y[i]));
            }

            return(output);
        }
Beispiel #7
0
        private void testing(double[,] arr)
        {
            // Extract the first columns (X)
            double[][] inputs = arr.GetColumns(0).ToJagged();

            // Extract the expected output values
            double[] expected = arr.GetColumn(1);

            // Compute the actual machine outputs
            double[] output = svm.Score(inputs);

            if (output.Length == 4)
            {
                output1 = new double[output.Length];
                for (int i = 0; i < output.Length; i++)
                {
                    output1[i] = output[i];
                }
            }
            else if (output.Length % 4 == 0)
            {
                for (int i = output.Length; i > (output.Length - output1.Length); i--)
                {
                    if (output[i - 1] > (output1[i - (output.Length - output1.Length) - 1] + 20))
                    {
                        MessageBox.Show("Network load is not matching predicted values. It could be network attack.");
                        return;
                    }
                }
            }
        }
Beispiel #8
0
            public double[] Compute(double[][] inputs)
            {
                if (Equals(svm, null))
                {
                    return(null);
                }

                return(svm.Score(inputs));
            }
Beispiel #9
0
            public void Optimizer_ObjectiveFunction(double[] solution, ref double fitnessValue)
            {
                Console.WriteLine(Optimizer.CurrentIteration);

                //Set kernal params :
                kernelG.Sigma = solution[0];

                // Set paramsfor regression learning algorithm
                teacherSMOR.Complexity = solution[1];
                teacherSMOR.Tolerance  = solution[2];
                teacherSMOR.Epsilon    = solution[3];

                // Use the teacher to create a machine
                svm = teacherSMOR.Learn(LearningInputs, LearningOutputs);

                // Check if we got support vectors
                if (svm.SupportVectors.Length == 0)
                {
                    Console.WriteLine("Sorry, No SVMs.");
                    return;
                }

                // Compute results for learning and testing data
                _Computed_LearningOutputs = svm.Score(LearningInputs);
                _Computed_TestingOutputs  = svm.Score(TestingInputs);

                // Compute statistical
                LearningIndex = Statistics.Compute_RMSE(LearningOutputs, _Computed_LearningOutputs);
                TestingIndex  = Statistics.Compute_RMSE(TestingOutputs, _Computed_TestingOutputs);

                // Compute correlation R for learning and testing to controle results :
                var Rlern = Statistics.Compute_CorrelationCoeff_R(LearningOutputs, _Computed_LearningOutputs);
                var Rtest = Statistics.Compute_CorrelationCoeff_R(TestingOutputs, _Computed_TestingOutputs);

                Console.WriteLine("Index (learn) = {0} | Index (test) = {1} ; Correlation : R (learn) = {2} | R (test) = {3}", LearningIndex, TestingIndex, Rlern, Rtest);
                if (BestLearningScore < LearningIndex && BestTestingScore < TestingIndex)
                {
                    BestLearningScore = LearningIndex;
                    BestTestingScore  = TestingIndex;
                }
                //set the fitness value
                fitnessValue = Math.Pow(LearningIndex, 2) + Math.Pow(TestingIndex, 2);
            }
Beispiel #10
0
        public IEnumerable <XYtoZ> Predict(IEnumerable <Tuple <double, double> > xvalues)
        {
            EnsureAlreadyTrained();

            double [][] xyvaluesArray = xvalues.Select(i => new double[] { i.Item1, i.Item2 }).ToArray();
            double []   zvaluesArray  = _supportVectorMachine.Score(xyvaluesArray);
            for (int i = 0; i < xyvaluesArray.Length; ++i)
            {
                yield return(new XYtoZ()
                {
                    XY = new Tuple <double, double>(xyvaluesArray[i][0], xyvaluesArray[i][1]),
                    Z = zvaluesArray[i]
                });
            }
        }
Beispiel #11
0
        private SupportVectorMachine <Linear> getSVMRegression(GeoWave geoWave, int labelIdx, bool[] Dim2TakeNode, ref double[] svmApprox)
        {
            SupportVectorMachine <Linear> svmRegression = null;

            double[][] dataForRegression  = new double[geoWave.pointsIdArray.Count][];
            double[]   labelForRegression = new double[geoWave.pointsIdArray.Count];
            int        amountOfFeatures   = training_dt[0].Length;

            for (int i = 0; i < geoWave.pointsIdArray.Count; i++)
            {
                int index = geoWave.pointsIdArray[i];
                dataForRegression[i] = new double[userConfig.nFeatures];
                int k = 0;
                for (int j = 0; j < amountOfFeatures; j++)
                {
                    if (Dim2TakeNode[j])
                    {
                        dataForRegression[i][k] = training_dt[index][j];
                        k++;
                    }
                }
                labelForRegression[i] = training_label[index][labelIdx];
            }

            LinearRegressionNewtonMethod tmpSvmRegression = new LinearRegressionNewtonMethod()
            {
                UseComplexityHeuristic = true
            };

            try
            {
                svmRegression = tmpSvmRegression.Learn(dataForRegression, labelForRegression);
                svmApprox     = svmRegression.Score(dataForRegression);
            }
            catch (Exception e)
            {
                return(null);
            }
            if (svmApprox.Contains(double.NaN))
            {
                return(null);
            }
            return(svmRegression);
        }
        private void btnTestingRun_Click(object sender, EventArgs e)
        {
            if (svm == null || dgvTestingSource.DataSource == null)
            {
                MessageBox.Show("Please create a machine first.");
                return;
            }


            // Creates a matrix from the source data table
            double[,] table = (dgvTestingSource.DataSource as DataTable).ToMatrix();


            // Extract the first columns (X)
            double[][] inputs = table.GetColumns(0).ToJagged();

            // Extract the expected output values
            double[] expected = table.GetColumn(1);

            // Compute the actual machine outputs
            double[] output = svm.Score(inputs);


            // Compute R² and Sum-of-squares error
            double rSquared = Accord.Statistics.Tools.Determination(output, expected);
            double error    = Elementwise.Pow(expected.Subtract(output), 2).Sum() / output.Length;


            // Anonymous magic! :D
            var r = new { RSquared = rSquared, Error = error };

            dgvPerformance.DataSource = (new[] { r }).ToList();


            // Create performance scatter plot
            CreateResultScatterplot(zedGraphControl1, inputs, expected, output);
        }
Beispiel #13
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);

            // Create the learning algorithm from the parameters
            var teacher = create(param, Cp, Cn, inputs, labels);

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

            // Run the learning algorithm
            var sw = Stopwatch.StartNew();
            SupportVectorMachine svm = teacher.Learn(inputs, labels);

            sw.Stop();

            double error = new HingeLoss(labels).Loss(svm.Score(inputs));

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

            Trace.WriteLine(String.Format("Finished {0}: {1} in {2}",
                                          param.Solver, error, sw.Elapsed));
        }
        public void ReceiverOperatingCharacteristicConstructorTest3()
        {
            // This example shows how to measure the accuracy of a
            // binary classifier using a ROC curve. For this example,
            // we will be creating a Support Vector Machine trained
            // on the following instances:

            double[][] inputs =
            {
                // Those are from class -1
                new double[] { 2, 4, 0 },
                new double[] { 5, 5, 1 },
                new double[] { 4, 5, 0 },
                new double[] { 2, 5, 5 },
                new double[] { 4, 5, 1 },
                new double[] { 4, 5, 0 },
                new double[] { 6, 2, 0 },
                new double[] { 4, 1, 0 },

                // Those are from class +1
                new double[] { 1, 4, 5 },
                new double[] { 7, 5, 1 },
                new double[] { 2, 6, 0 },
                new double[] { 7, 4, 7 },
                new double[] { 4, 5, 0 },
                new double[] { 6, 2, 9 },
                new double[] { 4, 1, 6 },
                new double[] { 7, 2, 9 },
            };

            int[] outputs =
            {
                -1, -1, -1, -1, -1, -1, -1, -1, // first eight from class -1
                +1, +1, +1, +1, +1, +1, +1, +1  // last  eight from class +1
            };

            // Create a linear Support Vector Machine with 3 inputs
            var machine = new SupportVectorMachine(inputs: 3);

            // Create the sequential minimal optimization teacher
            var learn = new SequentialMinimalOptimization(machine, inputs, outputs)
            {
                Complexity = 1
            };

            // Run the learning algorithm
            double error = learn.Run();

            // Extract the input labels predicted by the machine
            double[] predicted = new double[inputs.Length];
            for (int i = 0; i < predicted.Length; i++)
            {
                predicted[i] = machine.Score(inputs[i]);
            }


            // Create a new ROC curve to assess the performance of the model
            var roc = new ReceiverOperatingCharacteristic(outputs, predicted);

            roc.Compute(100); // Compute a ROC curve with 100 points

            /*
             *          // Generate a connected scatter plot for the ROC curve and show it on-screen
             *          ScatterplotBox.Show(roc.GetScatterplot(includeRandom: true), nonBlocking: true)
             *
             *              .SetSymbolSize(0)      // do not display data points
             *              .SetLinesVisible(true) // show lines connecting points
             *              .SetScaleTight(true)   // tighten the scale to points
             *              .WaitForClose();
             */

            Assert.AreEqual(0.25, error);
            Assert.AreEqual(0.78125, roc.Area);
            // Assert.AreEqual(0.1174774, roc.StandardError, 1e-6); HanleyMcNeil
            // Assert.AreEqual(0.11958120746409709, roc.StandardError, 1e-6);
            Assert.AreEqual(0.132845321574701, roc.StandardError, 1e-6);
        }
Beispiel #15
0
        public void learn_test_square_polynomial()
        {
            Accord.Math.Random.Generator.Seed = 0;

            // Example regression problem. Suppose we are trying
            // to model the following equation: f(x) = x * x

            double[][] inputs = // (x)
            {
                new double[] {  -1 },
                new double[] {   4 },
                new double[] {   8 },
                new double[] {   2 },
                new double[] {   6 },
                new double[] {   5 },
                new double[] {   9 },
                new double[] {   1 },
                new double[] {   6 },
                new double[] {  -5 },
                new double[] {  -2 },
                new double[] {  -3 },
                new double[] {   5 },
                new double[] {   4 },
                new double[] {   1 },
                new double[] {   2 },
                new double[] {   0 },
                new double[] {   4 },
                new double[] {   8 },
                new double[] {   2 },
                new double[] {   6 },
                new double[] {  52 },
                new double[] {  95 },
                new double[] {   1 },
                new double[] {   6 },
                new double[] {   5 },
                new double[] {  -1 },
                new double[] {   2 },
                new double[] {   5 },
                new double[] {   4 },
                new double[] {  -4 },
                new double[] { -50 },
            };

            double[] outputs = inputs.GetColumn(0).Pow(2);

            // Create the sequential minimal optimization teacher
            var learn = new FanChenLinSupportVectorRegression <Polynomial>()
            {
                Kernel     = new Polynomial(degree: 2, constant: 0),
                Complexity = 100
            };

            // Run the learning algorithm
            SupportVectorMachine <Polynomial> svm = learn.Learn(inputs, outputs);

            // Compute the predicted scores
            double[] predicted = svm.Score(inputs);

            // Compute the error between the expected and predicted
            double error = new SquareLoss(outputs).Loss(predicted);

            // Compute the answer for one particular example
            double fxy = svm.Score(inputs[0]); // 1.000776033448912

            Assert.AreEqual(1.0, fxy, 1e-3);
            for (int i = 0; i < outputs.Length; i++)
            {
                Assert.AreEqual(outputs[i], predicted[i], 2e-3);
            }
        }
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (dgvLearningSource.DataSource == null)
            {
                MessageBox.Show("Please load some data first.");
                return;
            }

            // Finishes and save any pending changes to the given data
            dgvLearningSource.EndEdit();



            // Creates a matrix from the entire source data table
            double[][] table = (dgvLearningSource.DataSource as DataTable).ToJagged(out columnNames);

            // Get only the input vector values (first column)
            double[][] inputs = table.GetColumns(0);

            // Get only the outputs (last column)
            double[] outputs = table.GetColumn(1);


            // Create the specified Kernel
            IKernel kernel = createKernel();

            // Creates a new SMO for regression learning algorithm
            var teacher = new SequentialMinimalOptimizationRegression()
            {
                // Set learning parameters
                Complexity = (double)numC.Value,
                Tolerance  = (double)numT.Value,
                Epsilon    = (double)numEpsilon.Value,
                Kernel     = kernel
            };


            try
            {
                // Use the teacher to create a machine
                svm = teacher.Learn(inputs, outputs);

                lbStatus.Text = "Training complete!";
            }
            catch (ConvergenceException)
            {
                lbStatus.Text = "Convergence could not be attained. " +
                                "The learned machine might still be usable.";
            }



            // Check if we got support vectors
            if (svm.SupportVectors.Length == 0)
            {
                dgvSupportVectors.DataSource = null;
                graphSupportVectors.GraphPane.CurveList.Clear();
                return;
            }



            // Show support vectors on the Support Vectors tab page
            double[][] supportVectorsWeights = svm.SupportVectors.InsertColumn(svm.Weights);

            string[] supportVectorNames = columnNames.RemoveAt(columnNames.Length - 1).Concatenate("Weight");
            dgvSupportVectors.DataSource = new ArrayDataView(supportVectorsWeights, supportVectorNames);



            // Show the support vector labels on the scatter plot
            var supportVectorLabels = new double[svm.SupportVectors.Length];

            for (int i = 0; i < supportVectorLabels.Length; i++)
            {
                int j = inputs.Find(sv => sv == svm.SupportVectors[i])[0];
                supportVectorLabels[i] = outputs[j];
            }

            double[][] graph = svm.SupportVectors.InsertColumn(supportVectorLabels);

            CreateScatterplot(graphSupportVectors, graph);



            // Get the ranges for each variable (X and Y)
            DoubleRange range = table.GetColumn(0).GetRange();

            double[][] map = Vector.Range(range, stepSize: 0.05).ToJagged();

            // Classify each point in the Cartesian coordinate system
            double[][] surface = map.InsertColumn(svm.Score(map));

            CreateScatterplot(zedGraphControl2, surface);
        }
        void TestSVM()///检测SVM正确性
        {
            Accord.Math.Random.Generator.Seed = 0;

            // Example regression problem. Suppose we are trying
            // to model the following equation: f(x, y) = 2x + y

            //            double[][] inputs = // (x, y)
            //            {
            //    new double[] { 0,  1 }, // 2*0 + 1 =  1
            //    new double[] { 4,  3 }, // 2*4 + 3 = 11
            //    new double[] { 8, -8 }, // 2*8 - 8 =  8
            //    new double[] { 2,  2 }, // 2*2 + 2 =  6
            //    new double[] { 6,  1 }, // 2*6 + 1 = 13
            //    new double[] { 5,  4 }, // 2*5 + 4 = 14
            //    new double[] { 9,  1 }, // 2*9 + 1 = 19
            //    new double[] { 1,  6 }, // 2*1 + 6 =  8
            //};
            double[][] inputs = new double[8][];
            for (int i = 0; i < 8; i++)
            {
                double[] temp = new double[1];
                temp[0]   = Math.PI / 2 / 8 * i;
                inputs[i] = temp;
            }
            //            double[] outputs = // f(x, y)
            //            {
            //    1, 11, 8, 6, 13, 14, 19, 8
            //};
            double[] outputs = new double[8];
            for (int i = 0; i < 8; i++)
            {
                outputs[i] = Math.Sin(Math.PI / 2 / 8 * i);
            }

            // Create the sequential minimal optimization teacher
            var learn = new SequentialMinimalOptimizationRegression <Gaussian>()
            {
                //Kernel = new Polynomial(3), // Polynomial Kernel of 2nd degree
                //Kernel = new Gaussian(2),
                Complexity = 100
            };

            // Run the learning algorithm
            SupportVectorMachine <Gaussian> svm = learn.Learn(inputs, outputs);

            // Compute the predicted scores
            double[] predicted = svm.Score(inputs);

            // Compute the error between the expected and predicted
            double error = new SquareLoss(outputs).Loss(predicted);

            // Compute the answer for one particular example
            double fxy = svm.Score(new double[1] {
                2
            });                                          // 1.0003849827673186

            //double[] myerror = new double[8];
            //for(int i=0;i<8;i++)
            //{
            //    myerror[i]=Math.Abs(predicted[i]-outputs[i])/
            //}
        }