Exemple #1
0
        public void Linear_Regression_Test_CostFunction_Regularized()
        {
            Vector theta = new Vector(new double[] { 1, 1 });

            Matrix X = new[, ] {
                { 1, -15.9368 },
                { 1, -29.1530 },
                { 1, 36.1895 },
                { 1, 37.4922 },
                { 1, -48.0588 },
                { 1, -8.9415 },
                { 1, 15.3078 },
                { 1, -34.7063 },
                { 1, 1.3892 },
                { 1, -44.3838 },
                { 1, 7.0135 },
                { 1, 22.7627 }
            };

            Vector y = new Vector(new double[] {
                2.1343,
                1.1733,
                34.3591,
                36.8380,
                2.8090,
                2.1211,
                14.7103,
                2.6142,
                3.7402,
                3.7317,
                7.6277,
                22.7524
            });

            numl.Math.Functions.Cost.ICostFunction          costFunction = new numl.Math.Functions.Cost.LinearCostFunction();
            numl.Math.Functions.Regularization.IRegularizer regulariser  = new numl.Math.Functions.Regularization.Regularization();

            double cost = costFunction.ComputeCost(theta, X, y, 1, regulariser);
            Vector grad = costFunction.ComputeGradient(theta, X, y, 1, regulariser);

            Assert.AreEqual(303.99, System.Math.Round(cost, 2));

            Assert.AreEqual(new double[] { -15.3, 598.3 }, grad.Select(s => System.Math.Round(s, 1)).ToArray());
        }
        public void Linear_Regression_Test_CostFunction()
        {
            Vector theta = new Vector(new double[] { 1, 1 });

            Matrix X = new[,] {
                        { 1, -15.9368 },
                        { 1, -29.1530 },
                        { 1,  36.1895 },
                        { 1,  37.4922 },
                        { 1, -48.0588 },
                        { 1,  -8.9415 },
                        { 1,  15.3078 },
                        { 1, -34.7063 },
                        { 1,   1.3892 },
                        { 1, -44.3838 },
                        { 1,   7.0135 },
                        { 1,  22.7627 }
            };

            Vector y = new Vector(new double[] {
                         2.1343,
                         1.1733,
                        34.3591,
                        36.8380,
                         2.8090,
                         2.1211,
                        14.7103,
                         2.6142,
                         3.7402,
                         3.7317,
                         7.6277,
                        22.7524
            });

            numl.Math.Functions.Cost.ICostFunction costFunction = new numl.Math.Functions.Cost.LinearCostFunction();
            double cost = costFunction.ComputeCost(theta, X, y, 0, null);
            Vector grad = costFunction.ComputeGradient(theta, X, y, 0, null);

            Assert.AreEqual(303.95d, System.Math.Round(cost, 2));

            Assert.AreEqual(new double[] { -15.3, 598.2 }, grad.Select(s => System.Math.Round(s, 1)).ToArray());
        }