Last active
January 20, 2018 20:23
-
-
Save hempnall/3ac2c0cf154080371efe745f091c6d4e to your computer and use it in GitHub Desktop.
Principal Component Analysis in various Languages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <dlib/statistics/dpca.h> | |
#include <dlib/statistics/dpca_abstract.h> | |
#include <dlib/matrix.h> | |
#include <initializer_list> | |
using namespace dlib; | |
int main() | |
{ | |
matrix< double , 2,1 > vectors_for_pca[] | |
{ | |
{ 4.0 , 3.0 } , | |
{ 3.0 , 2.25 } , | |
{ 2.0 , 1.5 } , | |
{ 1.03 , 0.7533 } , | |
}; | |
//std::cout << "data:\nNR=" << vectors_for_pca.nr() << "\nNC=" << vectors_for_pca.nc() << "\n"; | |
dlib::discriminant_pca< matrix< double> > pca; | |
for (int i=0 ; i< 4 ;++i) { | |
pca.add_to_total_variance( vectors_for_pca[i] ); | |
} | |
matrix<double> pca_out(2,2); | |
matrix<double> pca_evout(2,1); | |
// the third value acts as a threshold for filtering out the eigenvalues | |
// 1.0 --> return all values | |
// 0.9 --> return values until the cumulative sum of eigenvalues (when sorted descending) > 90% total eigen values | |
pca.dpca_matrix(pca_out, pca_evout,0.99); | |
// in vector size is the number if fields in each entry | |
auto vs = pca.in_vector_size(); | |
std::cout << "in_vector_size: " << vs << | |
"\nprincipal_transformation_matrix: \n" << pca_out << | |
"\neigenvalues:\n " << pca_evout << "\n"; | |
std::cout << "eigen value matrix of size 2:\n"; | |
auto m = pca.dpca_matrix_of_size(2); | |
std::cout << m; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.decomposition import PCA | |
p = PCA( 2 ) | |
arr = [ | |
[ 4 , 3 ], | |
[ 3 , 2.25 ] , | |
[ 2 , 1.5 ] , | |
[ 1 , 0.75 ] ] | |
p.fit( arr ) | |
# eigenvalues.. | |
print p.explained_variances_ | |
# eigenvectors.. | |
print p.components_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment