-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsr.h
86 lines (66 loc) · 1.86 KB
/
lsr.h
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
// SWAMI KARUPPASWAMI THUNNAI
#pragma once
#define NODEBUG 0
#define DEBUG 1
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <future>
#include <mutex>
#include "dataioclass.h"
#include "itrainpredict.h"
/*
Implementation of simple linear regression or Least Squares Regression
Written By: Visweswaran N @ 2019-08-26
*/
class simple_linear_regression : public DataIOClass, public ITrainPredict
{
private:
// ~~~~~~~~~~~~~ PRIVATE VARIABLES ~~~~~~~~~~~~
unsigned int N=0;
// Independent and dependent variable
std::vector<double> X;
std::vector<double> y;
// XX and Xy
std::vector<double> XX;
std::vector<double> Xy;
// To print into console
bool verbose;
// Summation
double sigma_X = 0.0;
double sigma_y = 0.0;
double sigma_XX = 0.0;
double sigma_Xy = 0.0;
// Slope
double m = 0.0;
// Bias (or) Intercept
double b = 0.0;
// ~~~~~~~~~~ Methods ~~~~~~~~~~~~~
void print(std::string message);
void calculate_N();
void x_square();
void x_cross_y();
void calculate_sigma();
void calculate_slope();
void calculate_bias();
public:
simple_linear_regression();
simple_linear_regression(std::string model_name);
simple_linear_regression(std::vector<double> X, std::vector<double> y, unsigned short verbose);
simple_linear_regression(const simple_linear_regression& copyFromThis);
simple_linear_regression(simple_linear_regression&& moveFromThis);
simple_linear_regression& operator=(const simple_linear_regression& copyFromThis);
simple_linear_regression& operator=(simple_linear_regression&& moveFromThis);
public: //Overrited Interfaces
void Train() override;
std::vector<double> Predict(std::vector<double>) override;
private:
std::future<void> TrainFuture;
std::mutex _trainingMutex;
protected: //Functionalities
void fit();
double predict(double _X);
void save_model(std::string file_name);
};