Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

pca.py 780 B

You have to be logged in to leave a comment. Sign In
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
  1. '''
  2. Principal Components Analysis
  3. '''
  4. import sys
  5. from sklearn.decomposition import PCA
  6. from sklearn.preprocessing import StandardScaler
  7. import numpy as np
  8. def set_trace():
  9. """A Poor mans break point"""
  10. # without this in iPython debugger can generate strange characters.
  11. from IPython.core.debugger import Pdb
  12. Pdb().set_trace(sys._getframe().f_back)
  13. def perform_pca(x, pca_opt):
  14. components = pca_opt
  15. x_std = StandardScaler().fit_transform(x)
  16. model = PCA(n_components=components)
  17. x_reduced = model.fit_transform(x_std)
  18. var_explained = model.explained_variance_ratio_.cumsum()
  19. print components, 'components explains ', var_explained[-1] * 100, '% variance'
  20. # get_covariance_matrix(x_std)
  21. return x_reduced
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...