Skip to content

Instantly share code, notes, and snippets.

@AVJdataminer
Created May 21, 2019 22:28
Show Gist options
  • Save AVJdataminer/7696453c2e9b635c857b392f792bf36f to your computer and use it in GitHub Desktop.
Save AVJdataminer/7696453c2e9b635c857b392f792bf36f to your computer and use it in GitHub Desktop.
import sklearn.decomposition
pca = sklearn.decomposition.PCA()
pca.fit(X)
variances = pca.explained_variance_ratio_
def select_n_components(var_ratio, goal_var: float) -> int:
total_variance = 0.0
n_components = 0
# For the explained variance of each feature:
for explained_variance in var_ratio:
# Add the explained variance to the total
total_variance += explained_variance
# Add one to the number of components
n_components += 1
if total_variance >= goal_var:
# End the loop
break
# Return the number of components
return n_components
pca = sklearn.decomposition.PCA(n1)
pca.fit(X)
XP=pca.components_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment