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 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_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 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)); }