-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsr.cpp
212 lines (188 loc) · 4.89 KB
/
lsr.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// SWAMI KARUPPASWAMI THUNNAI
#include "lsr.h"
void simple_linear_regression::print(std::string message)
{
if (this->verbose) std::cout << message << "\n";
}
void simple_linear_regression::calculate_N()
{
if (this->X.size() == this->y.size())
{
this->N = this->X.size();
}
else
{
throw "SIZE MISMATCH";
}
}
// Calculates the X square and saves it in XX vector
void simple_linear_regression::x_square()
{
print("Calculating X*X");
for (double i : this->X)
{
this->XX.push_back(i*i);
}
}
// Calculates X*y and stores it in Xy vector
void simple_linear_regression::x_cross_y()
{
print("Calculating X*y");
for (unsigned int index = 0; index < N; index++)
{
this->Xy.push_back(this->X[index] * this->y[index]);
}
}
// Calculate all the summation
void simple_linear_regression::calculate_sigma()
{
// Calculate summation of X
print("Calculating sigma for X");
for (double i : this->X)
{
this->sigma_X += i;
}
// Calculate the summation of Y
print("Calculating sigma for Y");
for (double i : this->y)
{
this->sigma_y += i;
}
// Calculate the summation of XX
print("Calculating sigma for X*X");
for (double i : this->XX)
{
this->sigma_XX += i;
}
// Calculate the summation of Xy
print("Calculating sigma for X*y");
for (double i : this->Xy)
{
this->sigma_Xy += i;
}
}
// Calculate our slope - m
void simple_linear_regression::calculate_slope()
{
print("Calculating slope");
this->m = ((this->N * this->sigma_Xy) - (this->sigma_X * this->sigma_y)) / ((this->N * this->sigma_XX) - (this->sigma_X * this->sigma_X));
}
// Calculate our intercept (or) bias
void simple_linear_regression::calculate_bias()
{
print("Calculating Intercept (or) Bias");
this->b = (sigma_y - (this->m * this->sigma_X)) / this->N;
}
// Constructor without parameter
simple_linear_regression::simple_linear_regression()
{
//Lets keep it in this way
this->verbose = DEBUG;
}
// Constructor to load existing model
simple_linear_regression::simple_linear_regression(std::string model_name)
{
std::ifstream file;
file.open(model_name);
if (!file.is_open()) throw "Model cannot be opened!";
std::stringstream stream;
stream << file.rdbuf();
std::string values = stream.str();
unsigned int index = values.find(",");
std::string _m = "";
std::string _b = "";
for (unsigned int i = 0; i < index; i++)
{
_m += values[i];
}
for (unsigned int i = index+1; i < values.size(); i++)
{
_b += values[i];
}
this->m = std::stod(_m);
this->b = std::stod(_b);
}
// constructor for start training new model
simple_linear_regression::simple_linear_regression(std::vector<double> X, std::vector<double> y, unsigned short verbose)
{
this->X = X;
this->y = y;
this->verbose = verbose;
}
//Copy Constructor
simple_linear_regression::simple_linear_regression(const simple_linear_regression& copyFromThis)
{
this->X = copyFromThis.X;
this->y = copyFromThis.y;
this->verbose = copyFromThis.verbose;
}
//Move Constructor
simple_linear_regression::simple_linear_regression(simple_linear_regression&& moveFromThis)
{
this->X = moveFromThis.X;
this->y = moveFromThis.y;
this->verbose = moveFromThis.verbose;
moveFromThis.X.clear();
moveFromThis.y.clear();
}
//Copy Assignment
simple_linear_regression& simple_linear_regression::operator=(const simple_linear_regression& copyFromThis)
{
this->X = copyFromThis.X;
this->y = copyFromThis.y;
this->verbose = copyFromThis.verbose;
return *this;
}
//Move Assignment
simple_linear_regression& simple_linear_regression::operator=(simple_linear_regression&& moveFromThis)
{
this->X = moveFromThis.X;
this->y = moveFromThis.y;
this->verbose = moveFromThis.verbose;
moveFromThis.X.clear();
moveFromThis.y.clear();
return *this;
}
// method to train our model, where all methods merges.
void simple_linear_regression::fit()
{
print("Training");
this->calculate_N();
this->x_square();
this->x_cross_y();
this->calculate_sigma();
this->calculate_slope();
this->calculate_bias();
if(TrainingFinish)
{
TrainingFinish(true);
std::cout<<"connected.."<<std::endl;
}
print("Model has been trained :)");
}
// Method used for predicting future values
double simple_linear_regression::predict(double _X)
{
return (this->m * _X) + this->b;
}
// A method used to save model so that we do not need to retrain it
void simple_linear_regression::save_model(std::string file_name)
{
std::ofstream file;
file.open(file_name);
if (!file.is_open()) throw "Model cannot be saved because file cannot be opened to write.Check for permissions and make sure the file is not open!";
file << this->m << "," << this->b;
file.close();
}
void simple_linear_regression::Train()
{
std::lock_guard<std::mutex> LG(_trainingMutex);
X = GetInput();
y = GetOutput();
TrainFuture = std::async(std::launch::async,&simple_linear_regression::fit,this);
}
std::vector<double> simple_linear_regression::Predict(std::vector<double> predictThis)
{
std::lock_guard<std::mutex> LG(_trainingMutex);
return {predict(predictThis[0])};
}