Ejemplo n.º 1
0
        private GeneralConfusionMatrix Test(MultinomialLogisticRegression logReg, double[][] x_test, int[] y_expected, out int[] y_predicted)
        {
            y_predicted = logReg.Decide(x_test);
            var logReg_conf = new GeneralConfusionMatrix(y_expected, y_predicted);

            return(logReg_conf);
        }
Ejemplo n.º 2
0
        private static void Predict()
        {
            var lgrPred = -1;
            var lgrProb = -1.0;

            if (MultinomialLogisticRegression != null)
            {
                lgrPred = MultinomialLogisticRegression.Decide(CurrPredictionPoints);
                lgrProb = MultinomialLogisticRegression.Probability(CurrPredictionPoints);
                PredictedFrequencyClassifiers = lgrPred;
                if (Classification.IsValidation)
                {
                    Console.WriteLine($"Real Frequency: {BrainStorm0.ClassificationShape.Hertz} Logistic Regression Predicted Frequency: {lgrPred} Probability: {lgrProb}");
                }
            }

            var mmdPred  = -1;
            var mmdScore = -1.0;

            if (MinimumMeanDistance != null)
            {
                mmdPred  = MinimumMeanDistance.Decide(CurrPredictionPoints);
                mmdScore = MinimumMeanDistance.Score(CurrPredictionPoints);
                if (Classification.IsValidation)
                {
                    Console.WriteLine(
                        $"Real Frequency: {BrainStorm0.ClassificationShape.Hertz} Minimun Distance Predicted Frequency: {PredictedFrequencyClassifiers}");
                }
                else
                {
                    Console.WriteLine(
                        $"MMD Predicted Frequency: {mmdPred} {mmdScore}");
                }
            }

            var rfPred = -1;

            if (RandomForest != null)
            {
                rfPred = RandomForest.Decide(CurrPredictionPoints);
                if (Classification.IsValidation)
                {
                    Console.WriteLine(
                        $"Real Frequency: {BrainStorm0.ClassificationShape.Hertz} Random Forest Predicted Frequency: {rfPred}");
                }
                else
                {
                    Console.WriteLine(
                        $"Random Forest Predicted Frequency: {rfPred}");
                }
            }
            if (IsTyping)
            {
                UpdateTypingPredictions();
            }
        }
Ejemplo n.º 3
0
        static public int [] MultiNomialLogRegressionLowerBoundNewtonRaphson(double [][] input1, int[] labels, string SaveFile)
        {
            // http://accord-framework.net/docs/html/T_Accord_Statistics_Models_Regression_MultinomialLogisticRegression.htm
            // Create a estimation algorithm to estimate the regression
            LowerBoundNewtonRaphson lbnr = new LowerBoundNewtonRaphson()
            {
                MaxIterations = 10,
                Tolerance     = 1e-6
            };
            // *******************************************************************************
            var cv = CrossValidation.Create(

                k: 10,     // We will be using 10-fold cross validation

                // First we define the learning algorithm:
                learner: (p) => new LowerBoundNewtonRaphson(),

                // Now we have to specify how the n.b. performance should be measured:
                loss: (actual, expected, p) => new ZeroOneLoss(expected).Loss(actual),

                // This function can be used to perform any special
                // operations before the actual learning is done, but
                // here we will just leave it as simple as it can be:
                fit: (teach, x, y, w) => teach.Learn(x, y, w),

                // Finally, we have to pass the input and output data
                // that will be used in cross-validation.
                x: input1, y: labels
                );
            // Genrate a cross validation of the data
            var cvresult = cv.Learn(input1, labels);



            // iteratively estimate the  model
            MultinomialLogisticRegression mlr = lbnr.Learn(input1, labels);

            // Generate statistics from confusion matrices
            ConfusionMatrix        cm  = ConfusionMatrix.Estimate(mlr, input1, labels);
            GeneralConfusionMatrix gcm = cvresult.ToConfusionMatrix(input1, labels);

            Funcs.Utility.OutPutStats(cvresult.NumberOfSamples, cvresult.NumberOfInputs,
                                      cvresult.Training.Mean, gcm.Accuracy, cm.FalsePositives, cm.FalseNegatives, cm.FScore);

            // We can compute the model answers
            int[]  answers       = mlr.Decide(input1);
            string modelsavefile = SaveFile.Replace(".csv", ".MLR.save");

            mlr.Save(modelsavefile, compression: SerializerCompression.None);

            return(answers);
        }
Ejemplo n.º 4
0
        static public int[] MultiNomialLogisticRegressionBFGS(double [][] input, int [] labels, string fName)
        {
            /* The L-BFGS algorithm is a member of the broad family of quasi-Newton optimization methods.
             * L-BFGS stands for 'Limited memory BFGS'. Indeed, L-BFGS uses a limited memory variation of
             * the Broyden–Fletcher–Goldfarb–Shanno (BFGS) update to approximate the inverse Hessian matrix
             * (denoted by Hk). Unlike the original BFGS method which stores a dense approximation, L-BFGS
             * stores only a few vectors that represent the approximation implicitly. Due to its moderate
             * memory requirement, L-BFGS method is particularly well suited for optimization problems with
             * a large number of variables.
             */

            // Create a lbfgs model
            var mlbfgs = new MultinomialLogisticLearning <BroydenFletcherGoldfarbShanno>();

            // Estimate using the data against a logistic regression
            MultinomialLogisticRegression mlr = mlbfgs.Learn(input, labels);

            //
            // Create a cross validation model derived from the training set to measure the performance of this
            // predictive model and estimate how well we expect the model will generalize. The algorithm executes
            // multiple rounds of cross validation on different partitions and averages the results.
            //
            int folds = 4; // could play around with this later
            var cv    = CrossValidation.Create(k: folds, learner: (p) => new MultinomialLogisticLearning <BroydenFletcherGoldfarbShanno>(),
                                               loss: (actual, expected, p) => new ZeroOneLoss(expected).Loss(actual),
                                               fit: (teacher, x, y, w) => teacher.Learn(x, y, w),
                                               x: input, y: labels);
            var result = cv.Learn(input, labels);
            GeneralConfusionMatrix gcm = result.ToConfusionMatrix(input, labels);
            ConfusionMatrix        cm  = ConfusionMatrix.Estimate(mlr, input, labels);

            //
            //output relevant statistics
            //
            Funcs.Utility.OutPutStats(result.NumberOfSamples, result.NumberOfInputs,
                                      result.Training.Mean, gcm.Accuracy, cm.FalsePositives, cm.FalseNegatives, cm.FScore);

            // Compute the model predictions and return the values
            int[] answers = mlr.Decide(input);

            // And also the probability of each of the answers
            double[][] probabilities = mlr.Probabilities(input);

            // Now we can check how good our model is at predicting
            double error = new Accord.Math.Optimization.Losses.ZeroOneLoss(labels).Loss(answers);

            mlr.Save(fName, compression: SerializerCompression.None);

            return(answers);
        }
        public void doc_learn()
        {
            #region doc_learn
            // Declare a very simple classification/regression
            // problem with only 2 input variables (x and y):
            double[][] inputs =
            {
                new[] { 3.0, 1.0 },
                new[] { 7.0, 1.0 },
                new[] { 3.0, 1.1 },
                new[] { 3.0, 2.0 },
                new[] { 6.0, 1.0 },
            };

            // Class labels for each of the inputs
            int[] outputs =
            {
                0, 2, 0, 1, 2
            };

            // Create a estimation algorithm to estimate the regression
            LowerBoundNewtonRaphson lbnr = new LowerBoundNewtonRaphson()
            {
                MaxIterations = 100,
                Tolerance     = 1e-6
            };

            // Now, we will iteratively estimate our model:
            MultinomialLogisticRegression mlr = lbnr.Learn(inputs, outputs);

            // We can compute the model answers
            int[] answers = mlr.Decide(inputs);

            // And also the probability of each of the answers
            double[][] probabilities = mlr.Probabilities(inputs);

            // Now we can check how good our model is at predicting
            double error = new ZeroOneLoss(outputs).Loss(answers);

            // We can also verify the classes with highest
            // probability are the ones being decided for:
            int[] argmax = probabilities.ArgMax(dimension: 1); // should be same as 'answers'
            #endregion

            Assert.AreEqual(0, error);
            Assert.AreEqual(answers, argmax);
        }
