Skip to content

Instantly share code, notes, and snippets.

@ImadDabbura
Created August 3, 2018 20:31
Show Gist options
  • Select an option

  • Save ImadDabbura/072be81d733ed0c59e4fe334b40dcaee to your computer and use it in GitHub Desktop.

Select an option

Save ImadDabbura/072be81d733ed0c59e4fe334b40dcaee to your computer and use it in GitHub Desktop.
# Build PCA using standarized trained data
pca = PCA(n_components=None, svd_solver="full")
pca.fit(StandardScaler().fit_transform(X_train))
cum_var_exp = np.cumsum(pca.explained_variance_ratio_)
plt.figure(figsize=(12, 6))
plt.bar(range(1, 18), pca.explained_variance_ratio_, align="center",
color='red', label="Individual explained variance")
plt.step(range(1, 18), cum_var_exp, where="mid", label="Cumulative explained variance")
plt.xticks(range(1, 18))
plt.legend(loc="best")
plt.xlabel("Principal component index", {"fontsize": 14})
plt.ylabel("Explained variance ratio", {"fontsize": 14})
plt.title("PCA on training data", {"fontsize": 16});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment