# 线性回归

74人阅读 评论(0)
#include <stdio.h>

#define MINDATA(a,b)    (a<b?a:b)

double calcAverage(double *x, int len)
{
int i;
double sum = 0;
for(i=0;i<len;i++)
{
sum += x[i];
}
return sum / len;
}

void calc_Y_with_a_b(double *x, double *y, double b, double a, int len)
{
int i;
for(i=0;i<len;i++)
{
y[i] = x[i] * b + a;
}
}

double calcVarianceSum(double *x, double xAverage, int len)
{
int i;
double sum = 0;
for(i=0;i<len;i++)
{
sum += (x[i] - xAverage) * (x[i] - xAverage);
}
return sum;
}

double calcConvarianceSum(double *x, double *y, double xAverage, double yAverage, int len)
{
int i;
double sum = 0;
for(i=0;i<len;i++)
{
sum += (x[i] - xAverage) * (y[i] - yAverage);
}
return sum;
}

double calcConvarianceSample(double *x, double *y, int len)
{
int i;
double sum = 0;
for(i=0;i<len;i++)
{
sum += (x[i] - y[i]) * (x[i] - y[i]);
}
return sum / len;
}

double debugPrintfArray(double *x, int len, const char *file, int line)
{
int i;
printf("the array is in:%s,%d", file, line);
for(i=0;i<len;i++)
{
if(i % 4 == 0)
printf("\\\n");
printf("%15.4lf ,", x[i]);
}
printf("\n");
printf("\n");
}

int main()
{
double outY[] =
{0.48 ,0.99 ,1.49 ,1.99 ,\
2.51 ,3.00 ,3.52 ,3.99 ,\
4.50 ,5.01 ,5.49 ,6.00 ,\
6.51 ,7.01 ,7.49 ,8.00 ,\
8.51 ,9.02 ,9.51 ,10.02 ,\
10.51 ,11.01 ,11.49 ,12.00 ,\
12.49 ,13.00 ,13.52 ,14.07 ,\
14.52 ,14.99 ,};
/*    double sample[] = {         0.2680 ,         0.8752 ,         1.3828 ,         1.8706 ,\
2.4578 ,         3.0650 ,         3.6424 ,         4.1799 ,\
4.6875 ,         5.1753 ,         5.6531 ,         6.1010 ,\
6.5489 ,         7.0068 ,         7.4945 ,         7.9723 ,\
8.4601 ,         8.9478 ,         9.4356 ,         9.9432 ,\
10.4708 ,        10.9884 ,        11.4960 ,        11.9937 ,\
12.4914 ,        12.9991 ,        13.5067 ,        14.0044 ,\
14.5121 ,        15.0098 ,
};*/
double sample[] =
{0.40 ,1.01 ,1.52 ,2.01 ,\
2.60 ,3.21 ,3.79 ,4.33 ,\
4.84 ,5.33 ,5.81 ,6.26 ,\
6.71 ,7.17 ,7.66 ,8.14 ,\
8.63 ,9.12 ,9.61 ,10.12 ,\
10.65 ,11.17 ,11.68 ,12.18 ,\
12.68 ,13.19 ,13.70 ,14.20 ,\
14.71 ,15.21 ,};

//y = bx+a
double b,a;
int sampleLen = MINDATA(sizeof(sample) / sizeof(double), sizeof(outY) / sizeof(double));
printf("sample=%lu,out=%lu,uselen=%d\n", sizeof(sample) / sizeof(double), sizeof(outY) / sizeof(double), sampleLen);
double xAver = calcAverage(sample, sampleLen);
double yAver = calcAverage(outY, sampleLen);
b = calcConvarianceSum(sample, outY, xAver, yAver, sampleLen) / calcVarianceSum(sample, xAver, sampleLen);
a = yAver - xAver * b;
printf("b=%lf,a=%lf\n", b, a);
double varianceOrigin = calcConvarianceSample(outY, sample, sampleLen);
double *ynew = new double[sampleLen];
calc_Y_with_a_b(sample, ynew, b, a, sampleLen);
double varianceNew = calcConvarianceSample(outY, ynew, sampleLen);
printf("v1=%lf,v2=%lf\n", varianceOrigin, varianceNew);
debugPrintfArray(ynew, sampleLen, __FILE__, __LINE__);
delete(ynew);
return 1;
}
0
0

【直播】计算机视觉原理及实战——屈教授
【套餐】深度学习入门视频课程——唐宇迪
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】机器学习之凸优化——马博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：39353次
• 积分：1122
• 等级：
• 排名：千里之外
• 原创：72篇
• 转载：25篇
• 译文：0篇
• 评论：1条
评论排行
最新评论