机器学习 -- 多元回归实现实例


实现依然基于accord.net framework


1. 二维数组作为输入和一元输出数组
2. 执行回归,得到回归的方差和,以及回归方程(ax+by+c)的各个系数


实现代码


 
public class MultiRegressionDemo
    {
        public static void Execute()
        {


            // 2 dimensions
            MultipleLinearRegression target = new MultipleLinearRegression(2, true);


            double[][] inputs =
            {
                new double[] { 1, 8 },
                new double[] { 2, 29 },
                new double[] { 3, 2 },
                new double[] { 0, 1 },
                new double[] { 5, 6 },
                new double[] { 4, 7 },
                new double[] { 14, 17 },
            };
            double[] outputs = { 9, 4, 3, 2, 5, 12, 7 };


            double error = target.Regress(inputs, outputs);
            Console.WriteLine("the regression error is '{0}'", error);


            // As result, we will be given the following:
            double a = target.Coefficients[0]; // a = 0
            double b = target.Coefficients[1]; // b = 0
            double c = target.Coefficients[2]; // c = 1


            Console.WriteLine("line functions is :{0}x + {1}y + {2}", a, b, c);
        }
    }


class Program
    {
        static void Main(string[] args)
        {
            MultiRegressionDemo.Execute();
            Console.ReadLine();
        }
    }




运行结果:



你可能感兴趣的:(机器学习 -- 多元回归实现实例)