A linear regression generator.
Inheritance: Generator
示例#1
0
        public void Linear_Regression_Learner_Test()
        {
            var datum = new[]
            {
                new { X = 1.0, Y = 1.0 },
                new { X = 2.0, Y = 2.0 },
                new { X = 3.0, Y = 1.3 },
                new { X = 4.0, Y = 3.75 },
                new { X = 5.0, Y = 5.25 }
            };

            var d = Descriptor.New("DATUM")
                              .With("X").As(typeof(double))
                              .Learn("Y").As(typeof(double));

            var generator = new LinearRegressionGenerator() { Descriptor = d };
            var model = Learner.Learn(datum, 0.9, 5, generator);

            Assert.True(0.8 >= model.Accuracy);
        }
示例#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));
        }
        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);
        }
示例#4
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);
        }