PCA principal component Analysis method, LDA linear discriminant analysis method, can be considered as supervised data dimensionality reduction. The following code implements two ways to reduce the dimension, respectively:
Print(__doc__)ImportMatplotlib.pyplot as Plt fromSklearnImportDatasets fromSklearn.decompositionImportPCA fromSklearn.discriminant_analysisImportLineardiscriminantanalysisiris=Datasets.load_iris () X=Iris.datay=Iris.targettarget_names=IRIS.TARGET_NAMESPCA= PCA (n_components=2) X_r=pca.fit (x). Transform (x) LDA= Lineardiscriminantanalysis (n_components=2) X_r2=lda.fit (x, y). Transform (x)#Percentage of Variance explained for each of the componentsPrint('explained Variance ratio (first):%s'%str (PCA.EXPLAINED_VARIANCE_RATIO_)) plt.figure () forc, I, target_nameinchZip"RGB", [0, 1, 2], Target_names): Plt.scatter (X_r[y= = I, 0], x_r[y = = i, 1], c=c, label=target_name) Plt.legend () Plt.title ('PCA of IRIS dataset') plt.figure () forc, I, target_nameinchZip"RGB", [0, 1, 2], Target_names): Plt.scatter (X_r2[y= = I, 0], x_r2[y = = i, 1], c=c, label=target_name) Plt.legend () Plt.title ('LDA of IRIS dataset') plt.show ()
The results are as follows
Comparison of PCA and LDA dimensionality reduction