-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnewLinearReg.cpp
More file actions
62 lines (44 loc) · 856 Bytes
/
Copy pathnewLinearReg.cpp
File metadata and controls
62 lines (44 loc) · 856 Bytes
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
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
void gradient_descent(vector<double> &x, vector<double> &y,int n)
{
double ss=0; // sum of squere
double sp=0; // sum of product;
double mx=0; // Mean of x's value
double my=0; // mean of Y's value
double m; // slope'
double c; // intercept
for(int i=0;i<n;i++)
{ mx = mx + x[i];
my = my + y[i];
}
mx = mx/n;
my = my/n;
for(int i=0;i<n;i++)
{
ss = ss + pow(x[i]-mx,2);
}
for(int i=0;i<n;i++)
{
sp = sp + (x[i]-mx) * (y[i]-my) ;
}
m = sp/ss;
c = my - m*mx;
for(int i=0;i<n;i++)
{ cout<<" yp "<<m*x[i]+c<< " x "<<x[i]<<endl;
}
cout<<endl;
cout<<" slope "<<m<<" intercept "<<c<<endl;
}
int main()
{
int n;
cin>>n;
vector<double> x(n);
vector<double> y(n);
for(int i=0;i<n;i++)
{ cin>>x[i]>>y[i]; }
gradient_descent(x,y,n);
}