Ejemplo n.º 6
0
        private void createSurface(double[][] table)
        {
            // Get the ranges for each variable (X and Y)
            DoubleRange[] ranges = table.GetRange(0);

            // Generate a Cartesian coordinate system
            double[][] map = Matrix.Mesh(ranges[0], 200, ranges[1], 200);

            MultinomialLogisticRegression lr = mlr.Regression;

            // Classify each point in the Cartesian coordinate system
            double[] result = lr.Decide(map).ToDouble();

            double[,] surface = map.ToMatrix().InsertColumn(result);

            decisionMap.DataSource = surface;
        }
Ejemplo n.º 7
0
        public void RegressTest2()
        {
            Accord.Math.Random.Generator.Seed = 0;

            double[][] inputs;
            int[]      outputs;

            MultinomialLogisticRegressionTest.CreateInputOutputsExample1(out inputs, out outputs);

            // Create an algorithm to estimate the regression
            var msgd = new MultinomialLogisticLearning <ConjugateGradient>();

            // Now, we can iteratively estimate our model
            MultinomialLogisticRegression mlr = msgd.Learn(inputs, outputs);

            int[] predicted = mlr.Decide(inputs);

            double acc = new ZeroOneLoss(outputs).Loss(predicted);

            Assert.AreEqual(0.61088435374149663, acc, 1e-8);
        }
