#pragma warning restore 612, 618

        private void computeInner(double[][] inputData, double[] outputData, double[] weights)
        {
            for (int i = 0; i < NumberOfInputs; i++)
            {
                // Create a diminished inner model without the current variable
                double[][] data = inputData.RemoveColumn(i);

                // Perform likelihood-ratio tests against diminished nested models
                var innerModel = new LogisticRegression()
                {
                    NumberOfInputs = NumberOfInputs - 1
                };

                var learning = new IterativeReweightedLeastSquares(innerModel)
                {
                    Iterations     = iterations,
                    Tolerance      = tolerance,
                    Regularization = regularization
                };

                learning.Learn(data, outputData, weights);

                double ratio = 2.0 * (logLikelihood - innerModel.GetLogLikelihood(data, outputData));
                ratioTests[i + 1] = new ChiSquareTest(ratio, 1);
            }

            innerComputed = true;
        }
Example #2
0
        /// <summary>
        /// Classify our data using Logistic Regression classifer and save the model.
        /// </summary>
        /// <param name="train_data">Frame objects that we will use to train classifers.</param>
        /// <param name="test_data">Frame objects that we will use to test classifers.</param>
        /// <param name="train_label">Labels of the train data.</param>
        /// <param name="test_label">Labels of the test data.</param>
        /// <param name="Classifier_Path">Path where we want to save the classifer on the disk.</param>
        /// <param name="Classifier_Name">Name of the classifer we wnat to save.</param>
        /// <returns></returns>
        public void LogisticRegression(double[][] train_data, double[][] test_data, int[] train_label, int[] test_label, String Classifier_Path, String Classifier_Name)
        {
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4,
                MaxIterations  = 100,
                Regularization = 0
            };

            LogisticRegression regression = learner.Learn(train_data, train_label);

            double ageOdds   = regression.GetOddsRatio(0);
            double smokeOdds = regression.GetOddsRatio(1);

            double[] scores = regression.Probability(test_data);

            //bool[] pre = regression.Decide(test_data);

            var cm = GeneralConfusionMatrix.Estimate(regression, test_data, test_label);

            double error = cm.Error;

            Console.WriteLine(error);

            regression.Save(Path.Combine(Classifier_Path, Classifier_Name));
        }
        /// <summary>
        /// standard IterativeReweightedLeastSquares algorithm
        /// </summary>
        private void DoLogisticRegressionAnalyze()
        {
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4,             // Let's set some convergence parameters
                Iterations     = 100,
                MaxIterations  = 100,              // maximum number of iterations to perform
                Regularization = 0
            };

            // Now, we can use the learner to finally estimate our model:
            LogisticRegression regression = learner.Learn(learningData, learningExpectedResult);

            foreach (var story in App.GetReleaseScrumData().CurrentSprintProxy.CurrentSprint.Stories)
            {
                if (excludeOwners.Contains(story.Owner))
                {
                    story.LRPreSuccessRate = noResultReasonExcludeUser;
                    continue;
                }
                if (story.Size <= 0)
                {
                    story.LRPreSuccessRate = noResultReasonStorySize0;
                    continue;
                }
                var inputData = GetInputDataForTargetStory(story);
                var scores    = regression.Probability(inputData);
                story.LRPreSuccessRate = string.Format("{0}%", string.Format("{0:N2}", scores[0] * 100));
            }
        }
Example #4
0
        static public int [] IterativeLeastSquares(double[][] input1, int[] output1, string fName)
        {
            double[] labels  = System.Array.ConvertAll <int, double>(output1, x => x);
            var      learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                // Gets or sets the tolerance value used to determine whether the algorithm has converged.
                Tolerance     = 1e-4, // Let's set some convergence parameters
                MaxIterations = 10,
                //MaxIterations = 100,  // maximum number of iterations to perform
                Regularization = 0
            };

            // Now, we can use the learner to finally estimate our model:
            LogisticRegression regression = learner.Learn(input1, output1);

            double [] coefficients = learner.Solution;

            double[] scores = regression.Probability(input1);

            regression.Save(fName.Replace(".csv", ".IRLS.save"), compression: SerializerCompression.None);

            // Finally, if we would like to arrive at a conclusion regarding
            // each sample, we can use the Decide method, which will transform
            // the probabilities (from 0 to 1) into actual true/false values:

            return(Funcs.Utility.BoolToInt(regression.Decide(input1)));

            // mean(double(p == y)) * 100);
        }
        private LogisticRegression compute(double[][] input, double[] output, double[] weights)
        {
            if (regression == null)
            {
                initialize(input, output);
                this.regression = new LogisticRegression()
                {
                    NumberOfInputs = input.Columns()
                };
            }

            var learning = new IterativeReweightedLeastSquares(regression)
            {
                Regularization = regularization,
                Iterations     = iterations,
                Tolerance      = tolerance,
                Token          = Token
            };

            learning.Learn(input, output, weights);

            this.InformationMatrix = learning.GetInformationMatrix();

            computeInformation(input, output, weights);

            innerComputed = false;
            if (ComputeInnerModels)
            {
                computeInner(input, output, weights);
            }

            return(regression);
        }
        static void Main(string[] args)
        {
            double[][] inputs =
            {
                new double[] {    0,    0 },
                new double[] { 0.25, 0.25 },
                new double[] {  0.5,  0.5 },
                new double[] {    1,    1 },
            };

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

            //Train a Logistic Regression Model
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                MaxIterations = 100
            };
            var logit = learner.Learn(inputs, outputs);

            //Predict Output
            bool[] predictions = logit.Decide(inputs);

            //Plot Results
            ScatterplotBox.Show("Expected Results", inputs, outputs);
            ScatterplotBox.Show("Actual Logistic Regression Output", inputs, predictions.ToZeroOne());

            Console.ReadKey();
        }
Example #7
0
        public void Classification_Train(double[,] train_docrule, int[] label, string algorithm)
        {
            string classmodelpath;
            int    attrSize     = eclatlitems.Count;
            int    attrSizeTest = eclatlitems.Count;

            // Specify the input variables
            DecisionVariable[] variables = new DecisionVariable[attrSize];
            for (int i = 0; i < attrSize; i++)
            {
                variables[i] = new DecisionVariable((i + 1).ToString(), DecisionVariableKind.Discrete);
            }

            if (algorithm == "Tree")
            {
                classmodelpath = algorithm + ".model";
                //RandomForest tree2 = new RandomForest(2, variables);
                DecisionTree tree    = new DecisionTree(variables, 2);
                C45Learning  teacher = new C45Learning(tree);
                var          model   = teacher.Learn(train_docrule.ToJagged(), label);
                //save model
                teacher.Save(Path.Combine("", classmodelpath));
            }
            if (algorithm == "SVM")
            {
                classmodelpath = algorithm + ".model";
                var learn = new SequentialMinimalOptimization()
                {
                    UseComplexityHeuristic = true,
                    UseKernelEstimation    = false
                };
                SupportVectorMachine teacher = learn.Learn(train_docrule.ToJagged(), label);
                //save model
                teacher.Save(Path.Combine("", classmodelpath));
            }

            if (algorithm == "Logistic")
            {
                classmodelpath = algorithm + ".model";
                var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
                {
                    Tolerance      = 1e-4, // Let's set some convergence parameters
                    Iterations     = 1,    // maximum number of iterations to perform
                    Regularization = 0
                };
                LogisticRegression teacher = learner.Learn(train_docrule.ToJagged(), label);
                teacher.Save(Path.Combine("", classmodelpath));
            }

            if (algorithm == "GA")
            {
                weights_ga_matlab();
            }
        }
