plot_pca_2d_projection#
- scikitplot.api.decomposition.plot_pca_2d_projection(clf, X, y, *, biplot=False, feature_labels=None, dimensions=[0, 1], label_dots=False, model_type=None, title='PCA 2-D Projection', ax=None, fig=None, figsize=None, title_fontsize='large', text_fontsize='medium', cmap='nipy_spectral', **kwargs)[source]#
Plots the 2-dimensional projection of PCA on a given dataset.
- Parameters:
- clfobject
Fitted PCA instance that can
transform
given data set into 2 dimensions.- Xarray-like, shape (n_samples, n_features)
Feature set to project, where n_samples is the number of samples and n_features is the number of features.
- yarray-like, shape (n_samples) or (n_samples, n_features)
Target relative to X for labeling.
- biplotbool, optional, default=False
If True, the function will generate and plot biplots. If False, the biplots are not generated.
- feature_labelsarray-like, shape (n_features), optional, default=None
List of labels that represent each feature of X. Its index position must also be relative to the features. If None is given, labels will be automatically generated for each feature (e.g. “variable1”, “variable2”, “variable3” …).
- titlestr, optional, default=’PCA 2-D Projection’
Title of the generated plot.
- axmatplotlib.axes.Axes, optional, default=None
The axes upon which to plot. If None, a new set of axes is created.
- figsizetuple of int, optional, default=None
Size of the figure (width, height) in inches.
- title_fontsizestr or int, optional, default=’large’
Font size for the plot title.
- text_fontsizestr or int, optional, default=’medium’
Font size for the text in the plot.
- cmapstr or matplotlib.colors.Colormap, optional, default=’viridis’
Colormap used for plotting the projection. See Matplotlib Colormap documentation for available options: https://matplotlib.org/users/colormaps.html
- Returns:
- matplotlib.axes.Axes
The axes on which the plot was drawn.
Examples
>>> from sklearn.decomposition import PCA >>> from sklearn.datasets import load_iris as data_3_classes >>> import scikitplot as skplt >>> X, y = data_3_classes(return_X_y=True, as_frame=True) >>> pca = PCA(random_state=0).fit(X) >>> skplt.decomposition.plot_pca_2d_projection(pca, X, y, biplot=True, feature_labels=X.columns.tolist());
(
Source code
,png
)>>> from sklearn.discriminant_analysis import LinearDiscriminantAnalysis >>> from sklearn.datasets import load_iris as data_3_classes >>> import scikitplot as skplt >>> X, y = data_3_classes(return_X_y=True, as_frame=True) >>> clf = LinearDiscriminantAnalysis().fit(X, y) >>> skplt.decomposition.plot_pca_2d_projection(clf, X, y, biplot=True, feature_labels=X.columns.tolist());
(
Source code
,png
)
Gallery examples#
plot_pca_2d_projection with examples