最近刚开始接触机器学习,在这里使用c#模拟一元线性回归,先上图看效果
因为源码中有一些控件是自己封装的,所以就不上传可运行的程序集了,贴出核心代码,以供参考,如有不对,请多多给予建议
1 private void ryButtonX1_Click(object sender, EventArgs e) 2 { 3 string[] xnum = richTextBox1.Text.Trim().Split(',');//x值 4 string[] ynum = richTextBox2.Text.Trim().Split(',');//y值 5 if (xnum.Length != ynum.Length) 6 { 7 MessageBox.Show("输入数据有误!"); 8 return; 9 } 10 ryTextBoxX1.Text = xnum.Length+"";//个数 11 decimal xsum = 0;//x值求和 12 decimal ysum = 0;//y值求和 13 for(int i = 0; i < xnum.Length; i++) 14 { 15 xsum = xsum + ConvertExtend.ToDecimal(xnum[i],0); 16 ysum = ysum + ConvertExtend.ToDecimal(ynum[i], 0); 17 } 18 decimal xAve = ConvertExtend.ToDecimal(xsum / xnum.Length, 0);//x平均值 19 decimal yAve = ConvertExtend.ToDecimal(ysum / xnum.Length, 0);//y平均值 20 21 ryTextBoxX3.Text = string.Format("{0:N}", xAve);//保留两位小数 22 ryTextBoxX4.Text = string.Format("{0:N}", yAve); 23 24 decimal molecule = 0;//分子 25 decimal Denominator = 0;//分母 26 for (int i = 0; i < xnum.Length; i++) 27 { 28 molecule = molecule + (ConvertExtend.ToDecimal(xnum[i], 0) - xAve) * (ConvertExtend.ToDecimal(ynum[i], 0) - yAve); 29 Denominator = Denominator+(ConvertExtend.ToDecimal(xnum[i], 0) - xAve) * (ConvertExtend.ToDecimal(xnum[i], 0) - xAve); 30 } 31 ryTextBoxX2.Text = string.Format("{0:N}", molecule / Denominator);//斜率 32 ryTextBoxX5.Text = (yAve - (molecule / Denominator) * xAve)+"";//截距 33 34 if (ConvertExtend.ToDecimal(ryTextBoxX5.Text, 0) < 0) 35 { 36 ryTextBoxX6.Text = ryTextBoxX2.Text + "X" + ryTextBoxX5.Text; 37 }else 38 { 39 ryTextBoxX6.Text = ryTextBoxX2.Text + "X+" + ryTextBoxX5.Text; 40 } 41 42 #region 画点 43 chartLabTrend.ChartAreas[0].AxisX.CustomLabels.Clear(); 44 List<decimal?> lx = new List<decimal?>(); 45 List<decimal?> l1 = new List<decimal?>(); 46 for (int i = 1; i <= xnum.Length; i++) 47 { 48 CustomLabel label1 = new CustomLabel(); 49 if (xnum[i - 1] != "") 50 { 51 label1.Text = ConvertExtend.ToDecimal(xnum[i - 1],0).ToString(); 52 label1.ToPosition = i * 2; 53 chartLabTrend.ChartAreas[0].AxisX.CustomLabels.Add(label1); 54 label1.GridTicks = GridTickTypes.Gridline; 55 lx.Add(i); 56 if (ynum[i - 1] == null) 57 { 58 l1.Add(null); 59 } 60 else 61 { 62 l1.Add(ConvertExtend.ToDecimal(ynum[i - 1],0)); 63 } 64 } 65 } 66 chartLabTrend.Series[0].Points.DataBindXY(lx, l1); 67 #endregion 68 69 #region 画线 70 chartLabTrend.ChartAreas[0].AxisX.CustomLabels.Clear(); 71 List<decimal?> lx1 = new List<decimal?>(); 72 List<decimal?> l11 = new List<decimal?>(); 73 for (int i = 1; i <= xnum.Length; i++) 74 { 75 CustomLabel label2 = new CustomLabel(); 76 if (xnum[i - 1] != "") 77 { 78 label2.Text = ConvertExtend.ToDecimal(xnum[i - 1], 0).ToString(); 79 label2.ToPosition = i * 2; 80 chartLabTrend.ChartAreas[0].AxisX.CustomLabels.Add(label2); 81 label2.GridTicks = GridTickTypes.Gridline; 82 lx1.Add(i); 83 if (ynum[i - 1] == null) 84 { 85 l11.Add(null); 86 } 87 else 88 { 89 l11.Add(ConvertExtend.ToDecimal(ConvertExtend.ToDecimal(xnum[i - 1],0)*molecule / Denominator 90 + ConvertExtend.ToDecimal(ryTextBoxX5.Text,0), 0)); 91 } 92 } 93 } 94 chartLabTrend.Series[1].Points.DataBindXY(lx1, l11); 95 #endregion 96 97 }