Example #8
0
        public void ExecuteTest()
        {
            string[][] words =
            {
                new string[] { "今日", "は", "いい", "天気", "です"   },
                new string[] { "明日", "も", "いい", "天気", "でしょう" }
            };

            var codebook = new BagOfWords()
            {
                //MaximumOccurance = 1 // the resulting vector will have only 0's and 1's
                MaximumOccurance = int.MaxValue
            };

            // Compute the codebook (note: this would have to be done only for the training set)
            codebook.Learn(words);

            // Now, we can use the learned codebook to extract fixed-length
            // representations of the different texts (paragraphs) above:

            // Extract a feature vector from the text 1:
            double[] bow1 = codebook.Transform(words[0]);

            // Extract a feature vector from the text 2:
            double[] bow2 = codebook.Transform(words[1]);

            // we could also have transformed everything at once, i.e.
            double[][] bow = codebook.Transform(words);


            // Now, since we have finite length representations (both bow1 and bow2 should
            // have the same size), we can pass them to any classifier or machine learning
            // method. For example, we can pass them to a Logistic Regression Classifier to
            // discern between the first and second paragraphs

            // Lets create a Logistic classifier to separate the two paragraphs:
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4, // Let's set some convergence parameters
                Iterations     = 100,  // maximum number of iterations to perform
                Regularization = 0
            };

            // Now, we use the learning algorithm to learn the distinction between the two:
            LogisticRegression reg = learner.Learn(new[] { bow1, bow2 }, new[] { false, true });

            // Finally, we can predict using the classifier:
            bool c1 = reg.Decide(bow1); // Should be false
            bool c2 = reg.Decide(bow2); // Should be true

            Console.WriteLine(c1);
            Console.WriteLine(c2);
        }
        /// <summary>
        ///   Fits a logistic regression model to data until convergence.
        /// </summary>
        ///
        private bool fit(LogisticRegression regression, double[][] input, double[] output)
        {
            var irls = new IterativeReweightedLeastSquares(regression)
            {
                Tolerance  = tolerance,
                Iterations = iterations,
            };

            irls.Learn(input, output);

            // Check if the full model has converged
            return(irls.Iterations <= iterations);
        }
    public LogisticRegression DecisionSteer(double[][] inputs, int[] outputs)
    {
        var teacher = new IterativeReweightedLeastSquares <LogisticRegression>()
        {
            MaxIterations  = 100,
            Regularization = 1e-6
        };

        // Use the learning algorithm to induce the tree
        LogisticRegression tree = teacher.Learn(inputs, outputs);

        return(tree);
    }
Example #11
0
        public void BuildLR(List <train> datalist)
        {
            double[][] input;
            int[]      output;
            GetData(out input, out output, datalist);

            // To verify this hypothesis, we are going to create a logistic
            // regression model for those two inputs (age and smoking), learned
            // using a method called "Iteratively Reweighted Least Squares":

            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4, // Let's set some convergence parameters
                Iterations     = 100,  // maximum number of iterations to perform
                Regularization = 0
            };

            // Now, we can use the learner to finally estimate our model:
            regression = learner.Learn(input, output);

            // At this point, we can compute the odds ratio of our variables.
            // In the model, the variable at 0 is always the intercept term,
            // with the other following in the sequence. Index 1 is the age
            // and index 2 is whether the patient smokes or not.

            // For the age variable, we have that individuals with
            //   higher age have 1.021 greater odds of getting lung
            //   cancer controlling for cigarette smoking.
            double ageOdds = regression.GetOddsRatio(1); // 1.0208597028836701

            // For the smoking/non smoking category variable, however, we
            //   have that individuals who smoke have 5.858 greater odds
            //   of developing lung cancer compared to those who do not
            //   smoke, controlling for age (remember, this is completely
            //   fictional and for demonstration purposes only).
            double smokeOdds = regression.GetOddsRatio(2); // 5.8584748789881331

            // If we would like to use the model to predict a probability for
            // each patient regarding whether they are at risk of cancer or not,
            // we can use the Probability function:

            double[] scores = regression.Probability(input);

            // Finally, if we would like to arrive at a conclusion regarding
            // each patient, we can use the Decide method, which will transform
            // the probabilities (from 0 to 1) into actual true/false values:


            log("The LogisticRegression model has been trained");
        }
Example #12
0
        private static LogisticRegression TrainAccordModel(IDataView trainingData)
        {
            AccordIO data = IDataViewToAccord(trainingData);

            var trainer = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                MaxIterations  = 1000,
                Regularization = 1e-6
            };

            // Use the teacher algorithm to learn the regression:
            LogisticRegression lr = trainer.Learn(data.inputs, data.labels);

            return(lr);
            // Classify the samples using the model
            //bool[] answers = lr.Decide(inputs);
        }
Example #13
0
        public LogisticRegression GetLogisticRegressionParams(int index, double percentage)
        {
            LogisticInfo LI = new LogisticInfo();

            LI.LogisticParams = new double[2];
            int length     = MD.Length;
            int samplesize = (int)(length * percentage);

            double [][] inputArr = new double[samplesize * 2][];
            for (int i = 0; i < samplesize * 2; i++)
            {
                inputArr[i] = new double[2];
            }
            double[] outputArr = new double[samplesize * 2];
            for (int i = 0; i < samplesize; i++)
            {
                inputArr[i][0] = MD[i].GetParameters()[index];
                inputArr[i][1] = 0;
                outputArr[i]   = 0;
            }
            int j = length - samplesize;

            for (int i = samplesize; i < samplesize * 2; i++)
            {
                inputArr[i][0] = MD[j].GetParameters()[index];
                inputArr[i][1] = 0;
                outputArr[i]   = 1;
                j++;
            }

            LogisticRegression LR = new LogisticRegression();

            LR.NumberOfInputs = 1;
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4, // Let's set some convergence parameters
                Iterations     = 100,  // maximum number of iterations to perform
                Regularization = 0
            };
            LogisticRegression regression = learner.Learn(inputArr, outputArr);

            LI.LogisticParams[0] = -(regression.Intercept + 1);
            LI.LogisticParams[1] = regression.GetOddsRatio(1) - 1;
            return(regression);
        }
        public static void training()
        {
            string filepath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath + @"Content\files\indian_liver_patient.xls");

            DataTable table = new ExcelReader(filepath).GetWorksheet("indian_liver_patient");

            double[][] inputs = table.ToJagged <double>("Age", "Gender", "Total_Bilirubin", "Direct_Bilirubin", "Alkaline_Phosphotase"
                                                        , "Alamine_Aminotransferase", "Aspartate_Aminotransferase"
                                                        , "Total_Protiens", "Albumin", "Albumin_and_Globulin_Ratio");
            int[] outputs = table.ToArray <int>("Dataset");

            for (int i = 0; i < outputs.Length; i++)
            {
                outputs[i] = outputs[i] - 1;
            }

            DecisionVariable[] var =
            {
                new DecisionVariable("A",   DecisionVariableKind.Continuous),
                new DecisionVariable("G",   DecisionVariableKind.Continuous),
                new DecisionVariable("TB",  DecisionVariableKind.Continuous),
                new DecisionVariable("DB",  DecisionVariableKind.Continuous),
                new DecisionVariable("AP",  DecisionVariableKind.Continuous),
                new DecisionVariable("AA",  DecisionVariableKind.Continuous),
                new DecisionVariable("AS",  DecisionVariableKind.Continuous),
                new DecisionVariable("TP",  DecisionVariableKind.Continuous),
                new DecisionVariable("ALB", DecisionVariableKind.Continuous),
                new DecisionVariable("AGR", DecisionVariableKind.Continuous)
            };

            tree = new DecisionTree(var, 2);

            C45Learning teacher = new C45Learning(tree);

            teacher.Learn(inputs, outputs);
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-6, // Let's set some convergence parameters
                MaxIterations  = 1000, // maximum number of iterations to perform
                Regularization = 0
            };

            regression = learner.Learn(inputs, outputs);
        }
        public static void training()
        {
            string filepath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath + @"Content\files\diabetes.xls");

            DataTable table = new ExcelReader(filepath).GetWorksheet("diabetes");

            double[][] inputs  = table.ToJagged <double>("Glucose", "BloodPressure", "Insulin", "BMI", "Age");
            int[]      outputs = table.ToArray <int>("Outcome");

            DecisionVariable[] var =
            {
                new DecisionVariable("G",   DecisionVariableKind.Continuous),
                new DecisionVariable("B",   DecisionVariableKind.Continuous),
                new DecisionVariable("I",   DecisionVariableKind.Continuous),
                new DecisionVariable("BMI", DecisionVariableKind.Continuous),
                new DecisionVariable("A",   DecisionVariableKind.Continuous),
            };

            tree = new DecisionTree(var, 2);

            C45Learning teacher = new C45Learning(tree);

            teacher.Learn(inputs, outputs);


            //From Here Trying to find out the probability
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-6, // Let's set some convergence parameters
                MaxIterations  = 1000, // maximum number of iterations to perform
                Regularization = 0
            };

            regression = learner.Learn(inputs, outputs);

            //lra = new LogisticRegressionAnalysis()
            //{
            //    Regularization = 0
            //};
            //lra.Learn(inputs, outputs);
        }
        private LogisticRegression compute(double[][] input, double[] output, double[] weights)
        {
            var learning = new IterativeReweightedLeastSquares(regression)
            {
                Regularization = regularization,
                Iterations     = iterations,
                Tolerance      = tolerance
            };

            learning.Learn(input, output, weights);

            computeInformation(input, output, weights);

            innerComputed = false;
            if (ComputeInnerModels)
            {
                computeInner(input, output, weights);
            }

            return(regression);
        }
