Exemplo n.º 1
0
        public void TestRidgeShapes()
        {
            var       rng       = new Random(0);
            const int nSamples  = 5;
            const int nFeatures = 10;
            Matrix    x         = DenseMatrix.CreateRandom(nSamples, nFeatures, new Normal {
                RandomSource = rng
            });
            Vector y = DenseVector.CreateRandom(nSamples, new Normal {
                RandomSource = rng
            });

            Matrix Y = DenseMatrix.OfColumns(y.Count, 2, new[] { y, y.Add(1) });

            var ridge = new RidgeRegression();

            ridge.Fit(x, y);
            Assert.AreEqual(ridge.Coef.Shape(), Tuple.Create(1, nFeatures));
            Assert.AreEqual(ridge.Intercept.Count, 1);

            ridge.Fit(x, MatrixExtensions.ToColumnMatrix(y));
            Assert.AreEqual(ridge.Coef.Shape(), Tuple.Create(1, nFeatures));
            Assert.AreEqual(ridge.Intercept.Count, 1);

            ridge.Fit(x, Y);
            Assert.AreEqual(ridge.Coef.Shape(), Tuple.Create(2, nFeatures));
            Assert.AreEqual(ridge.Intercept.Count, 2);
        }
Exemplo n.º 2
0
        public void TestToyRidgeObject()
        {
            var x = DenseMatrix.OfArray(new double[, ] {
                { 1 }, { 2 }
            });
            var y   = new DenseVector(new double[] { 1, 2 });
            var clf = new RidgeRegression(alpha: 0.0);

            clf.Fit(x, y);
            var xTest = DenseMatrix.OfArray(new double[, ] {
                { 1 }, { 2 }, { 3 }, { 4 }
            });

            Assert.AreEqual((clf.Predict(xTest) -
                             DenseMatrix.OfArray(new double[, ] {
                { 1 }, { 2 }, { 3 }, { 4 }
            })).FrobeniusNorm(), 0.0, 1e-10);

            Assert.AreEqual(clf.Coef.RowCount, 1);

            var y1 = DenseMatrix.OfColumns(y.Count, 2, new[] { y, y });

            clf.Fit(x, y1);

            //todo: what does this test do?
        }
Exemplo n.º 3
0
        private double?TestRidgeDiabetes(Func <Matrix <double>, Matrix <double> > filter)
        {
            var ridge = new RidgeRegression(fitIntercept: false);

            ridge.Fit(filter(xDiabetes), yDiabetes);
            return(Math.Round(ridge.Score(filter(xDiabetes), yDiabetes), 5));
        }
Exemplo n.º 4
0
        public void TestRidge()
        {
            var rng = new Random(0);
            const double alpha = 1.0;

            foreach (var solver in new[] { RidgeSolver.Svd, RidgeSolver.DenseCholesky, RidgeSolver.Lsqr })
            {
                // With more samples than features
                int nSamples = 6;
                int nFeatures = 5;
                var normal = new Normal { RandomSource = rng };
                Vector y = DenseVector.CreateRandom(nSamples, normal);
                Matrix x = DenseMatrix.CreateRandom(nSamples, nFeatures, normal);

                var ridge = new RidgeRegression(alpha: alpha, solver: solver);
                ridge.Fit(x, y);
                Assert.AreEqual(ridge.Coef.Row(0).Count, x.ColumnCount);
                Assert.IsTrue(ridge.Score(x, y) > 0.47);

                ridge.Fit(x, y, sampleWeight: DenseVector.Create(nSamples, i => 1.0));
                Assert.IsTrue(ridge.Score(x, y) > 0.47);

                // With more features than samples
                nSamples = 5;
                nFeatures = 10;
                y = DenseVector.CreateRandom(nSamples, normal);
                x = DenseMatrix.CreateRandom(nSamples, nFeatures, normal);
                ridge = new RidgeRegression(alpha: alpha, solver: solver);
                ridge.Fit(x, y);
                Assert.IsTrue(ridge.Score(x, y) > 0.9);

                ridge.Fit(x, y, sampleWeight: DenseVector.Create(nSamples, i => 1.0));
                Assert.IsTrue(ridge.Score(x, y) > 0.9);
            }
        }
