吴裕雄 python 机器学习——局部线性嵌入LLE降维模型

# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets,manifold

def load_data():
    '''
    加载用于降维的数据
    '''
    # 使用 scikit-learn 自带的 iris 数据集
    iris=datasets.load_iris()
    return  iris.data,iris.target

#局部线性嵌入LLE降维模型
def test_LocallyLinearEmbedding(*data):
    X,y=data
    # 依次考察降维目标为 4维、3维、2维、1维
    for n in [4,3,2,1]:
        lle=manifold.LocallyLinearEmbedding(n_components=n)
        lle.fit(X)
        print('reconstruction_error(n_components=%d) : %s'%(n, lle.reconstruction_error_))
        
# 产生用于降维的数据集
X,y=load_data() 
# 调用 test_LocallyLinearEmbedding
test_LocallyLinearEmbedding(X,y)   

def plot_LocallyLinearEmbedding_k(*data):
    '''
    测试 LocallyLinearEmbedding 中 n_neighbors 参数的影响,其中降维至 2维
    '''
    X,y=data
    # n_neighbors参数的候选值的集合
    Ks=[1,5,25,y.size-1]

    fig=plt.figure()
    for i, k in enumerate(Ks):
        lle=manifold.LocallyLinearEmbedding(n_components=2,n_neighbors=k)
        #原始数据集转换到二维
        X_r=lle.fit_transform(X)
        ## 两行两列,每个单元显示不同 n_neighbors 参数的 LocallyLinearEmbedding 的效果图
        ax=fig.add_subplot(2,2,i+1)
        # 颜色集合,不同标记的样本染不同的颜色
        colors=((1,0,0),(0,1,0),(0,0,1),(0.5,0.5,0),(0,0.5,0.5),(0.5,0,0.5),(0.4,0.6,0),(0.6,0.4,0),(0,0.6,0.4),(0.5,0.3,0.2))
        for label ,color in zip( np.unique(y),colors):
            position=y==label
            ax.scatter(X_r[position,0],X_r[position,1],label="target= %d"%label,color=color)
        ax.set_xlabel("X[0]")
        ax.set_ylabel("X[1]")
        ax.legend(loc="best")
        ax.set_title("k=%d"%k)
    plt.suptitle("LocallyLinearEmbedding")
    plt.show()
    
# 调用 plot_LocallyLinearEmbedding_k
plot_LocallyLinearEmbedding_k(X,y)   

def plot_LocallyLinearEmbedding_k_d1(*data):
    '''
    测试 LocallyLinearEmbedding 中 n_neighbors 参数的影响,其中降维至 1维
    '''
    X,y=data
    Ks=[1,5,25,y.size-1]# n_neighbors参数的候选值的集合

    fig=plt.figure()
    for i, k in enumerate(Ks):
        lle=manifold.LocallyLinearEmbedding(n_components=1,n_neighbors=k)
        X_r=lle.fit_transform(X)#原始数据集转换到 1 维

        ax=fig.add_subplot(2,2,i+1)## 两行两列,每个单元显示不同 n_neighbors 参数的 LocallyLinearEmbedding 的效果图
        colors=((1,0,0),(0,1,0),(0,0,1),(0.5,0.5,0),(0,0.5,0.5),(0.5,0,0.5),
            (0.4,0.6,0),(0.6,0.4,0),(0,0.6,0.4),(0.5,0.3,0.2),)# 颜色集合,不同标记的样本染不同的颜色
        for label ,color in zip( np.unique(y),colors):
            position=y==label
            ax.scatter(X_r[position],np.zeros_like(X_r[position]),
            label="target= %d"%label,color=color)

        ax.set_xlabel("X")
        ax.set_ylabel("Y")
        ax.legend(loc="best")
        ax.set_title("k=%d"%k)
    plt.suptitle("LocallyLinearEmbedding")
    plt.show()

# 调用 plot_LocallyLinearEmbedding_k_d1
plot_LocallyLinearEmbedding_k_d1(X,y)   

 

转载于:https://www.cnblogs.com/tszr/p/10796237.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值