Example #17
0
        public void train(double[][] input, double[][] output, int epochs = 1)
        {
            if (input[0].Length != inputsCount)
            {
                throw new Exception("Input has a unexpected length: " + input[0].Length + "!=" + inputsCount);
            }

            double[] buyOutput  = new double[output.Length];
            double[] sellOutput = new double[output.Length];

            for (int i = 0; i < output.Length; i++)
            {
                buyOutput[i]  = output[i][0];
                sellOutput[i] = output[i][1];
            }

            for (int i = 0; i < epochs; i++)
            {
                teacherBuy.Learn(input, buyOutput);
                teacherSell.Learn(input, sellOutput);
            }
        }
        protected override void EndProcessing()
        {
            double[][] inputs;
            double[]   outputs;

            if (ParameterSetName == "XY")
            {
                inputs  = Converter.ToDoubleJaggedArray(X);
                outputs = Converter.ToDoubleArray(Y);
            }
            else
            {
                outputs = _data.GetColumn(OutputName).ToDoubleArray();

                _data.RemoveColumn(OutputName);
                inputs = _data.ToDoubleJaggedArray();
            }

            double[] w = null;
            if (Weights != null)
            {
                w = Converter.ToDoubleArray(Weights);
            }

            var model = new GeneralizedLinearRegression(LinkFunctionConvert.Get(LinkFunction))
            {
                NumberOfInputs = inputs[0].Length
            };

            var learner = new IterativeReweightedLeastSquares(model)
            {
                MaxIterations  = 200,
                Regularization = 0
            };

            learner.Learn(inputs, outputs, w);

            WriteObject(model);
        }
Example #19
0
        private static void logisticRegression(double[][] inputs, int[] outputs)
        {
            // Create iterative re-weighted least squares for logistic regressions
            var teacher = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                MaxIterations  = 100,
                Regularization = 1e-6
            };

            // Use the teacher algorithm to learn the regression:
            LogisticRegression lr = teacher.Learn(inputs, outputs);

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

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

            // Plot the results
            ScatterplotBox.Show("Expected results", inputs, outputs);
            ScatterplotBox.Show("Logistic Regression results", inputs, zeroOneAnswers)
            .Hold();
        }
        public void prediction_interval()
        {
            CsvReader reader = CsvReader.FromText(Properties.Resources.logreg, true);
            DataTable data   = reader.ToTable();

            double[][] inputs = data.ToArray("AGE");
            double[]   output = data.Columns["CHD"].ToArray();

            var learner = new IterativeReweightedLeastSquares <LogisticRegression>();

            var lr = learner.Learn(inputs, output);

            Assert.AreEqual(0.111, lr.Weights[0], 5e-4);
            Assert.AreEqual(-5.309, lr.Intercept, 5e-4);

            Assert.AreEqual(1.1337, lr.StandardErrors[0], 5e-5);
            Assert.AreEqual(0.0241, lr.StandardErrors[1], 5e-5);

            double ll = lr.GetLogLikelihood(inputs, output);

            Assert.AreEqual(-53.6765, ll, 1e-4);

            double[] point = new double[] { 50 };
            double   y     = lr.Score(point);

            double[][] im = learner.GetInformationMatrix();
            //double se = lr.GetStandardError(inputs, im);
            var ci = lr.GetConfidenceInterval(point, inputs.Length, im);

            Assert.AreEqual(0.435, ci.Min, 5e-3);
            Assert.AreEqual(0.677, ci.Max, 5e-3);

            var pi = lr.GetPredictionInterval(point, inputs.Length, im);

            Assert.AreEqual(0.1405, pi.Min, 5e-3);
            Assert.AreEqual(0.9075, pi.Max, 5e-3);
        }