Ejemplo n.º 8
0
        private static void multinomial(double[][] inputs, int[] outputs)
        {
            var lbnr = new LowerBoundNewtonRaphson()
            {
                MaxIterations = 100,
                Tolerance     = 1e-6
            };

            // Learn a multinomial logistic regression using the teacher:
            MultinomialLogisticRegression mlr = lbnr.Learn(inputs, outputs);

            // We can compute the model answers
            int[] answers = mlr.Decide(inputs);

            // And also the probability of each of the answers
            double[][] probabilities = mlr.Probabilities(inputs);

            // Now we can check how good our model is at predicting
            double error = new AccuracyLoss(outputs).Loss(answers);

            // We can also verify the classes with highest
            // probability are the ones being decided for:
            int[] argmax = probabilities.ArgMax(dimension: 1); // should be same as 'answers'
        }
 /// <summary>
 /// Perform classification from loaded model and returns the infered class.
 /// </summary>
 /// <param name="vector">The input vector that should be list of double.</param>
 /// <returns>Resultant class as an integer</returns>
 public override int Classify(List <double> vector)
 {
     return(Model.Decide(vector.ToArray()));
 }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            if (args.Length > 3 | args.Length < 1 | args.Length < 3)
            {
                Console.WriteLine("Requires a previously trained and saved Model File");
                Console.WriteLine("Usage <testfile> <label file> <Model File>");
                System.Environment.Exit(-1);
            }

            Console.WriteLine("Logisitic Regression Prediction\n");
            string testFname   = args[0];
            string labelsFname = args[1];
            string ModelFname  = args[2];


            double[,] Rawdata;
            double[,] labeldata;
            // Read in the test data, validate file existence by attempting to open the files first
            try
            {
                FileStream fs = File.Open(testFname, FileMode.Open, FileAccess.Write, FileShare.None);
                fs.Close();
                // Reuse fs for validating labels
                fs = File.Open(labelsFname, FileMode.Open, FileAccess.Write, FileShare.None);
                fs.Close();

                fs = File.Open(ModelFname, FileMode.Open, FileAccess.Read, FileShare.None);
                fs.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error opening file{0}", e);
                System.Environment.Exit(-1);
            }
            using (CsvReader reader = new CsvReader(testFname, hasHeaders: false))
            {
                Rawdata = reader.ToMatrix();
            }
            using (CsvReader reader = new CsvReader(labelsFname, hasHeaders: false))
            {
                labeldata = reader.ToMatrix();
            }

            // Convert Raw data to Jagged array
            double[][] testdata = Rawdata.ToJagged();
            int[]      output1  = funcs.convetToJaggedArray(labeldata);

            int [] answers = new int[labeldata.GetLength(0)];

            // For Accord.net Logistic Regression the input data needs to be in Jagged Arrays
            // Labels can either be int (1,0) or bools
            if (ModelFname.IndexOf("bfgs", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                // Load a BFGS regression model
                try
                {
                    MultinomialLogisticRegression mlr = Serializer.Load <MultinomialLogisticRegression>(ModelFname);
                    answers = mlr.Decide(testdata);
                } catch (Exception e)
                {
                    Console.WriteLine("Error opening model file: {0}", ModelFname);
                    Console.WriteLine("Exception {0}", e);
                    System.Environment.Exit(-1);
                }
            }


            else if (ModelFname.IndexOf("pcd", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                LogisticRegression regression = new LogisticRegression();
                try
                {
                    regression = Serializer.Load <LogisticRegression>(ModelFname);
                    answers    = funcs.BoolToInt(regression.Decide(testdata));
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error opening model file: {0}", ModelFname);
                    Console.WriteLine("Exception {0}", e);
                    System.Environment.Exit(-1);
                }
            }

            Console.WriteLine("Successfully loaded model file => {0}", ModelFname);

            double subtotal = 0;
            int    index    = 0;

            foreach (var result in answers)
            {
                if (result == output1[index])
                {
                    subtotal = subtotal + 1;
                }
                index++;
            }
            double accuracy = subtotal / answers.Count();

            Console.WriteLine("Predicted accuracy using model:{0} is, {1}", ModelFname, Math.Round(accuracy * 100, 2));
        }
Ejemplo n.º 11
0
        public void LearnTest1()
        {
            #region doc_learn_0
            // Declare a simple classification/regression
            // problem with 5 input variables (a,b,c,d,e):
            double[][] inputs =
            {
                new double[] { 1, 4, 2, 0, 1 },
                new double[] { 1, 3, 2, 0, 1 },
                new double[] { 3, 0, 1, 1, 1 },
                new double[] { 3, 0, 1, 0, 1 },
                new double[] { 0, 5, 5, 5, 5 },
                new double[] { 1, 5, 5, 5, 5 },
                new double[] { 1, 0, 0, 0, 0 },
                new double[] { 1, 0, 0, 0, 0 },
                new double[] { 2, 4, 2, 0, 1 },
                new double[] { 2, 4, 2, 0, 1 },
                new double[] { 2, 6, 2, 0, 1 },
                new double[] { 2, 7, 5, 0, 1 },
            };

            // Class labels for each of the inputs
            int[] outputs =
            {
                0, 0, 1, 1, 2, 2, 3, 3, 0, 0, 0, 0
            };
            #endregion

            {
                #region doc_learn_cg
                // Create a Conjugate Gradient algorithm to estimate the regression
                var mcg = new MultinomialLogisticLearning <ConjugateGradient>();

                // Now, we can estimate our model using Conjugate Gradient
                MultinomialLogisticRegression mlr = mcg.Learn(inputs, outputs);

                // We can compute the model answers
                int[] answers = mlr.Decide(inputs);

                // And also the probability of each of the answers
                double[][] probabilities = mlr.Probabilities(inputs);

                // Now we can check how good our model is at predicting
                double error = new ZeroOneLoss(outputs).Loss(answers);
                #endregion

                Assert.AreEqual(0, error, 1e-5);
            }

            {
                #region doc_learn_gd
                // Create a Conjugate Gradient algorithm to estimate the regression
                var mgd = new MultinomialLogisticLearning <GradientDescent>();

                // Now, we can estimate our model using Gradient Descent
                MultinomialLogisticRegression mlr = mgd.Learn(inputs, outputs);

                // We can compute the model answers
                int[] answers = mlr.Decide(inputs);

                // And also the probability of each of the answers
                double[][] probabilities = mlr.Probabilities(inputs);

                // Now we can check how good our model is at predicting
                double error = new ZeroOneLoss(outputs).Loss(answers);
                #endregion

                Assert.AreEqual(0, error, 1e-5);
            }

            {
                #region doc_learn_bfgs
                // Create a Conjugate Gradient algorithm to estimate the regression
                var mlbfgs = new MultinomialLogisticLearning <BroydenFletcherGoldfarbShanno>();

                // Now, we can estimate our model using BFGS
                MultinomialLogisticRegression mlr = mlbfgs.Learn(inputs, outputs);

                // We can compute the model answers
                int[] answers = mlr.Decide(inputs);

                // And also the probability of each of the answers
                double[][] probabilities = mlr.Probabilities(inputs);

                // Now we can check how good our model is at predicting
                double error = new ZeroOneLoss(outputs).Loss(answers);
                #endregion

                Assert.AreEqual(0, error, 1e-5);
            }
        }
        public void LearnTest1()
        {
            double[][] inputs =
            {
                new double[] { 1, 4, 2, 0, 1 },
                new double[] { 1, 3, 2, 0, 1 },
                new double[] { 3, 0, 1, 1, 1 },
                new double[] { 3, 0, 1, 0, 1 },
                new double[] { 0, 5, 5, 5, 5 },
                new double[] { 1, 5, 5, 5, 5 },
                new double[] { 1, 0, 0, 0, 0 },
                new double[] { 1, 0, 0, 0, 0 },
                new double[] { 2, 4, 2, 0, 1 },
                new double[] { 2, 4, 2, 0, 1 },
                new double[] { 2, 6, 2, 0, 1 },
                new double[] { 2, 7, 5, 0, 1 },
            };

            int[] outputs =
            {
                0, 0,
                1, 1,
                2, 2,
                3, 3,
                0, 0, 0, 0
            };

            // Create an algorithm to estimate the regression
            var mcg = new MultinomialLogisticLearning <ConjugateGradient>();

            // Now, we can iteratively estimate our model
            MultinomialLogisticRegression mlr = mcg.Learn(inputs, outputs);

            int[] predicted = mlr.Decide(inputs);

            double error = new ZeroOneLoss(outputs).Loss(predicted);

            Assert.AreEqual(0, error);


            // Create an algorithm to estimate the regression
            var mgd = new MultinomialLogisticLearning <GradientDescent>();

            // Now, we can iteratively estimate our model
            mlr = mgd.Learn(inputs, outputs);

            predicted = mlr.Decide(inputs);

            error = new ZeroOneLoss(outputs).Loss(predicted);
            Assert.AreEqual(0, error, 1e-5);


            // Create an algorithm to estimate the regression
            var mlbfgs = new MultinomialLogisticLearning <BroydenFletcherGoldfarbShanno>();

            // Now, we can iteratively estimate our model
            mlr = mlbfgs.Learn(inputs, outputs);

            predicted = mlr.Decide(inputs);

            error = new ZeroOneLoss(outputs).Loss(predicted);
            Assert.AreEqual(0, error, 1e-5);
        }
 public override double Classify(List <double> featureVector)
 {
     return(Model.Decide(featureVector.ToArray()));
 }