代码:
# 降维,高维数据降成低维的数据
# manifold 流形
from sklearn.manifold import LocallyLinearEmbedding
from sklearn.decomposition import PCA
from sklearn import datasets
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
# 默认提供的方法。生成了3维的数据。
X,y = datasets.make_swiss_roll(n_samples=1500,noise=0.05)
fig = plt.figure()
axes3D = Axes3D(fig)
axes3D.view_init(7,-80)#调整视图的方向
axes3D.scatter(X[:,0],X[:,1],X[:,2],c = y)
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x21301295d48>
output_0_1.png
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
plt.scatter(X_pca[:,0],X_pca[:,1],c = y)
<matplotlib.collections.PathCollection at 0x2130146cd08>
output_1_1.png
lle = LocallyLinearEmbedding(n_components=2,n_neighbors=10)
X_lle = lle.fit_transform(X)
plt.scatter(X_lle[:,0],X_lle[:,1],c =y)
<matplotlib.collections.PathCollection at 0x213019be588>
output_2_1.png
网友评论