Example #21
0
        static void Main(string[] args)
        {
            // sample input
            double[][] inputs =
            {
                new double[] { 0, 0 },
                new double[] { 1, 0 },
                new double[] { 0, 1 },
                new double[] { 1, 1 },
            };

            // sample binary output
            int[] outputs =
            {
                0,
                1,
                1,
                0,
            };

            // sample binary output for Neural Network
            double[][] nnOutputs =
            {
                new double[] { 1, 0 },
                new double[] { 0, 1 },
                new double[] { 0, 1 },
                new double[] { 1, 0 },
            };

            // sample multinomial output
            int[] multiOutputs =
            {
                0,
                1,
                1,
                2,
            };

            // 1. Binary Logistic Regression
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                MaxIterations = 100
            };
            var model = learner.Learn(inputs, outputs);

            var preds = model.Decide(inputs);

            Console.WriteLine("\n\n*Binary Logistic Regression Predictions: {0}", String.Join(", ", preds));

            // 2. Multinomial Logistic Regression
            var learner2 = new MultinomialLogisticLearning <GradientDescent>()
            {
                MiniBatchSize = 4
            };
            var model2 = learner2.Learn(inputs, multiOutputs);

            var preds2 = model2.Decide(inputs);

            Console.WriteLine("\n\n*Multinomial Logistic Regression Predictions: {0}", String.Join(", ", preds2));

            // 3. Binary Naive Bayes Classifier
            var learner3 = new NaiveBayesLearning <NormalDistribution>();
            var model3   = learner3.Learn(inputs, outputs);

            var preds3 = model2.Decide(inputs);

            Console.WriteLine("\n\n*Binary Naive Bayes Predictions: {0}", String.Join(", ", preds3));

            // 4. RandomForest
            var learner4 = new RandomForestLearning()
            {
                NumberOfTrees = 3,

                CoverageRatio = 0.9,

                SampleRatio = 0.9
            };
            var model4 = learner4.Learn(inputs, outputs);

            var preds4 = model4.Decide(inputs);

            Console.WriteLine("\n\n*Binary RandomForest Classifier Predictions: {0}", String.Join(", ", preds4));

            // 5. SVM
            var learner5 = new SequentialMinimalOptimization <Gaussian>();
            var model5   = learner.Learn(inputs, outputs);

            var preds5 = model5.Decide(inputs);

            Console.WriteLine("\n\n*Binary SVM Predictions: {0}", String.Join(", ", preds5));

            // 6. Neural Network
            var network = new ActivationNetwork(
                new BipolarSigmoidFunction(2),
                2,
                1,
                2
                );

            var teacher = new LevenbergMarquardtLearning(network);

            Console.WriteLine("\n-- Training Neural Network");
            int    numEpoch = 3;
            double error    = Double.PositiveInfinity;

            for (int i = 0; i < numEpoch; i++)
            {
                error = teacher.RunEpoch(inputs, nnOutputs);
                Console.WriteLine("* Epoch {0} - error: {1:0.0000}", i + 1, error);
            }

            double[][] nnPreds = inputs.Select(
                x => network.Compute(x)
                ).ToArray();

            int[] preds6 = nnPreds.Select(
                x => x.ToList().IndexOf(x.Max())
                ).ToArray();

            Console.WriteLine("\n\n*Binary Neural Network Predictions: {0}", String.Join(", ", preds6));


            Console.WriteLine("\n\n\n\nDONE!!");
            Console.ReadKey();
        }
        public void scores_probabilities_test()
        {
            double[][] input =
            {
                new double[] { 55, 0 }, // 0 - no cancer
                new double[] { 28, 0 }, // 0
                new double[] { 65, 1 }, // 0
                new double[] { 46, 0 }, // 1 - have cancer

                new double[] { 86, 1 }, // 1
                new double[] { 86, 1 }, // 1
                new double[] { 56, 1 }, // 1
                new double[] { 85, 0 }, // 0

                new double[] { 33, 0 }, // 0
                new double[] { 21, 1 }, // 0
                new double[] { 42, 1 }, // 1
            };

            double[] output =
            {
                0, 0, 0, 1,
                1, 1, 1, 0,
                0, 0, 1
            };

            double[] weights =
            {
                1.0, 1.0, 1.0, 1.0,
                0.5, 0.5, 1.0, 1.0,
                1.0, 1.0, 1.0
            };


            var teacher = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Regularization = 0
            };

            var target = teacher.Learn(input, output, weights);

            LogitLinkFunction link = (LogitLinkFunction)target.Link;

            Assert.AreEqual(0, link.A);
            Assert.AreEqual(1, link.B);

            Assert.AreEqual(-2.4577464307294092, target.Intercept, 1e-8);
            Assert.AreEqual(-2.4577464307294092, target.Coefficients[0], 1e-8);
            Assert.AreEqual(0.020645118265359252, target.Coefficients[1], 1e-8);
            Assert.AreEqual(1.7678893101571855, target.Coefficients[2], 1e-8);

            // Test Scores, LogLikelihoods and Probability functions
            // https://github.com/accord-net/framework/issues/570

            double[][] scoresAllSamples         = target.Scores(input);
            double[][] logLikelihoodsAllSamples = target.LogLikelihoods(input);
            double[][] probabilitiesAllSamples  = target.Probabilities(input);
            Assert.IsTrue(scoresAllSamples.IsEqual(Matrix.Apply(probabilitiesAllSamples, link.Function), rtol: 1e-5));

            Assert.IsTrue(probabilitiesAllSamples.IsEqual(logLikelihoodsAllSamples.Exp()));
            Assert.IsTrue(probabilitiesAllSamples.Sum(dimension: 1).IsEqual(Vector.Ones(11), rtol: 1e-6));


            bool[] decideAllSamples = target.Decide(input);
            double err = new ZeroOneLoss(output).Loss(decideAllSamples);

            Assert.AreEqual(0.18181818181818182, err, 1e-5);
            Assert.AreEqual(decideAllSamples, scoresAllSamples.ArgMax(dimension: 1).ToBoolean());
            Assert.AreEqual(decideAllSamples.ToInt32(), logLikelihoodsAllSamples.ArgMax(dimension: 1));
            Assert.AreEqual(decideAllSamples, probabilitiesAllSamples.ArgMax(dimension: 1).ToBoolean());

            double[] scoreAllSamples = target.Score(input);
            Assert.AreEqual(scoreAllSamples, scoresAllSamples.GetColumn(1));
            double[] logLikelihoodAllSamples = target.LogLikelihood(input);
            Assert.AreEqual(logLikelihoodAllSamples, logLikelihoodsAllSamples.GetColumn(1));
            double[] probabilityAllSamples = target.Probability(input);
            Assert.AreEqual(probabilityAllSamples, probabilitiesAllSamples.GetColumn(1));

            for (int i = 0; i < input.Length; i++)
            {
                double[] scoresOneSample = target.Scores(input[i]);
                Assert.AreEqual(scoresOneSample, scoresAllSamples[i]);

                double[] logLikelihoodsOneSample = target.LogLikelihoods(input[i]);
                Assert.AreEqual(logLikelihoodsOneSample, logLikelihoodsAllSamples[i]);

                double[] probabilitiesOneSample = target.Probabilities(input[i]);
                Assert.AreEqual(probabilitiesOneSample, probabilitiesAllSamples[i]);

                bool decideOneSample = target.Decide(input[i]);
                Assert.AreEqual(decideOneSample, decideAllSamples[i]);

                double scoreOneSample = target.Score(input[i]);
                Assert.AreEqual(scoreOneSample, scoreAllSamples[i]);

                double logLikelihoodOneSample = target.LogLikelihood(input[i]);
                Assert.AreEqual(logLikelihoodOneSample, logLikelihoodAllSamples[i]);

                double probabilityOneSample = target.Probability(input[i]);
                Assert.AreEqual(probabilityOneSample, probabilityAllSamples[i]);
            }

            bool[] decideScoresAllSamples         = null; target.Scores(input, ref decideScoresAllSamples);
            bool[] decideLogLikelihoodsAllSamples = null; target.LogLikelihoods(input, ref decideLogLikelihoodsAllSamples);
            Assert.AreEqual(decideScoresAllSamples, decideLogLikelihoodsAllSamples);
            bool[] decideProbabilitiesAllSamples = null; target.Probabilities(input, ref decideProbabilitiesAllSamples);
            Assert.AreEqual(decideScoresAllSamples, decideProbabilitiesAllSamples);

            bool[] decideScoreAllSamples = null; target.Score(input, ref decideScoreAllSamples);
            Assert.AreEqual(decideScoreAllSamples, decideScoresAllSamples);
            bool[] decideLogLikelihoodAllSamples = null; target.LogLikelihood(input, ref decideLogLikelihoodAllSamples);
            Assert.AreEqual(decideScoreAllSamples, decideLogLikelihoodAllSamples);
            bool[] decideProbabilityAllSamples = null; target.Probability(input, ref decideProbabilityAllSamples);
            Assert.AreEqual(decideScoreAllSamples, decideProbabilityAllSamples);


            for (int i = 0; i < input.Length; i++)
            {
                bool decideScoresOneSample; target.Scores(input[i], out decideScoresOneSample);
                Assert.AreEqual(decideScoresOneSample, decideScoresAllSamples[i]);

                bool decideLogLikelihoodsOneSample; target.LogLikelihoods(input[i], out decideLogLikelihoodsOneSample);
                Assert.AreEqual(decideLogLikelihoodsOneSample, decideLogLikelihoodsAllSamples[i]);

                bool decideProbabilitiesOneSample; target.Probabilities(input[i], out decideProbabilitiesOneSample);
                Assert.AreEqual(decideProbabilitiesOneSample, decideProbabilitiesAllSamples[i]);

                bool decideScoreOneSample; target.Score(input[i], out decideScoreOneSample);
                Assert.AreEqual(decideScoreOneSample, decideScoreAllSamples[i]);

                bool decideLogLikelihoodOneSample; target.LogLikelihood(input[i], out decideLogLikelihoodOneSample);
                Assert.AreEqual(decideLogLikelihoodOneSample, decideLogLikelihoodAllSamples[i]);

                bool decideProbabilityOneSample; target.Probability(input: input[i], decision: out decideProbabilityOneSample);
                Assert.AreEqual(decideProbabilityOneSample, decideProbabilityAllSamples[i]);
            }


            //bool[][] decidesScoresAllSamples = null; target.Scores(input, ref decidesScoresAllSamples);
            //bool[][] decidesLogLikelihoodsAllSamples = null; target.LogLikelihoods(input, ref decidesLogLikelihoodsAllSamples);
            //bool[][] decidesProbabilitiesAllSamples = null; target.Probabilities(input, ref decidesProbabilitiesAllSamples);


            //bool[][] decidesScoreAllSamples = null; target.Score(input, ref decidesScoreAllSamples);
            //bool[][] decidesLogLikelihoodAllSamples = null; target.LogLikelihood(input, ref decidesLogLikelihoodAllSamples);
            //bool[][] decidesProbabilityAllSamples = null; target.Probability(input, ref decidesProbabilityAllSamples);
        }
