用较少的变量去解释原始数据中的大部分变量,即将许多相关性很高的变量转化成彼此相互独立或不相关的变量。
#-*- coding: utf-8 -*-
#主成分分析 降维
import pandas as pd
#参数初始化
inputfile = '../data/principal_component.xls'
outputfile = '../tmp/dimention_reducted.xls' #降维后的数据
data = pd.read_excel(inputfile, header = None) #读入数据
from sklearn.decomposition import PCA
pca = PCA() #保留所有成分
pca.fit(data)
pca.components_ #返回模型的各个特征向量
pca.explained_variance_ratio_ #返回各个成分各自的方差百分比(也称贡献率)
>>>pca.explained_variance_ratio_
array([7.74011263e-01, 1.56949443e-01, 4.27594216e-02, 2.40659228e-02,
1.50278048e-03, 4.10990447e-04, 2.07718405e-04, 9.24594471e-05])
当选取前3个主成分是,累计贡献率已经达到97.73%,所以选取前3个主成分进行计算。
pca = PCA(3) #选取累计贡献率大于80%的主成分(3个主成分)
pca.fit(data)
low_d = pca.transform(data) #降低维度
pd.DataFrame(low_d).to_excel(outputfile) #保存结果
降维结果
>>>low_d
array([[ 8.19133694, 16.90402785, 3.90991029],
[ 0.28527403, -6.48074989, -4.62870368],
[-23.70739074, -2.85245701, -0.4965231 ],
[-14.43202637, 2.29917325, -1.50272151],
[ 5.4304568 , 10.00704077, 9.52086923],
[ 24.15955898, -9.36428589, 0.72657857],
[ -3.66134607, -7.60198615, -2.36439873],
[ 13.96761214, 13.89123979, -6.44917778],
[ 40.88093588, -13.25685287, 4.16539368],
[ -1.74887665, -4.23112299, -0.58980995],
[-21.94321959, -2.36645883, 1.33203832],
[-36.70868069, -6.00536554, 3.97183515],
[ 3.28750663, 4.86380886, 1.00424688],
[ 5.99885871, 4.19398863, -8.59953736]])