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)
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]:
In [3]:
# Plot!
skplt.decomposition.plot_pca_2d_projection(pca, X, y, biplot=True, feature_labels=data.feature_names)
plt.show()