PCA参数如下:
class sklearn.decomposition.PCA(n_components=None, copy=True, whiten=False, svd_solver='auto', tol=0.0, iterated_power='auto', random_state=None)[source]
参数说明:
n_components
意义:PCA算法中所要保留的主成分个数n,也即保留下来的特征个数n
类型:int 或者 string,缺省时默认为None,所有成分被保留。
赋值为int,比如n_components=1,将把原始数据降到一个维度。
赋值为string,比如n_components=’mle’,将自动选取特征个数n,使得满足所要求的方差百分比。
copy
类型:bool,True或者False,缺省时默认为True。
意义:表示是否在运行算法时,将原始训练数据复制一份。
若为True,则运行PCA算法后,原始训练数据的值不会有任何改变,因为是在原始数据的副本上进行运算;
若为False,则运行PCA算法后,原始训练数据的值会改,因为是在原始数据上进行降维计算。
whiten
类型:bool,缺省时默认为False
意义:白化,使得每个特征具有相同的方差。关于“白化”,可参考:Ufldl教程
属性:
components_
类型:array,[n_components,n_features]
意义:特征空间中的主轴,表示数据中最大方差的方向。按explain_variance_排序。
explained_variance_
类型:array,[n_components]
意义:对应于每个主成分方向的方差值
explained_variance_ratio_
类型:array,[n_components]
意义:每个主成分方向的方差百分比。如果未设置n_components,则存储所有主成分,并且解释的方差的总和等于1.0
一个实例:
import numpy as np
from sklearn.decomposition import PCA
X = np.array([[-3, -1,0], [-2, -1,4], [-3, -2,6], [1, 1,-5], [2, 1,4], [3, 2,4]])
pca = PCA(n_components=3)
pca.fit(X)
print(pca.explained_variance_ratio_)
print(pca.components_)
print(pca.mean_)
print(X.mean(axis=0))
[ 0.65472741 0.34327618 0.00199641]
[[ 0.20516068 0.17751253 -0.96249592]
[ 0.84937517 0.45631899 0.26520708]
[-0.48628275 0.8719302 0.05715602]]
[-0.33333333 0. 2.16666667]
[-0.33333333 0. 2.16666667]
更多参考:scikit-learn PCA官方文档