Ejemplo n.º 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 });

            ICostFunction logisticCostFunction = new Functions.CostFunctions.LogisticCostFunction();
            IRegularizer  regularizer          = new Functions.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));
        }
Ejemplo n.º 2
0
        public void Logistic_Regression_Test_CostFunction_2_WithRegularization()
        {
            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 });

            ICostFunction logisticCostFunction = new Functions.CostFunctions.LogisticCostFunction();
            IRegularizer  regularizer          = new Functions.Regularization();

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

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

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

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