Example #23
0
        public void learn_test()
        {
            #region doc_learn
            // The Bag-Of-Words model can be used to extract finite-length feature
            // vectors from sequences of arbitrary length, like for example, texts:


            string[] texts =
            {
                @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas molestie malesuada 
                  nisi et placerat. Curabitur blandit porttitor suscipit. Nunc facilisis ultrices felis,
                  vitae luctus arcu semper in. Fusce ut felis ipsum. Sed faucibus tortor ut felis placerat
                  euismod. Vestibulum pharetra velit et dolor ornare quis malesuada leo aliquam. Aenean 
                  lobortis, tortor iaculis vestibulum dictum, tellus nisi vestibulum libero, ultricies 
                  pretium nisi ante in neque. Integer et massa lectus. Aenean ut sem quam. Mauris at nisl 
                  augue, volutpat tempus nisl. Suspendisse luctus convallis metus, vitae pretium risus 
                  pretium vitae. Duis tristique euismod aliquam",

                @"Sed consectetur nisl et diam mattis varius. Aliquam ornare tincidunt arcu eget adipiscing. 
                  Etiam quis augue lectus, vel sollicitudin lorem. Fusce lacinia, leo non porttitor adipiscing, 
                  mauris purus lobortis ipsum, id scelerisque erat neque eget nunc. Suspendisse potenti. Etiam 
                  non urna non libero pulvinar consequat ac vitae turpis. Nam urna eros, laoreet id sagittis eu,
                  posuere in sapien. Phasellus semper convallis faucibus. Nulla fermentum faucibus tellus in 
                  rutrum. Maecenas quis risus augue, eu gravida massa."
            };

            string[][] words = texts.Tokenize();

            // Create a new BoW with options:
            var codebook = new BagOfWords()
            {
                MaximumOccurance = 1 // the resulting vector will have only 0's and 1's
            };

            // Compute the codebook (note: this would have to be done only for the training set)
            codebook.Learn(words);


            // Now, we can use the learned codebook to extract fixed-length
            // representations of the different texts (paragraphs) above:

            // Extract a feature vector from the text 1:
            double[] bow1 = codebook.Transform(words[0]);

            // Extract a feature vector from the text 2:
            double[] bow2 = codebook.Transform(words[1]);

            // we could also have transformed everything at once, i.e.
            // double[][] bow = codebook.Transform(words);


            // Now, since we have finite length representations (both bow1 and bow2 should
            // have the same size), we can pass them to any classifier or machine learning
            // method. For example, we can pass them to a Logistic Regression Classifier to
            // discern between the first and second paragraphs

            // Lets create a Logistic classifier to separate the two paragraphs:
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4, // Let's set some convergence parameters
                Iterations     = 100,  // maximum number of iterations to perform
                Regularization = 0
            };

            // Now, we use the learning algorithm to learn the distinction between the two:
            LogisticRegression reg = learner.Learn(new[] { bow1, bow2 }, new[] { false, true });

            // Finally, we can predict using the classifier:
            bool c1 = reg.Decide(bow1); // Should be false
            bool c2 = reg.Decide(bow2); // Should be true
            #endregion

            Assert.AreEqual(bow1.Length, 99);
            Assert.AreEqual(bow2.Length, 99);

            Assert.AreEqual(bow1.Sum(), 67);
            Assert.AreEqual(bow2.Sum(), 63);

            Assert.IsFalse(c1);
            Assert.IsTrue(c2);
        }
