public class LogisticRegressionDemo { public static void Execute() { double[][] input = { new double[] { 55, 0 }, new double[] { 28, 0 }, new double[] { 65, 1 }, new double[] { 46, 0 }, new double[] { 86, 1 }, new double[] { 56, 1 }, new double[] { 85, 0 }, new double[] { 33, 0 }, new double[] { 21, 1 }, new double[] { 42, 1 }, }; double[] output = { 0, 0, 0, 1, 1, 1, 0, 0, 0, 1 }; LogisticRegression regression = new LogisticRegression(2); var trainer = new IterativeReweightedLeastSquares(regression); double delta = 0; do { // Perform an iteration delta = trainer.Run(input, output); } while (delta > 0.001); var b1 = regression.Coefficients[1]; var b2 = regression.Coefficients[2]; var b0 = regression.Intercept; var func = new Func<double, double, double>((x1, x2) => { var result = 1 / (1 + Math.Exp(-b0 - b1 * x1 - b2 * x2)); return result; }); var age = 79; var smoking = 0; var r = func(age, smoking); Console.WriteLine("input x [age:{0}, smoking:{1}] is {2}", age, smoking, r); } } static void Main(string[] args) { LogisticRegressionDemo.Execute(); Console.ReadLine(); }
本例使用了accord.net framework ,是不错的.NET机器学习算法学习库。