A Logistic Regression Model object
Inheritance: Model, IClassifier
Exemplo n.º 1
0
        /// <summary>Generate Logistic Regression model based on a set of examples.</summary>
        /// <param name="x">The Matrix to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <returns>Model.</returns>
        public override IModel Generate(Matrix x, Vector y)
        {
            // create initial theta
            Matrix copy = x.Copy();

            copy = PreProcessing.FeatureDimensions.IncreaseDimensions(copy, this.PolynomialFeatures);

            // add intercept term
            copy = copy.Insert(Vector.Ones(copy.Rows), 0, VectorType.Col);

            Vector theta = Vector.Ones(copy.Cols);

            var run = Math.Optimization.GradientDescent.Run(theta, copy, y, this.MaxIterations, this.LearningRate, new Math.Functions.Cost.LogisticCostFunction(),
                                                            this.Lambda, new Math.Functions.Regularization.Regularization());

            LogisticRegressionModel model = new LogisticRegressionModel()
            {
                Descriptor         = this.Descriptor,
                Theta              = run.Item2,
                LogisticFunction   = new Math.Functions.Logistic(),
                PolynomialFeatures = this.PolynomialFeatures
            };

            return(model);
        }
        /// <summary>Generate Logistic Regression model based on a set of examples.</summary>
        /// <param name="x">The Matrix to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <returns>Model.</returns>
        public override IModel Generate(Matrix x, Vector y)
        {
            // create initial theta
            Matrix copy = x.Copy();

            copy = PreProcessing.FeatureDimensions.IncreaseDimensions(copy, PolynomialFeatures);

            // add intercept term
            copy = copy.Insert(Vector.Ones(copy.Rows), 0, VectorType.Col);

            Vector theta = Vector.Ones(copy.Cols);

            var run = numl.Math.Optimization.GradientDescent.Run(theta, copy, y, this.MaxIterations, this.LearningRate, new numl.Math.Functions.Cost.LogisticCostFunction(), 
                this.Lambda, new numl.Math.Functions.Regularization.Regularization());

            LogisticRegressionModel model = new LogisticRegressionModel()
            {
                Descriptor = this.Descriptor,
                Theta = run.Item2,
                LogisticFunction = new Math.Functions.Logistic(),
                PolynomialFeatures = this.PolynomialFeatures
            };

            return model;
        }
Exemplo n.º 3
0
        public void Logistic_Regression_Test_Prediction()
        {
            Matrix m = new[,] 
            {{ 8, 1, 6 },
             { 3, 5 ,7 },
             { 4, 9, 2 }};

            var model = new LogisticRegressionModel() { 
                LogisticFunction = new Math.Functions.Logistic(), 
                Theta = new Vector(new double[] { 1, 2, 1, -9 }) 
            };

            var p = model.Predict(new Vector(new double[] { 8, 1, 6 }));

            Assert.AreEqual(1d, p);
        }
Exemplo n.º 4
0
        /// <summary>Generate Logistic Regression model based on a set of examples.</summary>
        /// <param name="X">The Matrix to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <returns>Model.</returns>
        public override IModel Generate(Matrix X, Vector y)
        {
            X = IncreaseDimensions(X, PolynomialFeatures);

            Preprocess(X);

            // guarantee 1/0 based label vector
            y = y.ToBinary(f => f == 1d, falseValue: 0d);

            // add intercept term
            X = X.Insert(Vector.Ones(X.Rows), 0, VectorType.Col, false);

            var theta = Vector.Rand(X.Cols);

            // run gradient descent
            var optimizer = new Optimizer(theta, MaxIterations, LearningRate)
            {
                CostFunction = new LogisticCostFunction
                {
                    X                = X,
                    Y                = y,
                    Lambda           = Lambda,
                    Regularizer      = new L2Regularizer(),
                    LogisticFunction = LogisticFunction
                }
            };

            optimizer.Run();

            var model = new LogisticRegressionModel
            {
                Descriptor         = Descriptor,
                NormalizeFeatures  = NormalizeFeatures,
                FeatureNormalizer  = FeatureNormalizer,
                FeatureProperties  = FeatureProperties,
                Theta              = optimizer.Properties.Theta,
                LogisticFunction   = LogisticFunction,
                PolynomialFeatures = PolynomialFeatures
            };

            return(model);
        }
        /// <summary>Generate Logistic Regression model based on a set of examples.</summary>
        /// <param name="X">The Matrix to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <returns>Model.</returns>
        public override IModel Generate(Matrix X, Vector y)
        {
            X = IncreaseDimensions(X, this.PolynomialFeatures);

            this.Preprocess(X);

            // guarantee 1/0 based label vector
            y = y.ToBinary(f => f == 1d, falseValue: 0d);

            // add intercept term
            X = X.Insert(Vector.Ones(X.Rows), 0, VectorType.Col, false);

            Vector theta = Vector.Rand(X.Cols);

            // run gradient descent
            var optimizer = new numl.Math.Optimization.Optimizer(theta, this.MaxIterations, this.LearningRate)
            {
                CostFunction = new numl.Math.Functions.Cost.LogisticCostFunction()
                {
                    X = X,
                    Y = y,
                    Lambda = this.Lambda,
                    Regularizer = new numl.Math.Functions.Regularization.L2Regularizer(),
                    LogisticFunction = this.LogisticFunction
                }
            };

            optimizer.Run();

            LogisticRegressionModel model = new LogisticRegressionModel()
            {
                Descriptor = this.Descriptor,
                NormalizeFeatures = base.NormalizeFeatures,
                FeatureNormalizer = base.FeatureNormalizer,
                FeatureProperties = base.FeatureProperties,
                Theta = optimizer.Properties.Theta,
                LogisticFunction = this.LogisticFunction,
                PolynomialFeatures = this.PolynomialFeatures
            };

            return model;
        }