plot_pca_component_variance with examples#

An example showing the plot_pca_component_variance function used by a scikit-learn PCA object.

 9 # Authors: The scikit-plots developers
10 # SPDX-License-Identifier: BSD-3-Clause

Import scikit-plots#

16 from sklearn.datasets import (
17     load_iris as data_3_classes,
18 )
19 from sklearn.decomposition import PCA
20 from sklearn.model_selection import train_test_split
21
22 import numpy as np
23
24 np.random.seed(0)  # reproducibility
25 # importing pylab or pyplot
26 import matplotlib.pyplot as plt
27
28 # Import scikit-plot
29 import scikitplot as sp

Loading the dataset#

35 # Load the data
36 X, y = data_3_classes(return_X_y=True, as_frame=False)
37 X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.5, random_state=0)

PCA#

43 # Create an instance of the PCA
44 pca = PCA(random_state=0).fit(X_train)

Plot!#

50 # Plot!
51 ax = sp.decomposition.plot_pca_component_variance(
52     pca,
53     figsize=(9, 5),
54     save_fig=True,
55     save_fig_filename="",
56     # overwrite=True,
57     add_timestamp=True,
58     # verbose=True,
59 )
Cumulative Explained Variance Ratio by Principal Components

Tags: model-type: regression model-type: classification model-workflow: feature engineering plot-type: line level: beginner purpose: showcase

Total running time of the script: (0 minutes 0.448 seconds)

Related examples

plot_pca_2d_projection with examples

plot_pca_2d_projection with examples

plot_cumulative_gain with examples

plot_cumulative_gain with examples

plot_lift with examples

plot_lift with examples

plot_elbow with examples

plot_elbow with examples

Gallery generated by Sphinx-Gallery