Ejemplo n.º 1
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);
        }
Ejemplo 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?
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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;
        }
Ejemplo n.º 6
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?
        }