示例#1
0
        public void TestLinearRegression()
        {
            // Control.LinearAlgebraProvider = new MklLinearAlgebraProvider();
            // a simple dataset
            var x = DenseMatrix.OfArray(new double[, ] {
                { 1 }, { 2 }
            });
            var y = DenseVector.OfEnumerable(new double[] { 1, 2 });

            var clf = new LinearRegression();

            clf.Fit(x, y);

            Assert.AreEqual(1.0, clf.Coef.Column(0)[0], 1E-5);
            //Assert.AreEqual(0.0, clf.Intercept[0]);
            Assert.IsTrue(DenseVector.OfEnumerable(new double[] { 1, 2 }).AlmostEquals(clf.Predict(x).Column(0)));

            // test it also for degenerate input
            x = DenseMatrix.OfArray(new double[, ] {
                { 1 }
            });
            y = DenseVector.OfEnumerable(new double[] { 0 });


            clf = new LinearRegression(fitIntercept: false);
            clf.Fit(x, y);
            Assert.AreEqual(0.0, clf.Coef.Column(0)[0]);
            //assert_array_almost_equal(clf.intercept_, [0])
            Assert.AreEqual(0.0, clf.Predict(x).Column(0)[0]);
        }
 public void PredictTest()
 {
     double[,] features = { { 1d, 0d }, { 1d, 1d }, { 1d, 2d }, { 1d, 3d }, { 1d, 4d } };
     double[] theta       = { 1d, -1d };
     double[] labels      = { 1d, 0d, -1d, -2d, -3d };
     double[] predictions = LinearRegression.Predict(features, theta);
     CollectionAssert.AreEqual(labels, predictions);
 }
        private void TestLinearRegression()
        {
            LinearRegression linearRegression = new LinearRegression(trainingEpochs, learningRate);

            linearRegression.Train(dataX, dataY);
            (float[] results, float optimalWeights, float optimalBias) = linearRegression.Predict(testX);
            //analytical solution coefficientVector=(X'*X)*X'*Y where X is the inputX with ones in the first column and Y is inputY: bias=0.7988 and weight=0.2516
            Assert.Equal(0.309068531, optimalWeights, 6);
            Assert.Equal(0.373608261, optimalBias, 6);
        }
        protected override double GetFileEstimation(ProjectFile file)
        {
            double numberOfBugsReal       = NumberOfFixedDefectsForFile(file.ID);
            double numberOfBugsEstimation = regression.Predict(
                GetPredictorValuesFor(context.SetFiles(e => e.IdIs(file.ID)))[0]
                );
            double numberOfBugs = numberOfBugsEstimation - numberOfBugsReal;

            return(numberOfBugs > 0 ? numberOfBugs : 0);
        }
示例#5
0
        public void TestLinearRegressionSparseMultipleOutcome()
        {
            var             random    = new Random(0);
            var             r         = SampleGenerator.MakeSparseUncorrelated(random: random);
            Matrix          x         = SparseMatrix.OfMatrix(r.X);
            Vector <double> y         = r.Y.Column(0);
            Matrix          y1        = DenseMatrix.OfColumns(y.Count, 2, new[] { y, y });
            int             nFeatures = x.ColumnCount;

            var ols = new LinearRegression();

            ols.Fit(x, y1);
            Assert.AreEqual(Tuple.Create(2, nFeatures), ols.Coef.Shape());
            Assert.AreEqual(Tuple.Create(2, nFeatures), ols.Coef.Shape());
            Matrix <double> yPred = ols.Predict(x);

            ols.Fit(x, y);
            Matrix <double> yPred1 = ols.Predict(x);

            Assert.IsTrue(yPred1.Column(0).AlmostEquals(yPred.Column(0)));
            Assert.IsTrue(yPred1.Column(0).AlmostEquals(yPred.Column(1)));
        }
示例#6
0
        public void TestLinearRegressionMultipleOutcome()
        {
            var result = SampleGenerator.MakeRegression(shuffle: false, random: new Random(0));

            Matrix y = DenseMatrix.OfColumns(
                result.Y.RowCount,
                2,
                new[] { result.Y.Column(0), result.Y.Column(0) });
            var numFeatures = result.X.RowCount;

            var clf = new LinearRegression(fitIntercept: true);

            clf.Fit(result.X, y);
            Assert.AreEqual(Tuple.Create(2, numFeatures), clf.Coef.Shape());

            Matrix <double> yPred = clf.Predict(result.X);

            clf.Fit(result.X, result.Y);
            Matrix <double> yPred1 = clf.Predict(result.X);

            Assert.IsTrue(yPred1.Column(0).AlmostEquals(yPred.Column(0)));
            Assert.IsTrue(yPred1.Column(0).AlmostEquals(yPred.Column(1)));
        }