《从机器学习到深度学习》第五章 数据降维代码实现

第五章讲述了多种功能相近的降维模型,包括:PCA.LDA,Isomap,t-SNE,MDS,LLE及其两种衍生。

本案例以将三维空间中的一个S状流形降到两维为目标。

执行代码为书本附带,由此下载 http://www.broadview.com.cn/book/5337 


from time import time

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import NullFormatter

from sklearn import manifold, datasets 
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA

Axes3D #启用matplot 3D

n_points = 1000      #样本数量
X, color = datasets.samples_generator.make_s_curve(n_points, random_state=0)
n_neighbors = 10    #流行学习近邻数量
n_components = 2    #目标维度数

fig = plt.figure(figsize=(15, 8))
plt.suptitle("Dimension Reduction with %i points, %i neighbors"
             % (1000, n_neighbors), fontsize=14)


ax = fig.add_subplot(251, projection='3d')   #在三维空间绘制原始数据
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=plt.cm.Spectral)
ax.view_init(4, -72)

methods = ['standard', 'ltsa', 'hessian', 'modified'] #定义三类LLE模型
labels = ['LLE', 'LTSA', 'Hessian LLE', 'Modified LLE']

for i, method in enumerate(methods):           #训练、显示LLE模型及其衍生模型
    #print('i,method',i,method)
    if i>2:
        continue
    t0 = time()
    Y = manifold.LocallyLinearEmbedding(n_neighbors, n_components,
                                        eigen_solver='auto',
                                        method=method).fit_transform(X)
    t1 = time()
    print("%s: %.2g sec" % (methods[i], t1 - t0)) #用t1-t0计算训练时间
    #显示降维结果
    ax = fig.add_subplot(252 + i)
    plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral)
    plt.title("%s (%.2g sec)" % (labels[i], t1 - t0))
    ax.xaxis.set_major_formatter(NullFormatter())
    ax.yaxis.set_major_formatter(NullFormatter())
    plt.grid(True)#格点
    plt.axis('tight')# 坐标轴适应数据量 axis 设置坐标轴

#初始化六种降维模型   
estimators = [(manifold.Isomap(n_neighbors, n_components), "Isomap"), #Isomap近邻数越大,效果越接近于普通PCA效果
              (manifold.MDS(n_components, max_iter=100, n_init=1), "MDS"),
              (manifold.SpectralEmbedding(n_components=n_components,
                                          n_neighbors=n_neighbors), "Laplace Eigenmaps"),#随近邻数变化最小
              (manifold.TSNE(n_components=n_components, init='pca', random_state=0), "t-SNE"),#训练时间长
              (PCA(n_components), "PCA"),
              (LDA(n_components=n_components), "LDA"),
              ]
#训练、显示六种降维模型
for idx, (estimator_obj, estimator_name) in enumerate(estimators):
    #print('idx,estimator_obj, estimator_name',idx,estimator_obj, estimator_name)
    # estimator_obj, estimator_name = estimator[0], estimator[1]
    t0 = time()
    if estimator_name=="LDA":
        Y = estimator_obj.fit_transform(X, (color).astype(int))
    else:
        Y = estimator_obj.fit_transform(X)
    t1 = time()
    print("%s: %.2g sec" % (estimator_name, t1 - t0))
    ax = fig.add_subplot(2, 5, 5+idx)
    plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=plt.cm.Spectral)
    plt.title("%s (%.2g sec)" % (estimator_name, t1 - t0))
    ax.xaxis.set_major_formatter(NullFormatter())
    ax.yaxis.set_major_formatter(NullFormatter())
    plt.grid(True)
    plt.axis('tight')
    
plt.show()

n_neighbours=10结果如下:

#standard: 0.15 sec
#ltsa: 0.25 sec
#hessian: 0.35 sec
#Isomap: 0.55 sec
#MDS: 5.5 sec
#Laplace Eigenmaps: 0.13 sec
#t-SNE: 45 sec
#PCA: 0.002 sec
#LDA: 0.005 sec
#PCA和LDA找到了一个最能体现样本之间差异的切面进行三维到二维的映射;MDS和流形学习模型都达到了展开彩带的效果;t-SNE训练时间长;只有LDA模型使用了标签数据。

当取n_neighbors=20、100时,图像如下:

可知:1.Isomap近邻数越大,效果越接近于普通PCA效果;

           2.Laplace Eigenmaps随近邻数变化最小,LLE则变化最大;

           3.t-SNE、PCA、LDA没有变化因为训练与近邻无关。

         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值