• Python机器学习--降维


    • 主成分分析(PCA)

    • 测试

    # -*- coding: utf-8 -*-
    """
    Created on Thu Aug 31 14:21:51 2017
    
    @author: Administrator
    """
    
    import matplotlib.pyplot as plt
    from sklearn.decomposition import PCA
    from sklearn.datasets import load_iris
     
    data = load_iris()
    y = data.target
    X = data.data
    pca = PCA(n_components=2)
    reduced_X = pca.fit_transform(X)
     
    red_x, red_y = [], []
    blue_x, blue_y = [], []
    green_x, green_y = [], []
     
    for i in range(len(reduced_X)):
        if y[i] == 0:
            red_x.append(reduced_X[i][0])
            red_y.append(reduced_X[i][1])
        elif y[i] == 1:
            blue_x.append(reduced_X[i][0])
            blue_y.append(reduced_X[i][1])
        else:
            green_x.append(reduced_X[i][0])
            green_y.append(reduced_X[i][1])
     
    plt.scatter(red_x, red_y, c='r', marker='x')
    plt.scatter(blue_x, blue_y, c='b', marker='D')
    plt.scatter(green_x, green_y, c='g', marker='.')
    plt.show()
    • 非负矩阵分解(NMF)

    • 测试

    # -*- coding: utf-8 -*-
    """
    Created on Thu Aug 31 14:24:26 2017
    
    @author: Administrator
    """
    
    
    from numpy.random import RandomState
    import matplotlib.pyplot as plt
    from sklearn.datasets import fetch_olivetti_faces
    from sklearn import decomposition
     
     
    n_row, n_col = 2, 3
    n_components = n_row * n_col
    image_shape = (64, 64)
     
     
    ###############################################################################
    # Load faces data
    dataset = fetch_olivetti_faces(shuffle=True, random_state=RandomState(0))
    faces = dataset.data
     
    ###############################################################################
    def plot_gallery(title, images, n_col=n_col, n_row=n_row):
        plt.figure(figsize=(2. * n_col, 2.26 * n_row)) 
        plt.suptitle(title, size=16)
     
        for i, comp in enumerate(images):
            plt.subplot(n_row, n_col, i + 1)
            vmax = max(comp.max(), -comp.min())
     
            plt.imshow(comp.reshape(image_shape), cmap=plt.cm.gray,
                       interpolation='nearest', vmin=-vmax, vmax=vmax)
            plt.xticks(())
            plt.yticks(())
        plt.subplots_adjust(0.01, 0.05, 0.99, 0.94, 0.04, 0.)
     
         
    plot_gallery("First centered Olivetti faces", faces[:n_components])
    ###############################################################################
     
    estimators = [
        ('Eigenfaces - PCA using randomized SVD',
             decomposition.PCA(n_components=6,whiten=True)),
     
        ('Non-negative components - NMF',
             decomposition.NMF(n_components=6, init='nndsvda', tol=5e-3))  # 设置k=6
    ]
     
    ###############################################################################
     
    for name, estimator in estimators:
        print("Extracting the top %d %s..." % (n_components, name))
        print(faces.shape)
        estimator.fit(faces)
        components_ = estimator.components_
        plot_gallery(name, components_[:n_components])
     
    plt.show()
    • 结果

    Extracting the top 6 Eigenfaces - PCA using randomized SVD...
    (400, 4096)
    Extracting the top 6 Non-negative components - NMF...
    (400, 4096)

  • 相关阅读:
    从零开始,开发一个 Web Office 套件(8):状态管理 & 拖动鼠标选中文字
    从零开始,开发一个 Web Office 套件(9):拖动鼠标选中文字 Edge Case
    从零开始, 开发一个 Web Office 套件 (1): 富文本编辑器
    从零开始,开发一个 Web Office 套件(6):光标 & Click 事件
    20211912 202120222 《网络攻防实践》第一周作业
    3天实践亲测简约靠谱Win10linux子系统Ubuntu下显示图形界面搭建固定IP远程服务器
    安卓通知转发
    RocketMQ(8) 消费幂等
    RocketMQ(5) 订阅关系的一致性规范
    RocketMQ(10) 消息类型
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/7458554.html
Copyright © 2020-2023  润新知