吴裕雄 python 机器学习——等度量映射Isomap降维模型


# -*- 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

#等度量映射Isomap降维模型
def test_Isomap(*data):
    X,y=data
    # 依次考察降维目标为 4维、3维、2维、1维
    for n in [4,3,2,1]: 
        isomap=manifold.Isomap(n_components=n)
        isomap.fit(X)
        print('reconstruction_error(n_components=%d) : %s'%(n, isomap.reconstruction_error()))
        
# 产生用于降维的数据集
X,y=load_data() 
# 调用 test_Isomap
test_Isomap(X,y)   

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

    fig=plt.figure()
    for i, k in enumerate(Ks):
        isomap=manifold.Isomap(n_components=2,n_neighbors=k)
        #原始数据集转换到二维
        X_r=isomap.fit_transform(X)
        ## 两行两列,每个单元显示不同 n_neighbors 参数的 Isomap 的效果图
        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("Isomap")
    plt.show()
    
# 调用 plot_Isomap_k
plot_Isomap_k(X,y)   

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

    fig=plt.figure()
    for i, k in enumerate(Ks):
        isomap=manifold.Isomap(n_components=1,n_neighbors=k)
        #原始数据集转换到 1 维
        X_r=isomap.fit_transform(X)
        ## 两行两列,每个单元显示不同 n_neighbors 参数的 Isomap 的效果图
        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],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("Isomap")
    plt.show()
    
# 调用 plot_Isomap_k_d1
plot_Isomap_k_d1(X,y)   

优质内容筛选与推荐>>
1、EF7学习资料整理
2、poj 1637 Sightseeing tour【最大流+欧拉路】
3、java日期格式大全 format SimpleDateFormat(转)
4、使用R语言链接Oracle-ROracle使用举例(转)
5、2019-07-31 C#基础知识学习


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn