-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathETL.h
More file actions
174 lines (148 loc) · 5.07 KB
/
ETL.h
File metadata and controls
174 lines (148 loc) · 5.07 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
//
// Created by Ryan.Zurrin001 on 12/16/2021.
//
#ifndef PHYSICSFORMULA_ETL_H
#define PHYSICSFORMULA_ETL_H
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <utility>
#include <vector>
#include <cmath>
#include "MatrixND.h"
/**
* @class ETL
* @details Extract transform and load, class for reading in data from a csv
* file and transforms it for use in machine learning and data analysis
* @author AI coding, implemented by Ryan Zurrin
* dateBuilt 5/26/2021
* lastEdit 5/26/2021
*/
template <typename T>
class ETL
{
std::string dataset;
std::string delimiter;
bool header;
public:
ETL()
{
dataset = "";
delimiter = " ";
header = false;
}
ETL(std::string data, std::string separator, bool head) : dataset(std::move(data)), delimiter(std::move(separator)), header(head)
{}
std::vector<std::vector<std::string>> readCSV();
MatrixND<double> CSVtoMatrix(std::vector<std::vector<std::string>> ds, int rows, int cols);
static MatrixND<T> Normalize(MatrixND<T> data, bool normalizeTarget);
//auto Mean(Eigen::MatrixXd data) -> decltype(data.colwise().mean());
//auto Std(Eigen::MatrixXd data) -> decltype(((data.array().square().colwise().sum())/(data.rows()-1)).sqrt());
std::tuple<MatrixND<T>,MatrixND<T>,MatrixND<T>,MatrixND<T>>
TrainTestSplit(MatrixND<T> data, float train_size)const;
void VectorToFile(std::vector<float> vector, std::string filename)const;
static void EigenToFile(MatrixND<T> data,const std::string& filename);
};
#endif //PHYSICSFORMULA_ETL_H
template <typename T>
std::vector<std::vector<std::string>> ETL<T>::readCSV()
{
std::ifstream file(dataset);
std::vector<std::vector<std::string>> dataString;
std::string line = "";
while(getline(file,line)){
std::vector<std::string> vec;
std::stringstream ss(line);
std::string token;
while(getline(ss,token,delimiter[0])){
vec.push_back(token);
}
dataString.push_back(vec);
}
file.close();
//print out the data
// for(int i = 0; i < dataString.size(); i++){
// for(int j = 0; j < dataString[i].size(); j++){
// std::cout << dataString[i][j] << " ";
// }
// std::cout << std::endl;
// }
return dataString;
}
template <typename T>
MatrixND<double> ETL<T>::CSVtoMatrix(std::vector<std::vector<std::string>>
ds, int rows, int cols)
{
if(header){
rows = rows - 1;
}
MatrixND<double> data(rows,cols);
// adding the data from vector to matrix
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; ++j){
data(i,j) = std::stod(ds[i][j]);
}
}
return data;
}
template <typename T>
inline std::tuple<MatrixND<T>,MatrixND<T>,MatrixND<T>,MatrixND<T>>
ETL<T>::TrainTestSplit(MatrixND<T> data, float train_size)const
{
const int rows = static_cast<int>( data.rows());
const int train_rows = static_cast<int>( round(train_size*rows));
const int test_rows = rows - train_rows;
MatrixND<T> train = data.topRows(train_rows);
MatrixND<T> X_train = train.leftCols(data.cols()-1);
MatrixND<T> y_train = train.rightCols(1);
MatrixND<T> test = data.bottomRows(test_rows);
MatrixND<T> X_test = test.leftCols(data.cols()-1);
MatrixND<T> y_test = test.rightCols(1);
return std::make_tuple(X_train, y_train, X_test, y_test);
}
template <typename T>
inline auto Mean(MatrixND<T> data) -> decltype(data.colwise().mean())
{
return data.colwise().mean();
}
template <typename T>
inline auto Std(MatrixND<T> data) ->
decltype(((data.array().square().colwise().sum())/(data.rows()-1)).sqrt())
{
return ((data.array().square().colwise().sum())/(data.rows()-1)).sqrt();
}
template <typename T>
inline MatrixND<T> ETL<T>::Normalize(MatrixND<T> data, bool normalizeTarget)
{
MatrixND<T> dataNorm;
if(normalizeTarget==true) {
dataNorm = data;
} else {
dataNorm = data.leftCols(data.cols()-1);
}
auto mean = dataNorm.colwise().mean();
MatrixND<T> scaled_data = dataNorm.rowwise() - mean;
auto std = ((scaled_data.array().square().colwise().sum()) /
(scaled_data.rows() - 1)).sqrt();
MatrixND<T> norm = scaled_data.array().rowwise()/std;
if(normalizeTarget==false) {
norm.conservativeResize(norm.rows(), norm.cols()+1);
norm.col(norm.cols()-1) = data.rightCols(1);
}
return norm;
}
template <typename T>
inline void ETL<T>::VectorToFile(std::vector<float> vector, std::string filename)const
{
std::ofstream output_file(filename);
std::ostream_iterator<float> output_iterator(output_file, "\n");
std::copy(vector.begin(), vector.end(), output_iterator);
}
template <typename T>
inline void ETL<T>::EigenToFile(MatrixND<T> data,const std::string& filename)
{
std::ofstream output_file(filename);
if(output_file.is_open()){
output_file << data << "\n";
}
}