-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlfda.cpp
More file actions
227 lines (177 loc) · 6.95 KB
/
lfda.cpp
File metadata and controls
227 lines (177 loc) · 6.95 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <algorithm>
#include <numeric>
#include <unordered_set>
#include <vector>
#include "lfda.h"
#include "metrics.h"
// File scope functions
std::unordered_set<double> get_unique_classes(Eigen::VectorXd y) {
std::unordered_set<double> unique_classes;
for (int i = 0; i < y.rows(); i++) {
// if the element i.s not present in the set, insert it
if (unique_classes.find(static_cast<int>(y(i))) == unique_classes.end()) {
unique_classes.insert(static_cast<int>(y(i)));
}
}
return unique_classes;
}
Eigen::MatrixXd filter_indices_by_class(
Eigen::MatrixXd X, Eigen::VectorXd y, int label) {
std::vector<Eigen::VectorXd> X_class;
for (int i = 0; i < X.rows(); i++) {
if (y(i) == label) {
X_class.push_back(X.row(i));
}
}
Eigen::MatrixXd X_class_eigen;
X_class_eigen.resize(X_class.size(), X_class.at(0).size());
for (int i = 0; i < (int)X_class.size(); i++) {
X_class_eigen.row(i) = X_class.at(i);
}
return X_class_eigen;
}
Eigen::MatrixXd calculate_sigma(const Eigen::MatrixXd& dist, int k) {
int n = dist.rows();
Eigen::MatrixXd sigma(n, 1);
#pragma omp parallel for shared(dist) firstprivate(n, k)
for (int col = 0; col < n; ++col) {
Eigen::VectorXd column_values = dist.col(col);
std::nth_element(column_values.data(), column_values.data() + k, column_values.data() + n);
double sigma_val = std::sqrt(column_values(k));
sigma(col, 0) = std::isnormal(sigma_val) ? sigma_val : 0.0; // Check for normal value
}
return sigma;
}
// Member functions of the LFDA class
LFDA::LFDA(const int n_components,
const int k,
const EMBEDDING_TYPE embedding) :
n_components_(n_components),
k_(k),
embedding_(embedding)
{
Eigen::initParallel();
// implemented as initializer list
}
LFDA::~LFDA() {
// empty
}
void LFDA::EigenSolver(const Eigen::MatrixXd& a, const Eigen::MatrixXd& b, int dim,
Eigen::MatrixXd& eigenVectors, Eigen::VectorXd& eigenValues) {
Eigen::GeneralizedSelfAdjointEigenSolver<Eigen::MatrixXd> solver(a, b);
if (solver.info() != Eigen::Success) {
throw std::runtime_error("Eigenvalue decomposition failed.");
}
eigenValues = solver.eigenvalues().real().topRows(dim);
eigenVectors = solver.eigenvectors().real().leftCols(dim);
// Filter out negative or nan eigenvalues which indicate numerical instability
for (int i = 0; i < eigenValues.size(); ++i) {
if (eigenValues(i) < 0 || std::isnan(eigenValues(i))) {
eigenValues(i) = 0;
}
}
}
Eigen::MatrixXd LFDA::AddSmallRegularization(const Eigen::MatrixXd& a, double epsilon) {
int n = a.rows();
double reg_strength = epsilon * a.norm(); // Adjust regularization strength based on matrix norm
Eigen::MatrixXd regularization = reg_strength * Eigen::MatrixXd::Identity(n, n);
return a + regularization;
}
Eigen::MatrixXd LFDA::OuterProduct(const Eigen::MatrixXd x) {
Eigen::VectorXd sum = x.colwise().sum();
return sum * sum.transpose();
}
int LFDA::CheckNComponents(int n_features, int n_components) {
if (n_components == -1) {
return n_features;
}
if (n_components > 0 && n_components <= n_features) {
return n_components;
}
throw std::invalid_argument(
"Invalid n_components, must be in [1, "
+ std::to_string(n_features) + "]");
}
void LFDA::Fit(const Eigen::MatrixXd& Xin,
const Eigen::VectorXd& yin) {
std::shared_ptr<Eigen::MatrixXd> X = std::make_shared<Eigen::MatrixXd>(Xin);
std::shared_ptr<Eigen::VectorXd> y = std::make_shared<Eigen::VectorXd>(yin);
// Check to see if X has sufficient number of rows
if (!X || !y || X->rows() <= 2 || X->rows() != y->rows()) {
return;
}
std::unordered_set<double> unique_classes = get_unique_classes(*y);
// Check to see if X has sufficient number of features
if (X->cols() < this->n_components_) {
return;
}
if (this->k_ <= 0) {
this->k_ = std::min<int>(7, X->cols() - 1);
}
else if (this->k_ >= X->cols()) {
// k must be <= d - 1
return;
}
Eigen::MatrixXd tSb;
Eigen::MatrixXd tSw;
tSb.resize(X->cols(), X->cols());
tSw.resize(X->cols(), X->cols());
tSb.setZero(X->cols(), X->cols());
tSw.setZero(X->cols(), X->cols());
for (const int& element : unique_classes) {
Eigen::MatrixXd X_class = filter_indices_by_class(*X, *y, element);
int nc = X_class.rows();
Eigen::MatrixXd dist = Metrics::pairwise_distances(X_class, "euclidean");
int local_k = std::min<int>(this->k_, nc - 1);
Eigen::MatrixXd sigma = calculate_sigma(dist, local_k);
Eigen::MatrixXd local_scale = LFDA::OuterProduct(sigma);
Eigen::MatrixXd A = (-dist.array() / (1e-7 + local_scale.array())).exp();
#pragma omp parallel for default(none) shared(A, local_scale) collapse(2) num_threads(2)
for (int i = 0; i < A.rows(); ++i) {
for (int j = 0; j < A.cols(); ++j) {
if (local_scale(i, j) == 0) {
A(i, j) = 0.0;
}
}
}
Eigen::MatrixXd G = X_class.transpose() * (A.rowwise().sum().array().matrix() * X_class);
tSb += G / X->rows() +
(1.0 - static_cast<double>(X_class.rows()) / X->rows())
* (X_class.transpose() * X_class) + LFDA::OuterProduct(X_class);
tSw += G / X_class.rows();
}
tSb -= (LFDA::OuterProduct(*X) / X->rows()) - tSw;
tSb = (tSb + tSb.transpose()) / 2;
tSw = (tSw + tSw.transpose()) / 2;
Eigen::VectorXd eigen_values;
Eigen::MatrixXd eigen_vectors;
LFDA::EigenSolver(tSb, tSw, this->n_components_, eigen_vectors, eigen_values);
std::vector<int> idx(eigen_values.size());
std::iota(idx.begin(), idx.end(), 0);
std::sort(idx.begin(), idx.end(), [&eigen_values](int i1, int i2) {return eigen_values(i1) > eigen_values(i2); });
Eigen::VectorXd sortedVals(this->n_components_);
Eigen::MatrixXd sortedVecs(eigen_vectors.rows(), this->n_components_);
for (int i = 0; i < this->n_components_; ++i) {
sortedVals(i) = eigen_values(idx[i]);
sortedVecs.col(i) = eigen_vectors.col(idx[i]);
}
eigen_values = sortedVals;
eigen_vectors = sortedVecs;
if (this->embedding_ == EMBEDDING_TYPE::WEIGHTED) {
for (int i = 0; i < this->n_components_; ++i) {
eigen_vectors.col(i) *= std::sqrt(eigen_values(i));
}
}
else if (this->embedding_ == EMBEDDING_TYPE::ORTHONORMALIZED) {
Eigen::HouseholderQR<Eigen::MatrixXd> qr(eigen_vectors);
eigen_vectors = qr.householderQ();
}
this->components_ = eigen_vectors.transpose();
return;
}
Eigen::MatrixXd LFDA::Transform(const Eigen::MatrixXd& X) {
std::cout << X.rows() << " " << X.cols() << "\n";
std::cout << this->components_.cols() << " " << this->components_.rows() << "\n";
Eigen::MatrixXd X_res = X * this->components_.transpose();
return X_res;
}