Exemplo n.º 5
0
        public void TestRidgeSampleWeights()
        {
            var          rng   = new Random(0);
            const double alpha = 1.0;

            foreach (var solver in new[] { RidgeSolver.Svd, RidgeSolver.DenseCholesky, RidgeSolver.Lsqr })
            {
                var tests = new[] { new[] { 6, 5 }, new[] { 5, 10 } };
                foreach (var test in tests)
                {
                    int    nSamples  = test[0];
                    int    nFeatures = test[1];
                    Vector y         = DenseVector.CreateRandom(nSamples, new Normal {
                        RandomSource = rng
                    });
                    Matrix x = DenseMatrix.CreateRandom(nSamples, nFeatures, new Normal {
                        RandomSource = rng
                    });
                    Vector sampleWeight = DenseVector.CreateRandom(nSamples, new ContinuousUniform(1, 2));

                    var ridgeRegression = new RidgeRegression(alpha: alpha, solver: solver, fitIntercept: false);
                    ridgeRegression.Fit(x, y, sampleWeight);
                    var coefs = ridgeRegression.Coef.Column(0);
                    // Sample weight can be implemented via a simple rescaling
                    // for the square loss
                    var regression2 = new RidgeRegression(alpha, solver: solver, fitIntercept: false);
                    regression2.Fit(
                        x.MulColumnVector(sampleWeight.Sqrt()),
                        y.PointwiseMultiply(sampleWeight.Sqrt()));
                    var coefs2 = regression2.Coef.Column(0);

                    Assert.IsTrue(coefs.AlmostEquals(coefs2));
                }
            }
        }
Exemplo n.º 6
0
        public void FitPredictLSQR_WithoutInterceptAndWithRandomData_ReturnsCorrectResult()
        {
            var(x, y, theta) = Generate(intercept: false);
            var(xTrain, yTrain, xTest, yTest) = ModelValidation.TrainTestSplit(x, y, 0.5);

            var estimator  = new RidgeRegression(intercept: false, method: RidgeRegressionMethod.LSQR).Fit(x, y);
            var yTrainPred = estimator.Predict(xTrain);
            var yTestPred  = estimator.Predict(xTest);

            FloatingPointAssert.Equal(theta, estimator.Weights, 1e-3);
            FloatingPointAssert.Equal(yTrain, yTrainPred, 1e-2);
            FloatingPointAssert.Equal(yTest, yTestPred, 1e-2);
        }
Exemplo n.º 7
0
        public void RidgeRegressionSample()
        {
            var clf = new RidgeRegression(alpha: 0.5);

            clf.Fit(new[, ] {
                { 0.0, 0.0 }, { 0.0, 0.0 }, { 1.0, 1.0 }
            }, new[] { 0.0, 0.1, 1.0 });
            Console.WriteLine(clf.Coef);
            Console.WriteLine(clf.Intercept);

            var prediction = clf.Predict(new[] { 5.0, 6.0 });

            Console.WriteLine(prediction);
        }
Exemplo n.º 8
0
        private double?TestTolerance(Func <Matrix <double>, Matrix <double> > filter)
        {
            var ridge = new RidgeRegression(tol: 1e-5);

            ridge.Fit(filter(xDiabetes), yDiabetes);
            double score = ridge.Score(filter(xDiabetes), yDiabetes);

            var ridge2 = new RidgeRegression(tol: 1e-3);

            ridge2.Fit(filter(xDiabetes), yDiabetes);
            double score2 = ridge2.Score(filter(xDiabetes), yDiabetes);

            Assert.IsTrue(score >= score2);

            return(null);
        }
Exemplo n.º 9
0
        private double?TestMultiRidgeDiabetes(Func <Matrix <double>, Matrix <double> > filter)
        {
            // simulate several responses
            Matrix y         = yDiabetes.HStack(yDiabetes);
            int    nFeatures = xDiabetes.ColumnCount;

            var ridge = new RidgeRegression(fitIntercept: false);

            ridge.Fit(filter(xDiabetes), y);
            Assert.AreEqual(ridge.Coef.Shape(), Tuple.Create(2, nFeatures));
            Matrix <double> yPred = ridge.Predict(filter(xDiabetes));

            ridge.Fit(filter(xDiabetes), yDiabetes);
            var yPred1 = ridge.Predict(filter(xDiabetes));

            Assert.IsTrue(yPred1.HStack(yPred1).AlmostEquals(yPred));
            return(null);
        }
Exemplo n.º 10
0
        public void TestRidgeIntercept()
        {
            var       rng       = new Random(0);
            const int nSamples  = 5;
            const int nFeatures = 10;
            Matrix    x         = DenseMatrix.CreateRandom(nSamples, nFeatures, new Normal {
                RandomSource = rng
            });
            Vector y = DenseVector.CreateRandom(nSamples, new Normal {
                RandomSource = rng
            });

            Matrix y1 = DenseMatrix.OfColumns(y.Count, 2, new[] { y, y.Add(1) });

            var ridge = new RidgeRegression();

            ridge.Fit(x, y);
            var intercept = ridge.Intercept;

            ridge.Fit(x, y1);
            Assert.AreEqual(ridge.Intercept[0], intercept[0], 1e-10);
            Assert.AreEqual(ridge.Intercept[1], intercept[0] + 1, 1e-10);
        }