Example #24
0
        /// <summary>
        /// The main application entry point.
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        public static void Main(string[] args)
        {
            // get data
            Console.WriteLine("Loading data....");
            var path    = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\california_housing.csv"));
            var housing = Frame.ReadCsv(path, separators: ",");

            housing = housing.Where(kv => ((decimal)kv.Value["median_house_value"]) < 500000);

            // shuffle the frame
            var rnd     = new Random();
            var indices = Enumerable.Range(0, housing.Rows.KeyCount).OrderBy(v => rnd.NextDouble());

            housing = housing.IndexRowsWith(indices).SortRowsByKey();

            // create the median_high_house_value feature
            housing.AddColumn("median_high_house_value",
                              housing["median_house_value"].Select(v => v.Value >= 265000 ? 1.0 : 0.0));

            // create one-hot vectors for longitude and latitude
            var vectors_long =
                from l in housing["longitude"].Values
                select Vector.Create <double>(
                    1,
                    (from b in RangeFromTo(-125, -114, 1)
                     select l >= b.Min && l < b.Max).ToArray());

            var vectors_lat =
                from l in housing["latitude"].Values
                select Vector.Create <double>(
                    1,
                    (from b in RangeFromTo(32, 43, 1)
                     select l >= b.Min && l < b.Max).ToArray());

            // multiply vectors and create columns
            var vectors_cross =
                vectors_long.Zip(vectors_lat, (lng, lat) => lng.Outer(lat));

            for (var i = 0; i < 12; i++)
            {
                for (var j = 0; j < 12; j++)
                {
                    housing.AddColumn($"location {i},{j}", from v in vectors_cross select v[i, j]);
                }
            }

            // set up model columns
            var columns = (from i in Enumerable.Range(0, 12)
                           from j in Enumerable.Range(0, 12)
                           select $"location {i},{j}").ToList();

            columns.Add("housing_median_age");
            columns.Add("total_rooms");
            columns.Add("total_bedrooms");
            columns.Add("population");
            columns.Add("households");
            columns.Add("median_income");

            // create training, validation, and test partitions
            var training   = housing.Rows[Enumerable.Range(0, 12000)];
            var validation = housing.Rows[Enumerable.Range(12000, 2500)];
            var test       = housing.Rows[Enumerable.Range(14500, 2500)];

            ////////////////////////////////////////////////////////////////////////
            // Without regularization
            ////////////////////////////////////////////////////////////////////////

            // train the model
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                MaxIterations  = 50,
                Regularization = 0
            };
            var regression = learner.Learn(
                training.Columns[columns].ToArray2D <double>().ToJagged(),
                training["median_high_house_value"].Values.ToArray());

            // display training results
            Console.WriteLine("TRAINING WITHOUT REGULARIZATION");
            Console.WriteLine($"Weights:     {regression.Weights.ToString<double>("0.00")}");
            Console.WriteLine($"Intercept:   {regression.Intercept}");
            Console.WriteLine();

            // plot a histogram of the nonzero weights
            var histogram = new Histogram();

            histogram.Compute(regression.Weights, 0.1); // set to 1.0 when regularization is disabled

            // draw the histogram
            var x = new List <double>();
            var y = new List <double>();

            for (int i = 0; i < histogram.Values.Length; i++)
            {
                var xcor = histogram.Bins[i].Range.Min;
                x.AddRange(from n in Enumerable.Range(0, histogram.Values[i]) select xcor);
                y.AddRange(from n in Enumerable.Range(0, histogram.Values[i]) select n * 1.0);
            }
            var plot = new Scatterplot("Without Regularization", "prediction", "count");

            plot.Compute(x.ToArray(), y.ToArray());
            ScatterplotBox.Show(plot);

            ////////////////////////////////////////////////////////////////////////
            // With regularization
            ////////////////////////////////////////////////////////////////////////

            // train the model
            learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                MaxIterations  = 50,
                Regularization = 50
            };
            regression = learner.Learn(
                training.Columns[columns].ToArray2D <double>().ToJagged(),
                training["median_high_house_value"].Values.ToArray());

            // display training results
            Console.WriteLine("TRAINING WITH REGULARIZATION");
            Console.WriteLine($"Weights:     {regression.Weights.ToString<double>("0.00")}");
            Console.WriteLine($"Intercept:   {regression.Intercept}");
            Console.WriteLine();

            // plot a histogram of the nonzero weights
            histogram = new Histogram();
            histogram.Compute(regression.Weights, 0.1); // set to 1.0 when regularization is disabled

            // draw the histogram
            x = new List <double>();
            y = new List <double>();
            for (int i = 0; i < histogram.Values.Length; i++)
            {
                var xcor = histogram.Bins[i].Range.Min;
                x.AddRange(from n in Enumerable.Range(0, histogram.Values[i]) select xcor);
                y.AddRange(from n in Enumerable.Range(0, histogram.Values[i]) select n * 1.0);
            }
            plot = new Scatterplot("With Regularization", "prediction", "count");
            plot.Compute(x.ToArray(), y.ToArray());
            ScatterplotBox.Show(plot);

            Console.ReadLine();
        }
Example #25
0
        /// <summary>
        /// Run the lesson.
        /// </summary>
        public static void Run()
        {
            // get data
            Console.WriteLine("Loading data....");
            var path    = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\california_housing.csv"));
            var housing = Frame.ReadCsv(path, separators: ",");

            housing = housing.Where(kv => ((decimal)kv.Value["median_house_value"]) < 500000);

            // create the median_high_house_value feature
            housing.AddColumn("median_high_house_value",
                              housing["median_house_value"].Select(v => v.Value >= 265000 ? 1.0 : 0.0));

            // shuffle the frame
            var rnd     = new Random();
            var indices = Enumerable.Range(0, housing.Rows.KeyCount).OrderBy(v => rnd.NextDouble());

            housing = housing.IndexRowsWith(indices).SortRowsByKey();

            // create training, validation, and test frames
            var training   = housing.Rows[Enumerable.Range(0, 12000)];
            var validation = housing.Rows[Enumerable.Range(12000, 2500)];
            var test       = housing.Rows[Enumerable.Range(14500, 2500)];

            // build the list of features we're going to use
            var columns = new string[] {
                "latitude",
                "longitude",
                "housing_median_age",
                "total_rooms",
                "total_bedrooms",
                "population",
                "households",
                "median_income"
            };

            // train the model using a logistic regressor
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                MaxIterations = 100
            };
            var regression = learner.Learn(
                training.Columns[columns].ToArray2D <double>().ToJagged(),
                training["median_high_house_value"].Values.ToArray());

            // get probabilities
            var features_validation = validation.Columns[columns].ToArray2D <double>().ToJagged();
            var label_validation    = validation["median_high_house_value"].Values.ToArray();
            var probabilities       = regression.Probability(features_validation);

            // calculate the histogram of probabilities
            var histogram = new Histogram();

            histogram.Compute(probabilities, 0.05);

            // draw the histogram
            Plot(histogram, "Probability histogram", "prediction", "count");

            // get predictions and actuals
            var predictions = regression.Decide(features_validation);
            var actuals     = label_validation.Select(v => v == 1.0 ? true : false).ToArray();

            // create confusion matrix
            var confusion = new ConfusionMatrix(predictions, actuals);

            // display classification scores
            Console.WriteLine($"True Positives:  {confusion.TruePositives}");
            Console.WriteLine($"True Negatives:  {confusion.TrueNegatives}");
            Console.WriteLine($"False Positives: {confusion.FalsePositives}");
            Console.WriteLine($"False Negatives: {confusion.FalseNegatives}");
            Console.WriteLine();

            // display accuracy, precision, and recall
            Console.WriteLine($"Accuracy:        {confusion.Accuracy}");
            Console.WriteLine($"Precision:       {confusion.Precision}");
            Console.WriteLine($"Recall:          {confusion.Recall}");
            Console.WriteLine();

            // display TPR and FPR
            Console.WriteLine($"TPR:             {confusion.Sensitivity}");
            Console.WriteLine($"FPR:             {confusion.FalsePositiveRate}");
            Console.WriteLine();

            // calculate roc curve
            var roc = new ReceiverOperatingCharacteristic(
                actuals,
                predictions.Select(v => v ? 1 : 0).ToArray());

            roc.Compute(100);

            // generate the scatter plot
            var rocPlot = roc.GetScatterplot(true);

            // show roc curve
            Plot(rocPlot);

            // show the auc
            Console.WriteLine($"AUC:             {roc.Area}");
        }
