1. 给定输入点和输出点数组
2. 进行线性拟合,计算方差和
3. 打印回归线方程
实现代码:
public class SimpleLinearDemo
{
public static void Execute()
{
double[] inputs = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
double[] outputs = { 6, 9.5, 13, 15, 16.5, 17.5, 18.5, 19, 19.5, 19.7, 19.8 };
double[] logx = Matrix.Log(inputs);
var lr = new SimpleLinearRegression();
double error = lr.Regress(logx, outputs);
Console.WriteLine("the ‘generalized least squares’ value is {0}", error);
string result = lr.ToString("N4", CultureInfo.InvariantCulture);
Console.WriteLine("the line function is : {0}", result);
}
}
class Program
{
static void Main(string[] args)
{
SimpleLinearDemo.Execute();
Console.ReadLine();
}
}
运行结果: