特征提取:PCA主成分分析法和NMF非负矩阵分解法
作者:互联网
1 from sklearn.datasets import load_wine 2 from sklearn.preprocessing import StandardScaler 3 wine=load_wine() 4 X,y=wine.data,wine.target 5 scaler=StandardScaler() 6 X_scaled=scaler.fit_transform(X)
1 from sklearn.decomposition import PCA 2 pca=PCA(n_components=2) 3 pca.fit(X_scaled) 4 X_pca=pca.transform(X_scaled) 5 X0=X_pca[wine.target==0] 6 X1=X_pca[wine.target==1] 7 X2=X_pca[wine.target==2]
1 import matplotlib.pyplot as plt 2 plt.scatter(X0[:,0],X0[:,1],c='b',s=60,edgecolors='k') 3 plt.scatter(X1[:,0],X1[:,1],c='g',s=60,edgecolors='k') 4 plt.scatter(X2[:,0],X2[:,1],c='r',s=60,edgecolors='k') 5 plt.legend(wine.target_names,loc='best') 6 plt.xlabel("component 1") 7 plt.ylabel("component 2") 8 plt.show()
1 plt.matshow(pca.components_,cmap='plasma') 2 plt.yticks([0,1],['component 1','component 2']) 3 plt.colorbar() 4 plt.xticks(range(len(wine.feature_names)),wine.feature_names,rotation=20,ha='left') 5 plt.show()
1 from sklearn.decomposition import NMF 2 from sklearn.preprocessing import MinMaxScaler 3 mms=MinMaxScaler() 4 nmf=NMF(n_components=2) 5 X_scaled=mms.fit_transform(X) 6 nmf.fit(X_scaled) 7 X_nmf=nmf.transform(X_scaled) 8 print(X_nmf.shape) 9 X0=X_nmf[wine.target==0] 10 X1=X_nmf[wine.target==1] 11 X2=X_nmf[wine.target==2]
1 import matplotlib.pyplot as plt 2 plt.scatter(X0[:,0],X0[:,1],c='b',s=60,edgecolors='k') 3 plt.scatter(X1[:,0],X1[:,1],c='g',s=60,edgecolors='k') 4 plt.scatter(X2[:,0],X2[:,1],c='r',s=60,edgecolors='k') 5 plt.legend(wine.target_names,loc='best') 6 plt.xlabel("component 1") 7 plt.ylabel("component 2") 8 plt.show()
标签:nmf,plt,target,非负,NMF,特征提取,import,pca,wine 来源: https://www.cnblogs.com/St-Lovaer/p/12269600.html