-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPCAEigen.cpp
42 lines (34 loc) · 1.34 KB
/
PCAEigen.cpp
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
// Calistirmak icin g++ PCAEigen.cpp -I./eigen-git-mirror
// eigen-git-mirror'i indirmek icin: https://github.com/eigenteam/eigen-git-mirror
#include<iostream>
#include<Eigen/Dense>
using Eigen::MatrixXd;
int main(){
Eigen::MatrixXd m(2,2);
m(0,1) = 1;
m(0,0) = 0.5;
// Matrisin aritmetik ortalamasi
std::cout << m.mean() << std::endl;
// Matris sutunlarinin aritmetik ortalamasi
std::cout << m.colwise().mean() << std::endl;
// 0.25, 0.5
// https://stackoverflow.com/questions/42811084/eigen-subtracting-vector-from-matrix-columns
Eigen::VectorXd mean_vector = m.colwise().mean();
// Merkezleme
// Amacimiz sutunlardan sutun ortalamalarini cikarmak:
// 0.25 0.5
// -0.25 -0.5
std::cout << m << std::endl;
std::cout << mean_vector << std::endl;
std::cout << m.rowwise() - mean_vector.transpose() << std::endl;
Eigen::MatrixXd centered = m.rowwise() - mean_vector.transpose();
// Kovaryans Matrisi Cikarimi
Eigen::MatrixXd cov = (centered.adjoint() * centered) / ( m.rows() - 1 ) ;
std::cout << cov << std::endl;
// Eigenvector ayristirmasi
Eigen::SelfAdjointEigenSolver<MatrixXd> eig_solver(2);
eig_solver.compute(cov);
std::cout << eig_solver.eigenvalues() << std::endl;
std::cout << eig_solver.eigenvectors() << std::endl;
return 0;
}