zoukankan      html  css  js  c++  java
  • <第一周>降维

    PCA###

    矩阵的主成分就是其协方差矩阵对应的特征向量,按照对应的特征值大小进行排序,最大的特征值为第一主成分,以此类推

    主要过程####

    • 对所有样本进行中心化
    • 计算样本的协方差矩阵 XX.T
    • 对协方差矩阵做特征值分解
    • 取最大的几个特征向量

    使用方法####

    sklearn sklearn.decomposition.PCA

    参数:

    • n_components
    • svd_solver auto默认 full arpack randomized 特征值分解的方法

    对鸢尾花进行降维

    # -*- coding: utf-8 -*-
    """
    Created on Mon May 22 17:33:47 2017
    
    @author: sfzyk
    """
    
    import numpy as np
    import sklearn.decomposition as skld
    
    import matplotlib.pyplot as plt
    from sklearn.datasets import load_iris
    
    data=load_iris()
    #字典形式
    y=data.target
    
    x=data.data
    pca=skld.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])
        elif y[i]==2:
            green_x.append(reduced_x[i][0])
            green_y.append(reduced_x[i][1])
    
    #plt.plot(red_x,red_y,'or',blue_x,blue_y,'bo',green_x,green_y,'go')
    plt.scatter(red_x,red_y)
    plt.scatter(green_x,green_y)
    plt.scatter(blue_x,blue_y)
    plt.show()
    
    
    

    非负矩阵分解###

    NMF
    给定非负矩阵V
    NMF 可以找到一个W 与H 值使得WH近似等于矩阵V中的值

    W矩阵 基础图像矩阵,相当于抽取出来的特征
    H矩阵 稀疏矩阵

    最小函数
    传统上 欧氏距离
    ( argminfrac{1}{2}||X-WH||2=frac{1}{2}(X_{ij}-WH_{ij})2)
    KL散度的求解方法
    ( graminJ(W,H)= sum_{ij}(X_{ij}lnfrac{X_ij}{WH_ij}-X_{ij}+WH_{ij}) )

    具体求解是迭代算法

    sklearn.decomposition.NMF 算

    参数####

    • n_components 用于只等分解后矩阵的单个维度k
    • init W矩阵和H矩阵初始化方式,默认为nndsvdar
    • ...

    NMF的使用方法

    NMF 人脸数据特征提取

    设置k=6

                H k*400
    

    W 4096k V 4096400

    # -*- coding: utf-8 -*-
    """
    Created on Wed May 24 12:06:56 2017
    
    @author: sfzyk
    """
    
    import matplotlib.pyplot as plt
    import sklearn.decomposition  as skld
    import sklearn.datasets as skldata
    import numpy as np
    
    n_row,n_col=2,3
    n_compeonents=6
    image_shape=(64,64)
    dataset=skldata.fetch_olivetti_faces(shuffle=True,random_state=np.random.RandomState(0))
    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.subplots_adjust(0.01,0.05,0.99,0.93,0.04,0.)
            
            
    plot_gallery("RAW",dataset.images[0:6])
    
    estimators=[('PCA',skld.PCA(n_components=6,whiten=True)),('NMF',skld.NMF(n_components=6,init='nndsvda',tol=5e-3))]
    faces=dataset.data
    for name,estimator in estimators:
        estimator.fit(faces)
        components_=estimator.components_
        plot_gallery(name,components_[:])
    plt.show()
    
  • 相关阅读:
    20150216 IMX257实现GPIO-查询按键驱动程序
    20150216简单的Linux字符设备驱动程序
    Linux内核驱动编程
    解决安装完centos6.6之后/etc/sysconfig/目录下没有iptables 的问题
    centos6.6安装配置jboss7.1.1
    mysql5.6中 order by 多个字段排序问题
    centos6.6编译安装lnmp系列之PHP
    centos6.6编译安装lnmp系列之nginx
    centos6.6编译安装lnmp系列之mysql
    mysq 安装时候进行生成数据库系统时候执行语句 ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql 时候报错
  • 原文地址:https://www.cnblogs.com/sfzyk/p/6890532.html
Copyright © 2011-2022 走看看