forked from tripsycodes/CS-PRJOECT-10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regression.c
64 lines (64 loc) · 1.58 KB
/
regression.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <math.h>
#include <stdio.h>
#define float double
int main()
{
int n, i;
float x, y, m, c, d;
float sumx = 0, sumxsq = 0, sumy = 0, sumxy = 0;
float sumysq = 0;
float mae = 0;
float mse = 0;
printf("enter the number of values for n:");
scanf("%d", &n);
float ax[n];
float ay[n];
float p = n;
for (i = 0; i < n; i++)
{
printf("enter values of x and y\n");
scanf("%lf%lf", &x, &y);
sumx = sumx + x;
sumxsq = sumxsq + (x * x);
sumy = sumy + y;
sumxy = sumxy + (x * y);
sumysq = sumysq + (y * y);
ax[i] = x;
ay[i] = y;
}
d = p * sumxsq - sumx * sumx;
m = (p* sumxy - sumx * sumy) / d;
c = (sumy * sumxsq - sumx * sumxy) / d;
while(1)
{
printf("1.compute sum\n");
printf("2.find best fitting line\n");
printf("3.end\n");
int num;
scanf("%d", &num);
if (num == 1)
{
printf("sum x = %lf\n", sumx);
printf("sum y = %lf\n", sumy);
printf("sum xy = %lf\n", sumxy);
printf("sum xsq = %lf\n", sumxsq);
printf("sum ysq = %lf\n", sumysq);
}
else if (num == 2)
{
float p = n;
printf("the best fitting line is y = %lfx+%lf\n", m, c);
for (int i = 0; i < n; i++)
{
mae += abs(ay[i] - (m * ax[i] + c));
mse += (ay[i] - (m * ax[i] + c)) * (ay[i] - (m * ax[i] + c));
}
printf("mean absolute error = %lf\n", mae / p);
printf("mean squared error = %lf\n", mse / p);
}
else if(num==3)
{
return 0;
}
}
}