-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLinearRegressionNode.cpp
More file actions
201 lines (159 loc) · 6.2 KB
/
LinearRegressionNode.cpp
File metadata and controls
201 lines (159 loc) · 6.2 KB
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
#include "LinearRegressionNode.hpp"
LinearRegressionNode::LinearRegressionNode(std::string name)
:Node(name){}
LinearRegressionNode::~LinearRegressionNode(){}
//Checking if target is selected
bool LinearRegressionNode::IsTargetSelected() const{
return !(targetColumnName.compare("") == 0);
}
//Getters
arma::Row<double> LinearRegressionNode::TargetPredictions() const{
return targetPredictions;
}
arma::vec LinearRegressionNode::Params() const{
return params;
}
double LinearRegressionNode::RssScore() const{
return rssScore;
}
//Setters
void LinearRegressionNode::SetTargetPredictions(const arma::Row<double> predictions){
targetPredictions = predictions;
}
void LinearRegressionNode::SetRssScore(const double& score){
rssScore = score;
}
void LinearRegressionNode::SetParams(const arma::vec& parameters){
params = parameters;
}
void LinearRegressionNode::setTarget(std::string targetName) {
targetColumnName = targetName;
}
void LinearRegressionNode::SetTargetColumn() {
std::vector<std::string> columns = inputDataTable->ColumnNames();
std::map<std::string, std::set<std::string>> map = inputDataTable->CategoricalValues();
size_t index = 0;
//Finding the column with targetColumnName
for(unsigned i = 0; i < columns.size(); i++) {
if(0 == columns[i].compare(targetColumnName)) {
if(map.find(targetColumnName) != map.end()) {
std::cout << "Greska! Izabrana kolona sa kategorickom vrednoscu!" << std::endl;
return;
}
else {
auto matrix = inputDataTable->DataMatrix();
targetColumn.set_size(matrix.n_rows);
std::copy(matrix.begin_col(index), matrix.end_col(index), targetColumn.begin());
}
break;
}
//If the column we just passed was categorical, we increase index by the number of values of that column in order to skip binarized columns
if(map.find(columns[i]) != map.end()) {
index += map.at(columns[i]).size();
} else {
index++;
}
}
}
//Calculating rssScore (sum(trueValue(i) - predictedValue(i))^2/numberOfTuples)
void LinearRegressionNode::RSS(arma::Row<double> values, arma::Row<double> predictions){
size_t nRows = values.size();
double sum = 0;
for(unsigned long i = 0; i < nRows; i++){
sum += (values[i] - predictions[i])*(values[i] - predictions[i]);
}
SetRssScore(sum);
}
void LinearRegressionNode::run(){
//Checking to see of targetColumnName is selected
if (!this->IsTargetSelected()){
std::cout << "Target variable not selected" << std::endl;
DataTable dt = *InputDataTable();
this->setOutDataTable(dt);
return;
}
//Select targetColumn
this->SetTargetColumn();
//Filter targetColumn from data
DataTable dt = filter(targetColumnName);
arma::mat data = dt.DataMatrix();
//If not partitioned
if (!dt.IsPartitioned()){
//Make a model from whole data
data = trans(data);
mlpack::regression::LinearRegression lr(data, targetColumn);
//Get and set parameters of regressioin
SetParams(lr.Parameters());
std::cout << "Parameters: " << std::endl;
std::cout << Params() << std::endl;
//Calculate predictions and rrsScore
arma::Row<double> predictions;
lr.Predict(data, predictions);
std::cout << "Predictions: " << std::endl;
std::cout << predictions << std::endl;
RSS(targetColumn, predictions);
std::cout << RssScore() << std::endl;
SetTargetPredictions(predictions);
} //If there is partition
else {
//Split data into test and train data and predictions
arma::mat testData(dt.TestSize(), data.n_cols);
arma::mat trainData(data.n_rows - dt.TestSize(), data.n_cols);
arma::Col<double> testTarget(dt.TestSize());
arma::Col<double> trainTarget(data.n_rows - dt.TestSize());
std::vector<bool> partition = dt.Partition();
//Fill test and train sets
unsigned long train_index = 0;
unsigned long test_index = 0;
for(unsigned long i = 0; i < data.n_rows; i++){
if (partition[i]){
testTarget[test_index] = targetColumn[i];
for(unsigned long j = 0; j < data.n_cols; j++){
testData(test_index, j) = data(i, j);
}
test_index++;
} else {
trainTarget[train_index] = targetColumn[i];
for(unsigned long j = 0; j < data.n_cols; j++){
trainData(train_index, j) = data(i, j);
}
train_index++;
}
}
trainData = trans(trainData);
testData = trans(testData);
//Make model from train data
mlpack::regression::LinearRegression lr(trainData, trainTarget);
//Get and set parameters
SetParams(lr.Parameters());
std::cout << "Parameters: " << std::endl;
std::cout << Params() << std::endl;
//Calculate predictions and rssScore from test data
arma::Row<double> predictions;
lr.Predict(testData, predictions);
std::cout << "Predictions: " << std::endl;
std::cout << predictions << std::endl;
RSS(testTarget, predictions);
std::cout << RssScore() << std::endl;
//Calculate predictions and rssScore from whole data
arma::Row<double> allPredictions;
data = trans(data);
lr.Predict(data, allPredictions);
SetTargetPredictions(allPredictions);
std::cout << TargetPredictions() << std::endl;
}
//Set output
std::string result = "Paramteres:";
arma::vec parameters = Params();
for (unsigned long i = 0; i < parameters.size(); i++){
result += " ";
result += std::to_string(parameters[i]);
}
result += "\n";
result += "RSS: ";
result += std::to_string(RssScore());
result += "\n";
setOutputMessage(result);
DataTable dataTable = *InputDataTable();
this->setOutDataTable(dataTable);
}