Example #26
0
        private void regression(List <Cell> samplePoints)
        {
            // 构造输入输出数据集

            // 样本数目
            int COUNT = samplePoints.Count;

            // 构造输入和输出数据集
            double[][] inputs  = new double[COUNT][];
            bool[]     outputs = new bool[COUNT];
            for (int i = 0; i < COUNT; i++)
            {
                Cell cell = samplePoints[i];
                int  pos  = cell.row * width + cell.col;
                inputs[i] = (from buffer in driveBuffers
                             select buffer[pos]).ToArray <double>();
                outputs[i] = cell.type;
            }



            var learner = new IterativeReweightedLeastSquares <Accord.Statistics.Models.Regression.LogisticRegression>()
            {
                Tolerance             = 1e-8, // 收敛参数
                Iterations            = 20,   // 最大循环数目
                Regularization        = 0,
                ComputeStandardErrors = true
            };


            Accord.Statistics.Models.Regression.LogisticRegression regression = learner.Learn(inputs, outputs);


            // 输出 odds
            StringBuilder strb = new StringBuilder();

            for (int i = 0; i <= inputs[0].Length; i++)
            {
                strb.AppendLine(" " + i + " : " + regression.GetOddsRatio(i));
            }
            updateConsoleEvent(strb.ToString());

            // 输出 weights
            StringBuilder strw = new StringBuilder();

            strw.AppendLine("权重系数:");
            strw.AppendLine("截距: " + regression.Intercept.ToString());
            var weights = regression.Weights;

            for (int i = 0; i < weights.Length; i++)
            {
                strw.AppendLine("权重" + (i + 1) + ":" + weights[i]);
            }
            updateConsoleEvent(strw.ToString());

            double[] result  = new double[width * height];
            double   minProp = double.MaxValue;

            double[] minInput = null;
            for (int row = 0; row < height; row++)
            {
                for (int col = 0; col < width; col++)
                {
                    int pos = row * width + col;
                    if (beginBuffer[pos] < 0 || !IsValid(pos))
                    {
                        result[pos] = this.landUse.NullInfo.LandUseTypeValue;
                        continue;
                    }
                    double[] input = (from buffer in driveBuffers
                                      select buffer[pos]).ToArray <double>();
                    double prop = regression.Probability(input);
                    if (prop < minProp)
                    {
                        minProp  = prop;
                        minInput = input;
                    }
                    result[pos] = prop;
                }
            }



            // 新建 GDAL dataset
            OSGeo.GDAL.Driver  driver  = OSGeo.GDAL.Gdal.GetDriverByName("GTIFF");
            OSGeo.GDAL.Dataset dataset = driver.Create(this.ResultLayerName, width, height, 1, OSGeo.GDAL.DataType.GDT_Float64, null);

            dataset.WriteRaster(0, 0, width, height, result, width, height, 1, new int[1] {
                1
            }, 0, 0, 0);
            dataset.FlushCache();
        }
Example #27
0
        public static void Execute()
        {
            double[][] input =
            {
                new double[] { 55, 0 },
                new double[] { 28, 0 },
                new double[] { 65, 0 },
                new double[] { 46, 0 },
                new double[] { 86, 0 },
                new double[] { 56, 0 },
                new double[] { 85, 0 },
                new double[] { 33, 0 },
                new double[] { 21, 0 },
                new double[] { 42, 0 },
            };

            double[] output =
            {
                0, 0, 0, 1, 1, 1, 0, 0, 0, 1
            };
            LogisticRegression regression = new LogisticRegression(2);
            var    trainer = new IterativeReweightedLeastSquares(regression);
            double delta   = 0;

            do
            {
                // Perform an iteration
                delta = trainer.Run(input, output);
            } while (delta > 0.001);

            var b1 = regression.Coefficients[1];
            var b2 = regression.Coefficients[2];
            var b0 = regression.Intercept;

            System.Console.WriteLine(b0);
            System.Console.WriteLine(b1);
            System.Console.WriteLine(b2);

            var func = new Func <double, double, double>((x1, x2) =>
            {
                var result = 1 / (1 + Math.Exp(-b0 - b1 * x1 - b2 * x2));
                return(result);
            });
            var age     = 79;
            var smoking = 0;
            var r       = func(age, smoking);

            System.Console.WriteLine("input x [age:{0}, smoking:{1}] is {2}", age, smoking, r);

            LogisticRegression LR = new LogisticRegression();

            LR.NumberOfInputs = 1;
            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4, // Let's set some convergence parameters
                Iterations     = 100,  // maximum number of iterations to perform
                Regularization = 0
            };

            LR = learner.Learn(input, output);
            System.Console.WriteLine(LR.Intercept);
            System.Console.WriteLine(LR.GetOddsRatio(1) - 1);
            System.Console.WriteLine(LR.GetOddsRatio(2) - 1);
            double [] test = new double[] { 79, 0 };
            System.Console.WriteLine(LR.Probability(test));
        }
        public void learn_new_mechanism()
        {
            #region doc_log_reg_1
            // Suppose we have the following data about some patients.
            // The first variable is continuous and represent patient
            // age. The second variable is dichotomic and give whether
            // they smoke or not (This is completely fictional data).

            // We also know if they have had lung cancer or not, and
            // we would like to know whether smoking has any connection
            // with lung cancer (This is completely fictional data).

            double[][] input =
            {                           // age, smokes?, had cancer?
                new double[] { 55, 0 }, // false - no cancer
                new double[] { 28, 0 }, // false
                new double[] { 65, 1 }, // false
                new double[] { 46, 0 }, // true  - had cancer
                new double[] { 86, 1 }, // true
                new double[] { 56, 1 }, // true
                new double[] { 85, 0 }, // false
                new double[] { 33, 0 }, // false
                new double[] { 21, 1 }, // false
                new double[] { 42, 1 }, // true
            };

            bool[] output = // Whether each patient had lung cancer or not
            {
                false, false, false, true, true, true, false, false, false, true
            };


            // To verify this hypothesis, we are going to create a logistic
            // regression model for those two inputs (age and smoking), learned
            // using a method called "Iteratively Reweighted Least Squares":

            var learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4, // Let's set some convergence parameters
                Iterations     = 100,  // maximum number of iterations to perform
                Regularization = 0
            };

            // Now, we can use the learner to finally estimate our model:
            LogisticRegression regression = learner.Learn(input, output);

            // At this point, we can compute the odds ratio of our variables.
            // In the model, the variable at 0 is always the intercept term,
            // with the other following in the sequence. Index 1 is the age
            // and index 2 is whether the patient smokes or not.

            // For the age variable, we have that individuals with
            //   higher age have 1.021 greater odds of getting lung
            //   cancer controlling for cigarette smoking.
            double ageOdds = regression.GetOddsRatio(1); // 1.0208597028836701

            // For the smoking/non smoking category variable, however, we
            //   have that individuals who smoke have 5.858 greater odds
            //   of developing lung cancer compared to those who do not
            //   smoke, controlling for age (remember, this is completely
            //   fictional and for demonstration purposes only).
            double smokeOdds = regression.GetOddsRatio(2); // 5.8584748789881331

            // If we would like to use the model to predict a probability for
            // each patient regarding whether they are at risk of cancer or not,
            // we can use the Probability function:

            double[] scores = regression.Probability(input);

            // Finally, if we would like to arrive at a conclusion regarding
            // each patient, we can use the Decide method, which will transform
            // the probabilities (from 0 to 1) into actual true/false values:

            bool[] actual = regression.Decide(input);
            #endregion

            double[] expected =
            {
                0.21044171560168326,
                0.13242527535212373,
                0.65747803433771812,
                0.18122484822324372,
                0.74755661773156912,
                0.61450041841477232,
                0.33116705418194975,
                0.14474110902457912,
                0.43627109657399382,
                0.54419383282533118
            };

            for (int i = 0; i < scores.Length; i++)
            {
                Assert.AreEqual(expected[i], scores[i], 1e-8);
            }

            double[] transform = regression.Transform(input, scores);
            for (int i = 0; i < scores.Length; i++)
            {
                Assert.AreEqual(expected[i], transform[i], 1e-8);
            }

            Assert.AreEqual(1.0208597028836701, ageOdds, 1e-10);
            Assert.AreEqual(5.8584748789881331, smokeOdds, 1e-6);

            Assert.AreEqual(-2.4577464307294092, regression.Intercept, 1e-8);
            Assert.AreEqual(-2.4577464307294092, regression.Coefficients[0], 1e-8);
            Assert.AreEqual(0.020645118265359252, regression.Coefficients[1], 1e-10);
            Assert.AreEqual(1.7678893101571855, regression.Coefficients[2], 1e-8);

            Assert.IsFalse(actual[0]);
            Assert.IsFalse(actual[1]);
            Assert.IsTrue(actual[2]);
            Assert.IsFalse(actual[3]);
            Assert.IsTrue(actual[4]);
            Assert.IsTrue(actual[5]);
            Assert.IsFalse(actual[6]);
            Assert.IsFalse(actual[7]);
            Assert.IsFalse(actual[8]);
            Assert.IsTrue(actual[9]);
        }
