plot_pca_2d_projection with examples#
An example showing the plot_pca_2d_projection
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=True)
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)
45
46 # Create an instance of the LogisticRegression
47 # model = LogisticRegression(max_iter=int(1e5), random_state=0).fit(X_train, y_train)
48
49 # Perform predictions
50 # y_val_prob = model.predict_proba(X_val)
Plot!#
56 # Plot!
57 ax = sp.decomposition.plot_pca_2d_projection(
58 pca,
59 X_train,
60 y_train,
61 biplot=True,
62 feature_labels=X.columns.tolist(),
63 save_fig=True,
64 save_fig_filename="",
65 # overwrite=True,
66 add_timestamp=True,
67 # verbose=True,
68 )

Total running time of the script: (0 minutes 0.552 seconds)
Related examples