Generate() public method

Generate Linear Regression model based on a set of examples.
public Generate ( Matrix X, Vector y ) : IModel
X Matrix The Matrix to process.
y numl.Math.LinearAlgebra.Vector The Vector to process.
return IModel
        public void Save_And_Load_LinearRegression()
        {
            Matrix x = new [,]
                {
                    {2104, 3},
                    {1600, 3},
                    {2400, 3},
                    {1416, 2},
                    {3000, 4},
                    {1985, 4},
                    {1534, 3},
                    {1427, 3},
                    {1380, 3},
                    {1494, 3}
                };

            Vector y = new[] 
                { 
                    399900,
                    329900,
                    369000,
                    232000,
                    539900,
                    299900,
                    314900,
                    198999,
                    212000,
                    242500
                };

            LinearRegressionGenerator generator = new LinearRegressionGenerator() { LearningRate = 0.01, MaxIterations = 400, Lambda = 1 };
            var model = generator.Generate(x.Copy(), y.Copy()) as LinearRegressionModel;

            Serialize(model);

            var lmodel = Deserialize<LinearRegressionModel>();
            Assert.AreEqual(model.Theta, lmodel.Theta);
        }
Example #2
0
        public void Linear_Regression_Test_House_Predictions_Regularized()
        {
            // test house prices based on ft-sq and no# bedrooms
            Matrix x = new [,]
                {
                    {2104, 3},
                    {1600, 3},
                    {2400, 3},
                    {1416, 2},
                    {3000, 4},
                    {1985, 4},
                    {1534, 3},
                    {1427, 3},
                    {1380, 3},
                    {1494, 3}
                };

            Vector y = new[] 
                { 
                    399900,
                    329900,
                    369000,
                    232000,
                    539900,
                    299900,
                    314900,
                    198999,
                    212000,
                    242500
                };

            LinearRegressionGenerator generator = new LinearRegressionGenerator() { LearningRate = 0.01, MaxIterations = 400, Lambda = 1 };
            var model = generator.Generate(x.Copy(), y.Copy());
            var priceGrad = model.Predict(new Vector(new double[] { 1650, 3 }));

            double actualGrad = 280942d;

            Assert.AreEqual(actualGrad, System.Math.Round(priceGrad, 0));
        }
Example #3
0
        public void Linear_Regression_Test_House_Predictions_Normal()
        {
            // test house prices based on ft-sq and no# bedrooms
            Matrix x = new [,]
                {
                    {2104, 3},
                    {1600, 3},
                    {2400, 3},
                    {1416, 2},
                    {3000, 4},
                    {1985, 4},
                    {1534, 3},
                    {1427, 3},
                    {1380, 3},
                    {1494, 3}
                };

            Vector y = new[]
                {
                    399900,
                    329900,
                    369000,
                    232000,
                    539900,
                    299900,
                    314900,
                    198999,
                    212000,
                    242500
                };

            LinearRegressionGenerator generator = new LinearRegressionGenerator() { LearningRate = 0.01, MaxIterations = 400, Lambda = 0, NormalizeFeatures = true };
            var model = generator.Generate(x.Copy(), y.Copy());
            var priceEqns = model.Predict(new Vector(new double[] { 1650, 3 }));

            // CK 150929: increased due to improvements in optimisation
            double actualEqns = 295107.0d;

            Almost.Equal(actualEqns, System.Math.Round(priceEqns, 0), 5000);
        }