Exemplo n.º 11
0
        public void TestRidge()
        {
            var          rng   = new Random(0);
            const double alpha = 1.0;

            foreach (var solver in new[] { RidgeSolver.Svd, RidgeSolver.DenseCholesky, RidgeSolver.Lsqr })
            {
                // With more samples than features
                int nSamples  = 6;
                int nFeatures = 5;
                var normal    = new Normal {
                    RandomSource = rng
                };
                Vector y = DenseVector.CreateRandom(nSamples, normal);
                Matrix x = DenseMatrix.CreateRandom(nSamples, nFeatures, normal);

                var ridge = new RidgeRegression(alpha: alpha, solver: solver);
                ridge.Fit(x, y);
                Assert.AreEqual(ridge.Coef.Row(0).Count, x.ColumnCount);
                Assert.IsTrue(ridge.Score(x, y) > 0.47);

                ridge.Fit(x, y, sampleWeight: DenseVector.Create(nSamples, i => 1.0));
                Assert.IsTrue(ridge.Score(x, y) > 0.47);

                // With more features than samples
                nSamples  = 5;
                nFeatures = 10;
                y         = DenseVector.CreateRandom(nSamples, normal);
                x         = DenseMatrix.CreateRandom(nSamples, nFeatures, normal);
                ridge     = new RidgeRegression(alpha: alpha, solver: solver);
                ridge.Fit(x, y);
                Assert.IsTrue(ridge.Score(x, y) > 0.9);

                ridge.Fit(x, y, sampleWeight: DenseVector.Create(nSamples, i => 1.0));
                Assert.IsTrue(ridge.Score(x, y) > 0.9);
            }
        }
Exemplo n.º 12
0
        public void TestRidgeVsLstsq()
        {
            var random = new Random(0);
            // we need more samples than features
            const int nSamples  = 5;
            const int nFeatures = 4;
            var       y         = DenseVector.CreateRandom(nSamples, new Normal {
                RandomSource = random
            });
            var x = DenseMatrix.CreateRandom(nSamples, nFeatures, new Normal {
                RandomSource = random
            });

            var ridge = new RidgeRegression(alpha: 0.0, fitIntercept: false);
            var ols   = new LinearRegression(fitIntercept: false);

            ridge.Fit(x, y);
            ols.Fit(x, y);
            Assert.IsTrue(ridge.Coef.AlmostEquals(ols.Coef));

            ridge.Fit(x, y);
            ols.Fit(x, y);
            Assert.IsTrue(ridge.Coef.AlmostEquals(ols.Coef));
        }
Exemplo n.º 13
0
        public void TestRidgeShapes()
        {
            var rng = new Random(0);
            const int nSamples = 5;
            const int nFeatures = 10;
            Matrix x = DenseMatrix.CreateRandom(nSamples, nFeatures, new Normal { RandomSource = rng });
            Vector y = DenseVector.CreateRandom(nSamples, new Normal { RandomSource = rng });

            Matrix Y = DenseMatrix.OfColumns(y.Count, 2, new[] { y, y.Add(1) });

            var ridge = new RidgeRegression();

            ridge.Fit(x, y);
            Assert.AreEqual(ridge.Coef.Shape(), Tuple.Create(1, nFeatures));
            Assert.AreEqual(ridge.Intercept.Count, 1);

            ridge.Fit(x, MatrixExtensions.ToColumnMatrix(y));
            Assert.AreEqual(ridge.Coef.Shape(), Tuple.Create(1, nFeatures));
            Assert.AreEqual(ridge.Intercept.Count, 1);

            ridge.Fit(x, Y);
            Assert.AreEqual(ridge.Coef.Shape(), Tuple.Create(2, nFeatures));
            Assert.AreEqual(ridge.Intercept.Count, 2);
        }
Exemplo n.º 14
0
        public void TestToyRidgeObject()
        {
            var x = DenseMatrix.OfArray(new double[,] { { 1 }, { 2 } });
            var y = new DenseVector(new double[] { 1, 2 });
            var clf = new RidgeRegression(alpha: 0.0);
            clf.Fit(x, y);
            var xTest = DenseMatrix.OfArray(new double[,] { { 1 }, { 2 }, { 3 }, { 4 } });
            Assert.AreEqual((clf.Predict(xTest) -
                DenseMatrix.OfArray(new double[,] { { 1 }, { 2 }, { 3 }, { 4 } })).FrobeniusNorm(), 0.0, 1e-10);

            Assert.AreEqual(clf.Coef.RowCount, 1);

            var y1 = DenseMatrix.OfColumns(y.Count, 2, new[] { y, y });

            clf.Fit(x, y1);

            //todo: what does this test do?
        }
