zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    【小错误】ORA-00265: instance recovery required, cannot set ARCHIVELOG mode
    【小错误】Device eth2 has different MAC address than expected, ignoring.
    Bloom filters 布隆过滤器
    ORA-600 [729] "UGA Space Leak" (文档 ID 31056.1)
    Procwatcher: Script to Monitor and Examine Oracle DB and Clusterware Processes (文档 ID 459694.1)
    TECH: Getting a Stack Trace from a CORE file on Unix (文档 ID 1812.1)
    Diagnostic Tools Catalog (文档 ID 559339.1)
    How to Analyze Problems Related to Internal Errors (ORA-600) and Core Dumps (ORA-7445) using My Oracle Support (文档 ID 260459.1)
    windows DOS命令
    收集UNDO管理信息的脚本
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/7458554.html
Copyright © 2011-2022 走看看