An example showing the plot_pca_2d_projection method used by a scikit-learn PCA object

In this example, we'll be plotting the 2D-projection of the embeddings fitted by Principal Component Analysis (PCA) algorithm. First, we'll call a PCA instance and fit the data, then pass this fitted instance with the dataset into the scikitplot.decomposition.plot_pca_2d_projection method.


In [1]:
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris as load_data
from sklearn import preprocessing
import matplotlib.pyplot as plt

# Import scikit-plot
import scikitplot as skplt

%pylab inline
pylab.rcParams['figure.figsize'] = (14, 14)


Populating the interactive namespace from numpy and matplotlib

In [2]:
#Loading the data
data = load_data()

#Encode labels to y
encode=preprocessing.LabelEncoder()
encode.fit(data.target_names)

X = data.data   
y = encode.inverse_transform(data.target)

# Create a PCA instance and fit
pca = PCA(random_state=1)
pca.fit(X)


Out[2]:
PCA(copy=True, iterated_power='auto', n_components=None, random_state=1,
  svd_solver='auto', tol=0.0, whiten=False)

In [3]:
# Plot!
skplt.decomposition.plot_pca_2d_projection(pca, X, y, biplot=True, feature_labels=data.feature_names)
plt.show()