公式: 线性回归的目标是拟合一条直线,形式为: y=mx+by=mx+b 其中:
优点:
缺点:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using MathNet.Numerics; using MathNet.Numerics.LinearRegression; class Program { static void Main() { double[] x = { 1, 2, 3, 4, 5 }; double[] y = { 2, 4, 6, 8, 10 }; // 进行线性回归 var (slope, intercept) = SimpleRegression.Fit(x, y); Console.WriteLine($"拟合方程: y = {intercept} + {slope}x"); } } |
公式: 多项式拟合的目标是拟合一个多项式,形式为: y=anxn+an−1xn−1+...+a1x+a0y=an?xn+an−1?xn−1+...+a1?x+a0? 其中:
优点:
缺点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using MathNet.Numerics; using MathNet.Numerics.LinearRegression; class Program { static void Main() { double[] x = { 1, 2, 3, 4, 5 }; double[] y = { 2, 3, 5, 7, 11 }; // 一组非线性数据 // 进行多项式拟合,设定次数为2 double[] coefficients = Fit.Polynomial(x, y, degree: 2); Console.WriteLine("拟合方程:"); for (int i = coefficients.Length - 1; i >= 0; i--) { Console.WriteLine($"{coefficients[i]}x^{i}"); } } } |
特征 | 线性回归 | 多项式拟合 |
---|---|---|
拟合形式 | 直线 y=mx+by=mx+b | 多项式 y=anxn+...y=an?xn+... |
优点 | 简单、快速 | 能拟合复杂非线性关系 |
缺点 | 只能处理线性关系 | 容易过拟合,计算复杂度高 |
适用场景 | 数据呈线性关系时 | 数据呈现非线性关系时 |