-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
144 lines (132 loc) · 4.46 KB
/
utils.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
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
#ifndef UTILS_H
#define UTILS_H
#include <memory>
#include <string>
#include <set>
#include <list>
// #include
#include <iostream>
#include <ostream>
#include <istream>
#include <sstream>
#include <fstream>
#include <regex>
#define EIGEN_USE_BLAS
#include <Eigen/Core>
using std::shared_ptr;
using std::unique_ptr;
using std::make_shared;
using std::cout;
using std::cin;
using std::set;
namespace Scaler{
const int STD = 0;
const int MINMAX = 1;
const int NORMALIZE = 2;
const int COL = 0;
const int ROW = 1;
const int NORM_L2 = 0;
const int NORM_L1 = 1;
const int NORM_MAX = 2;
template<typename T>
void scale(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& feature, int flags[])
{
if (flags[1] == COL)
{
if (flags[0] == STD)
{
auto mean(feature.colwise().mean());
feature.rowwise() -= mean;
auto std_dev = feature.colwise().norm() / feature.rows();
feature.array().rowwise() /= std_dev.array();
}
else if (flags[0] == MINMAX)
{
auto arr = feature.array();
auto min = arr.colwise().minCoeff();
auto range = arr.colwise().maxCoeff() - min;
arr.rowwise() -= min;
arr.rowwise() /= range;
}
else if(flags[0] == NORMALIZE)
{
auto norm = flags[2];
auto arr = feature.array();
if (norm==NORM_L2)
arr.rowwise() /= arr.colwise().norm();
else if (norm==NORM_L1)
arr.rowwise() /= arr.abs().colwise().sum();
else if (norm==NORM_MAX)
arr.rowwise() /= arr.abs().colwise().maxCoeff();
}
}
else{
flags[1] = COL;
feature.transposeInPlace();
scale(feature, flags);
feature.transposeInPlace();
}
}
template<typename T>
void standardize(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& feature, int flag = COL)
{
int args[] = {STD, flag};
scale(feature, args);
}
template<typename T>
void minmax(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& feature, int flag = COL)
{
int args[] = {MINMAX, flag};
scale(feature, args);
}
template<typename T>
void normalize(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& feature, int flag = COL, int norm = NORM_L2)
{
int args[] = {NORMALIZE, flag, norm};
scale(feature, args);
}
}
template <typename T>
int loadMatrix(const std::string& path, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &M, const std::string& pattern = " ")
{
std::ifstream str(path);
std::string line, ele;
std::regex r(pattern);
if (str.is_open())
{
std::vector<std::vector<std::string>> str_matrix;
while(std::getline(str, line))
{
str_matrix.push_back(std::vector<std::string>(std::sregex_token_iterator(line.begin(), line.end(), r, -1),
std::sregex_token_iterator()));
}
size_t row = str_matrix.size();
size_t col = str_matrix[0].size();
if (row > 0 && col > 0)
{
M = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>(row, col);
for (size_t i = 0; i < row; ++i)
for (size_t j = 0; j < col; ++j)
M(i, j) = std::stod(str_matrix[i][j]);
return 1;
}
}
return 0;
}
int loadMatrix_2(const std::string& path, Eigen::MatrixXd &M, const std::string& pattern = " ");
inline double calculate_error(const Eigen::MatrixXd& predict, const Eigen::MatrixXd& gts)
{
return (predict.array() - gts.array()).abs().sum();
}
inline double hinge_loss(const Eigen::MatrixXd& predict, const Eigen::MatrixXd& gts)
{
auto mask = (predict.array() * gts.array() > 0).select(Eigen::MatrixXd::Ones(gts.rows(), 1),
Eigen::MatrixXd::Zero(gts.rows(), 1));
return ((predict.array() - gts.array()).abs() * mask.array()).sum();
}
inline double classify_accuracy(const Eigen::MatrixXd& classify_result, const Eigen::MatrixXd& gt)
{
return (classify_result.array() == gt.array()).select(Eigen::MatrixXd::Ones(gt.rows(), gt.cols()),
Eigen::MatrixXd::Zero(gt.rows(),gt.cols())).sum() / gt.count();
}
#endif