C#直线的最小二乘法线性回归运算实例
作者:北风其凉 时间:2022-05-03 11:19:33
本文实例讲述了C#直线的最小二乘法线性回归运算方法。分享给大家供大家参考。具体如下:
1.Point结构
在编写C#窗体应用程序时,因为引用了System.Drawing命名空间,其中自带了Point结构,本文中的例子是一个控制台应用程序,因此自己制作了一个Point结构
/// <summary>
/// 二维笛卡尔坐标系坐标
/// </summary>
public struct Point
{
public double X;
public double Y;
public Point(double x = 0, double y = 0)
{
X = x;
Y = y;
}
}
2.线性回归
/// <summary>
/// 对一组点通过最小二乘法进行线性回归
/// </summary>
/// <param name="parray"></param>
public static void LinearRegression(Point[] parray)
{
//点数不能小于2
if (parray.Length < 2)
{
Console.WriteLine("点的数量小于2,无法进行线性回归");
return;
}
//求出横纵坐标的平均值
double averagex = 0, averagey = 0;
foreach (Point p in parray)
{
averagex += p.X;
averagey += p.Y;
}
averagex /= parray.Length;
averagey /= parray.Length;
//经验回归系数的分子与分母
double numerator = 0;
double denominator = 0;
foreach (Point p in parray)
{
numerator += (p.X - averagex) * (p.Y - averagey);
denominator += (p.X - averagex) * (p.X - averagex);
}
//回归系数b(Regression Coefficient)
double RCB = numerator / denominator;
//回归系数a
double RCA = averagey - RCB * averagex;
Console.WriteLine("回归系数A: " + RCA.ToString("0.0000"));
Console.WriteLine("回归系数B: " + RCB.ToString("0.0000"));
Console.WriteLine(string.Format("方程为: y = {0} + {1} * x",
RCA.ToString("0.0000"), RCB.ToString("0.0000")));
//剩余平方和与回归平方和
double residualSS = 0; //(Residual Sum of Squares)
double regressionSS = 0; //(Regression Sum of Squares)
foreach (Point p in parray)
{
residualSS +=
(p.Y - RCA - RCB * p.X) *
(p.Y - RCA - RCB * p.X);
regressionSS +=
(RCA + RCB * p.X - averagey) *
(RCA + RCB * p.X - averagey);
}
Console.WriteLine("剩余平方和: " + residualSS.ToString("0.0000"));
Console.WriteLine("回归平方和: " + regressionSS.ToString("0.0000"));
}
3.Main函数调用
static void Main(string[] args)
{
//设置一个包含9个点的数组
Point[] array = new Point[9];
array[0] = new Point(0, 66.7);
array[1] = new Point(4, 71.0);
array[2] = new Point(10, 76.3);
array[3] = new Point(15, 80.6);
array[4] = new Point(21, 85.7);
array[5] = new Point(29, 92.9);
array[6] = new Point(36, 99.4);
array[7] = new Point(51, 113.6);
array[8] = new Point(68, 125.1);
LinearRegression(array);
Console.Read();
}
4.运行结果
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,二乘法,线性,运算
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java @Accessors注解图文详解
2023-10-10 06:06:51
![](https://img.aspxhome.com/file/2023/2/73072_0s.png)
Java ThreadLocal的使用详解
2023-11-29 04:48:43
Java 实现将List平均分成若干个集合
2023-10-16 21:36:59
![](https://img.aspxhome.com/file/2023/2/101932_0s.jpg)
Java手动实现Redis的LRU缓存机制
2023-07-31 12:51:30
![](https://img.aspxhome.com/file/2023/7/57807_0s.png)
java基础之泛型知识点总结
2021-09-03 21:30:29
Maven分模块开发执行指令失败的问题
2021-07-10 19:00:15
![](https://img.aspxhome.com/file/2023/0/72560_0s.jpg)
java模拟实现双向链表
2022-06-05 09:16:16
![](https://img.aspxhome.com/file/2023/2/62072_0s.jpg)
JetCache 缓存框架的使用及源码解析(推荐)
2021-07-23 12:21:25
![](https://img.aspxhome.com/file/2023/2/64402_0s.png)
Java字符串拼接详解
2022-10-17 06:22:51
Android编程实现下载时主界面与详细界面一致更新的方法
2023-01-02 10:07:35
通过实例了解java spring使用构造器注入的原因
2022-11-25 03:48:24
避免sql注入_动力节点Java学院整理
2023-08-21 17:39:12
举例讲解Java中Piped管道输入输出流的线程通信控制
2021-06-25 14:19:58
![](https://img.aspxhome.com/file/2023/0/69910_0s.jpg)
详解Java使用Jsch与sftp服务器实现ssh免密登录
2023-11-24 18:34:18
ShardingSphere jdbc实现分库分表核心概念详解
2023-11-24 12:09:45
![](https://img.aspxhome.com/file/2023/3/59763_0s.png)
Spring component-scan XML配置与@ComponentScan注解配置
2023-07-04 03:37:12
一文详解gRPC快速整合SpringCloud
2021-12-11 05:01:02
![](https://img.aspxhome.com/file/2023/4/70504_0s.jpg)
java文件上传下载代码实例
2023-11-10 05:06:14
Java如何使用httpclient检测url状态及链接是否能打开
2022-07-03 21:23:25
JAVA中使用FTPClient实现文件上传下载实例代码
2021-08-17 20:45:05