Principal Component Analysis applied to the Iris dataset.
See here for more information on this dataset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | print (__doc__) # Code source: Ga Varoquaux # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from sklearn import decomposition from sklearn import datasets np.random.seed( 5 ) centers = [[ 1 , 1 ], [ - 1 , - 1 ], [ 1 , - 1 ]] iris = datasets.load_iris() X = iris.data y = iris.target fig = plt.figure( 1 , figsize = ( 4 , 3 )) plt.clf() ax = Axes3D(fig, rect = [ 0 , 0 , . 95 , 1 ], elev = 48 , azim = 134 ) plt.cla() pca = decomposition.PCA(n_components = 3 ) pca.fit(X) X = pca.transform(X) for name, label in [( 'Setosa' , 0 ), ( 'Versicolour' , 1 ), ( 'Virginica' , 2 )]: ax.text3D(X[y = = label, 0 ].mean(), X[y = = label, 1 ].mean() + 1.5 , X[y = = label, 2 ].mean(), name, horizontalalignment = 'center' , bbox = dict (alpha = . 5 , edgecolor = 'w' , facecolor = 'w' )) # Reorder the labels to have colors matching the cluster results y = np.choose(y, [ 1 , 2 , 0 ]).astype(np. float ) ax.scatter(X[:, 0 ], X[:, 1 ], X[:, 2 ], c = y, cmap = plt.cm.spectral) ax.w_xaxis.set_ticklabels([]) ax.w_yaxis.set_ticklabels([]) ax.w_zaxis.set_ticklabels([]) plt.show() |
Total running time of the script: (0 minutes 0.115 seconds)
Download Python source code:
plot_pca_iris.py
Download IPython notebook:
plot_pca_iris.ipynb
Please login to continue.