示例#1
0
        public void Logistic_Regression_Test_CostFunction_1()
        {
            Matrix X = new[, ]
            {
                { 1, 1, 1 },
                { 1, 1, 1 },
                { 1, 1, 1 },
                { 8, 1, 6 },
                { 3, 5, 7 },
                { 4, 9, 2 }
            };

            Vector y     = new Vector(new double[] { 1, 0, 1, 0, 1, 0 });
            Vector theta = new Vector(new double[] { 0, 1, 0 });

            numl.Math.Functions.Cost.ICostFunction          logisticCostFunction = new numl.Math.Functions.Cost.LogisticCostFunction();
            numl.Math.Functions.Regularization.IRegularizer regularizer          = new numl.Math.Functions.Regularization.Regularization();

            double cost = logisticCostFunction.ComputeCost(theta.Copy(), X, y, 3, regularizer);

            theta = logisticCostFunction.ComputeGradient(theta.Copy(), X, y, 3, regularizer);

            Assert.AreEqual(2.2933d, System.Math.Round(cost, 4));

            Assert.AreEqual(1.6702d, System.Math.Round(theta[0], 4));
            Assert.AreEqual(2.1483d, System.Math.Round(theta[1], 4));
            Assert.AreEqual(1.0887d, System.Math.Round(theta[2], 4));
        }
示例#2
0
        public void Logistic_Regression_Test_CostFunction_2_WithoutRegularization()
        {
            Matrix X = new[, ] {
                { 8, 1, 6 },
                { 3, 5, 7 },
                { 4, 9, 2 }
            };

            Vector y     = new Vector(new double[] { 1, 1, 0 });
            Vector theta = new Vector(new double[] { 0, 1, 0 });

            numl.Math.Functions.Cost.ICostFunction logisticCostFunction = new numl.Math.Functions.Cost.LogisticCostFunction();

            double cost = logisticCostFunction.ComputeCost(theta.Copy(), X, y, 0, null);

            theta = logisticCostFunction.ComputeGradient(theta.Copy(), X, y, 0, null);

            Assert.AreEqual(3.1067d, System.Math.Round(cost, 4));

            Assert.AreEqual(0.6093d, System.Math.Round(theta[0], 4));
            Assert.AreEqual(2.8988d, System.Math.Round(theta[1], 4));
            Assert.AreEqual(0.1131d, System.Math.Round(theta[2], 4));
        }
        public void Logistic_Regression_Test_CostFunction_1()
        {
            Matrix X = new[,] 
            {{ 1, 1, 1 },
             { 1, 1, 1 },
             { 1, 1, 1 },
             { 8, 1, 6 },
             { 3, 5 ,7 },
             { 4, 9, 2 }};

            Vector y = new Vector(new double[] { 1, 0, 1, 0, 1, 0 });
            Vector theta = new Vector(new double[] { 0, 1, 0 });

            numl.Math.Functions.Cost.ICostFunction logisticCostFunction = new numl.Math.Functions.Cost.LogisticCostFunction();
            numl.Math.Functions.Regularization.IRegularizer regularizer = new numl.Math.Functions.Regularization.Regularization();

            double cost = logisticCostFunction.ComputeCost(theta.Copy(), X, y, 3, regularizer);
            
            theta = logisticCostFunction.ComputeGradient(theta.Copy(), X, y, 3, regularizer);

            Assert.AreEqual(2.2933d, System.Math.Round(cost, 4));

            Assert.AreEqual(1.6702d, System.Math.Round(theta[0], 4));
            Assert.AreEqual(2.1483d, System.Math.Round(theta[1], 4));
            Assert.AreEqual(1.0887d, System.Math.Round(theta[2], 4));
        }
        public void Logistic_Regression_Test_CostFunction_2_WithoutRegularization()
        {
            Matrix X = new[,] {
             { 8, 1, 6 },
             { 3, 5 ,7 },
             { 4, 9, 2 }};

            Vector y = new Vector(new double[] { 1, 1, 0 });
            Vector theta = new Vector(new double[] { 0, 1, 0 });

            numl.Math.Functions.Cost.ICostFunction logisticCostFunction = new numl.Math.Functions.Cost.LogisticCostFunction();

            double cost = logisticCostFunction.ComputeCost(theta.Copy(), X, y, 0, null);
            
            theta = logisticCostFunction.ComputeGradient(theta.Copy(), X, y, 0, null);

            Assert.AreEqual(3.1067d, System.Math.Round(cost, 4));

            Assert.AreEqual(0.6093d, System.Math.Round(theta[0], 4));
            Assert.AreEqual(2.8988d, System.Math.Round(theta[1], 4));
            Assert.AreEqual(0.1131d, System.Math.Round(theta[2], 4));
        }