
问题
编写一个程序来实现线性回归算法。
用户需要输入总共的数值个数。
解决方案
使用C编程语言计算线性回归的解决方案如下:
线性回归通过将线性方程与观测数据相连接来找到两个变量之间的关系。一个变量是解释变量,另一个是因变量。
关于线性回归的逻辑如下所述:
for(i=0;i<n;i++){ printf("enter values of x and y"); scanf("%f%f",&x,&y); sumx=sumx+x; sumxsq=sumxsq+(x*x); sumy=sumy+y; sumxy=sumxy+(x*y);}d=n*sumxsq-sumx*sumx;m=(n*sumxy-sumx*sumy)/d;c=(sumy*sumxsq-sumx*sumxy)/d;
Finally, print m and c.
Example
Following is the C program to compute the linear regression −
Live Demo
#include
Output
When the above program is executed, it produces the following result −
enter the number of values for n:5enter values of x and y1 5enter values of x and y2 6enter values of x and y2 4enter values of x and y3 7enter values of x and y1 1M=2.000000 C=1.000000
以上就是C程序计算线性回归的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1443310.html
微信扫一扫
支付宝扫一扫