Exemplo n.º 15
0
        public void TestRidgeVsLstsq()
        {
            var random = new Random(0);
            // we need more samples than features
            const int nSamples = 5;
            const int nFeatures = 4;
            var y = DenseVector.CreateRandom(nSamples, new Normal { RandomSource = random });
            var x = DenseMatrix.CreateRandom(nSamples, nFeatures, new Normal { RandomSource = random });

            var ridge = new RidgeRegression(alpha: 0.0, fitIntercept: false);
            var ols = new LinearRegression(fitIntercept: false);

            ridge.Fit(x, y);
            ols.Fit(x, y);
            Assert.IsTrue(ridge.Coef.AlmostEquals(ols.Coef));

            ridge.Fit(x, y);
            ols.Fit(x, y);
            Assert.IsTrue(ridge.Coef.AlmostEquals(ols.Coef));
        }
Exemplo n.º 16
0
 private double? TestRidgeDiabetes(Func<Matrix<double>, Matrix<double>> filter)
 {
     var ridge = new RidgeRegression(fitIntercept: false);
     ridge.Fit(filter(xDiabetes), yDiabetes);
     return Math.Round(ridge.Score(filter(xDiabetes), yDiabetes), 5);
 }
Exemplo n.º 17
0
        public void TestRidgeIntercept()
        {
            var rng = new Random(0);
            const int nSamples = 5;
            const int nFeatures = 10;
            Matrix x = DenseMatrix.CreateRandom(nSamples, nFeatures, new Normal { RandomSource = rng });
            Vector y = DenseVector.CreateRandom(nSamples, new Normal { RandomSource = rng });

            Matrix y1 = DenseMatrix.OfColumns(y.Count, 2, new[] { y, y.Add(1) });

            var ridge = new RidgeRegression();

            ridge.Fit(x, y);
            var intercept = ridge.Intercept;

            ridge.Fit(x, y1);
            Assert.AreEqual(ridge.Intercept[0], intercept[0], 1e-10);
            Assert.AreEqual(ridge.Intercept[1], intercept[0] + 1, 1e-10);
        }
Exemplo n.º 18
0
        private double? TestMultiRidgeDiabetes(Func<Matrix<double>, Matrix<double>> filter)
        {
            // simulate several responses
            Matrix y = yDiabetes.HStack(yDiabetes);
            int nFeatures = xDiabetes.ColumnCount;

            var ridge = new RidgeRegression(fitIntercept: false);
            ridge.Fit(filter(xDiabetes), y);
            Assert.AreEqual(ridge.Coef.Shape(), Tuple.Create(2, nFeatures));
            Matrix<double> yPred = ridge.Predict(filter(xDiabetes));
            ridge.Fit(filter(xDiabetes), yDiabetes);
            var yPred1 = ridge.Predict(filter(xDiabetes));
            Assert.IsTrue(yPred1.HStack(yPred1).AlmostEquals(yPred));
            return null;
        }
Exemplo n.º 19
0
        public void TestRidgeSampleWeights()
        {
            var rng = new Random(0);
            const double alpha = 1.0;

            foreach (var solver in new[] { RidgeSolver.Svd, RidgeSolver.DenseCholesky, RidgeSolver.Lsqr })
            {
                var tests = new[] { new[] { 6, 5 }, new[] { 5, 10 } };
                foreach (var test in tests)
                {
                    int nSamples = test[0];
                    int nFeatures = test[1];
                    Vector y = DenseVector.CreateRandom(nSamples, new Normal { RandomSource = rng });
                    Matrix x = DenseMatrix.CreateRandom(nSamples, nFeatures, new Normal { RandomSource = rng });
                    Vector sampleWeight = DenseVector.CreateRandom(nSamples, new ContinuousUniform(1, 2));

                    var ridgeRegression = new RidgeRegression(alpha: alpha, solver: solver, fitIntercept: false);
                    ridgeRegression.Fit(x, y, sampleWeight);
                    var coefs = ridgeRegression.Coef.Column(0);
                    // Sample weight can be implemented via a simple rescaling
                    // for the square loss
                    var regression2 = new RidgeRegression(alpha, solver: solver, fitIntercept: false);
                    regression2.Fit(
                        x.MulColumnVector(sampleWeight.Sqrt()),
                        y.PointwiseMultiply(sampleWeight.Sqrt()));
                    var coefs2 = regression2.Coef.Column(0);

                    Assert.IsTrue(coefs.AlmostEquals(coefs2));
                }
            }
        }
Exemplo n.º 20
0
        private double? TestTolerance(Func<Matrix<double>, Matrix<double>> filter)
        {
            var ridge = new RidgeRegression(tol: 1e-5);
            ridge.Fit(filter(xDiabetes), yDiabetes);
            double score = ridge.Score(filter(xDiabetes), yDiabetes);

            var ridge2 = new RidgeRegression(tol: 1e-3);
            ridge2.Fit(filter(xDiabetes), yDiabetes);
            double score2 = ridge2.Score(filter(xDiabetes), yDiabetes);

            Assert.IsTrue(score >= score2);

            return null;
        }