PCA主成分分析

来源:互联网 发布:下载强力卸载软件 编辑:程序博客网 时间:2024/05/16 06:33

PCA(主成分分析)是用于数据降维的一种方法,可以用来将高维数据映射到低维空间,去掉那些无关属性,便于对数据进行分析。

在python的sklearn库中提供了相应方法。

sklearn.decomposition.PCA(n_components=None,copy=True,whiten=False)

参数说明:

(1)n_components

PCA算法中要保留的主成分个数即保留下来的特征数。

类型:int或string,缺省为None,所有成分被保留。赋值为int,比如n_components=1将原始数据降到一维。

赋值为string,比如n_components='mle'将自动选取特征个数,使得满足所要求的方差百分比。

(2)copy

类型:bool,缺省为True

表示在运行算法时是否赋值原数据集。若为True则在副本上运行算法不改变原数据集。

(3)whiten

类型:bool缺省为False

白化,使得每个特征具有相同方差。

举个例子:

import numpy as npfrom sklearn.decomposition import PCAD=np.random.rand(10,4)pca = PCA()pca.fit(D)print(pca.components_)print(pca.explained_variance_ratio_)
输出:

array([[ 0.50337803, -0.58076386, -0.57386558,  0.28284659],
       [-0.16288191, -0.73178568,  0.65241137,  0.11098921],
       [ 0.26130446, -0.21547305, -0.01640765, -0.94075615],
       [-0.80734133, -0.2842084 , -0.49474083, -0.15052265]])

array([ 0.48584656,  0.30710106,  0.12088463,  0.08616775])

1 0
原创粉丝点击