SNE降维与可视化
from sklearn import datasets digits = datasets.load_digits(n_class=5) X = digits.data y = digits.target from sklearn.manifold import TSNE from sklearn.decomposition import PCA import matplotlib.pyplot as plt tsne =TSNE(n_components=2, init='pca', random_state=0) '''n_components维度降为2维,init设置embedding的初始化方式,可选random或者pca''' X_tsne = tsne.fit_transform(X) X_pca = PCA().fit_transform(X) plt.figure(figsize=(9, 5)) plt.subplot(121) plt.scatter(X_tsne[:, 0], X_tsne[:, 1],c=digits.target,label='X_tsne') plt.legend(loc='upper left') plt.subplot(122) plt.scatter(X_pca[:, 0], X_pca[:, 1], c=digits.target,label='X_pca') plt.legend(loc='upper left') plt.show()