Example #29
0
        public double[] GetResult()
        {
            // 采样
            List <Cell> samplePoints = getSample(this.NumberOfSample);

            // 样本数目
            int COUNT = samplePoints.Count;

            // 构造输入和输出数据集
            double[][] inputs  = new double[COUNT][];
            bool[]     outputs = new bool[COUNT];
            for (int i = 0; i < COUNT; i++)
            {
                Cell cell = samplePoints[i];
                int  pos  = cell.row * width + cell.col;
                inputs[i] = (from buffer in driveBuffers
                             select buffer[pos]).ToArray <double>();
                outputs[i] = cell.type;
            }



            var learner = new IterativeReweightedLeastSquares <Accord.Statistics.Models.Regression.LogisticRegression>()
            {
                Tolerance             = 1e-8, // 收敛参数
                Iterations            = 20,   // 最大循环数目
                Regularization        = 0,
                ComputeStandardErrors = true
            };


            Accord.Statistics.Models.Regression.LogisticRegression regression = learner.Learn(inputs, outputs);


            //// 输出 odds
            //StringBuilder strb = new StringBuilder();
            //for (int i = 0; i <= inputs[0].Length; i++)
            //{
            //    strb.AppendLine(" " + i + " : " + regression.GetOddsRatio(i));
            //}
            //updateConsoleEvent(strb.ToString());

            //// 输出 weights
            //StringBuilder strw = new StringBuilder();
            //strw.AppendLine("权重系数:");
            //strw.AppendLine("截距: " + regression.Intercept.ToString());
            //var weights = regression.Weights;
            //for (int i = 0; i < weights.Length; i++)
            //{
            //    strw.AppendLine("权重" + (i + 1) + ":" + weights[i]);
            //}
            //updateConsoleEvent(strw.ToString());

            double[] result  = new double[width * height];
            double   minProp = double.MaxValue;

            double[] minInput = null;
            for (int row = 0; row < height; row++)
            {
                for (int col = 0; col < width; col++)
                {
                    int pos = row * width + col;
                    if (beginBuffer[pos] < 0 || !IsValid(pos))
                    {
                        result[pos] = this.landUse.NullInfo.LandUseTypeValue;
                        continue;
                    }
                    double[] input = (from buffer in driveBuffers
                                      select buffer[pos]).ToArray <double>();
                    double prop = regression.Probability(input);
                    if (prop < minProp)
                    {
                        minProp  = prop;
                        minInput = input;
                    }
                    result[pos] = prop;
                }
            }
            return(result);
        }
Example #30
0
        /// <summary>
        /// Uses data from <paramref name="fileName">fileName</paramref> to train a logistic regression model./>
        /// </summary>
        /// <param name="fileName">The name of the data file.</param>
        /// <returns>A string to print giving information about the weights and odds ratios.</returns>
        public static string Learn(string fileName)
        {
            //Read all inputs and outputs from training file.
            string[]   lines   = File.ReadAllLines("Logistic Regression Model/data/" + fileName + ".txt");
            double[][] inputs  = new double[lines.Length][];
            int[]      outputs = new int[lines.Length];

            for (int a = 0; a < lines.Length; a++)
            {
                string[] split = lines[a].Split(':');

                //Dynamically get variables from file.
                string[] scores = split[1].Split('&');
                inputs[a] = new double[scores.Length];
                for (int b = 0; b < scores.Length; b++)
                {
                    inputs[a][b] = double.Parse(scores[b]);
                }

                outputs[a] = int.Parse(split[2]);
            }

            //Set up Accord.NET learner.
            IterativeReweightedLeastSquares <LogisticRegression> learner = new IterativeReweightedLeastSquares <LogisticRegression>()
            {
                Tolerance      = 1e-4,
                MaxIterations  = 100,
                Regularization = 1e-10
            };

            //Shuffle the input and output pairs to eliminate some inherent bias from
            //training data.
            Dictionary <double[], int> map = inputs.Zip(outputs, (arg1, arg2) => new { arg1, arg2 }).ToDictionary(x => x.arg1, x => x.arg2);

            map.Shuffle();
            inputs  = map.Keys.ToArray();
            outputs = map.Values.ToArray();


            //Train Regression
            LogisticRegression regression = learner.Learn(inputs, outputs.ToBoolArray());



            //Save to a Model file.
            int counter = 0;

            while (File.Exists("Logistic Regression Model/models/Model-" + counter + ".txt"))
            {
                counter++;
            }

            //Create a file writer
            FileStream   fs     = File.Create("Logistic Regression Model/models/Model-" + counter + ".txt");
            StreamWriter writer = new StreamWriter(fs);

            //Print the weights
            string result = "Weights: " + regression.Weights.GetString() + "\n";

            //Write lines.
            writer.WriteLine(regression.Weights.Append(regression.Intercept).ToArray().GetString());
            for (int c = 0; c < regression.Weights.Length; c++)
            {
                writer.WriteLine(regression.GetOddsRatio(c));
                result += "Odds Ratio " + c + ": " + regression.GetOddsRatio(c) + "\n";
            }

            //Get Loss values.
            double[] actual   = new double[inputs.Length];
            double[] expected = new double[outputs.Length];
            for (int a = 0; a < actual.Length; a++)
            {
                actual[a]   = regression.Probability(inputs[a]);
                expected[a] = outputs[a];
            }

            //Calculate and print square loss.
            string loss = "Loss: " + new SquareLoss(expected)
            {
                Mean = true,
                Root = true
            }.Loss(actual);

            result += loss + "\n";
            writer.WriteLine(loss);

            Console.WriteLine("\n\n" + loss);


            //Calculate and print R-squared Loss
            string r2 = "R2: " + new RSquaredLoss(inputs[0].Length, expected).Loss(actual);

            result += r2;
            writer.WriteLine(r2);

            //Cleanup
            writer.Close();
            writer.Dispose();

            fs.Close();
            fs.Dispose();

            Console.WriteLine("Model trained successfully!");
            Console.WriteLine("\nEvaluating...\n");

            //Get the VIFs
            float[] VIFs = CalculateVIFs(inputs);

            //Log it
            for (int a = 0; a < VIFs.Length; a++)
            {
                Logger.Log("Variance Inflation Factor #" + a + ": " + VIFs[a]);
            }

            return(result);
        }