PCA计算原特征(指标)对主成分的贡献量/权重

1. 用PCA反推原始特征对主成分的贡献量或权重

使用python的sklearn包中的pca函数

# -*- coding: utf-8 -*-
import os
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from sklearn import preprocessing
np.set_printoptions(suppress=True)

def normalization(data):
    _range = np.max(data) - np.min(data)
    return (data - np.min(data)) / _range


# np.random.seed(3)
# data = np.random.randint(10,size=(100,4))/10
# 数据导入,生成随机数
csv_file = r'D:\research\tmp.xlsx'
csv_data = pd.read_excel(csv_file,sheet_name='Sheet1')
data = np.array(pd.DataFrame(csv_data))
data = data[:, :4]
# data = normalization(data)
print(data.shape)
# 主成分分析建模
pca = PCA(n_components='mle')  # 将自动选取主成分个数n,使得满足所要求的方差百分比
# pca = PCA(n_components=3)  # n_components提取因子数量
# pca = PCA(n_components=None)  # n_components提取因子数量,返回所有主成分
pca.fit(data)
print('特征根',pca.explained_variance_)  # 贡献方差,即特征根
# print(pca.explained_variance_ratio_)  # 方差贡献率
print('pca:',pca.components_, pca.components_.shape)  # 成分矩阵,sklearn已经除过pca.explained_variance_,不需要再次处理
# k1_spss = pca.components_ / np.sqrt(pca.explained_variance_.reshape(-1, 1))  # 成分得分系数矩阵
# pca.components_ 行是成分,列是特征,这里进行转置,方便后边计算
k1_spss = pca.components_.T 
# print(k1_spss)

有个问题,spss里面一般会默认使用两个主成分,但实际上pca可以提取≤特征个主成分,所以在计算的时候pca的主成分数量怎么确定呢?是使用所有主成分、自动选、还是指定数量?这篇博文 http://blog.sina.com.cn/s/blog_a032adb90101k47u.html 说的是,特征根>1,累计方差贡献率≥80%,但是没有出处

# 确定权重
# 求指标在不同主成分线性组合中的系数
weight = (np.dot(k1_spss, pca.explained_variance_ratio_)) / np.sum(pca.explained_variance_ratio_)
print('weight:',weight)
# 
weighted_weight = weight/np.sum(weight)
print('weighted_weight:', weighted_weight)

# 
# https://blog.csdn.net/weixin_39782545/article/details/112216185?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0.no_search_link&spm=1001.2101.3001.4242
# 参考以上两篇文章,确定权重这不是矩阵点乘么?下面博主的代码好像有问题
# # 原博主代码 https://blog.csdn.net/weixin_43166884/article/details/109363740
# # 确定权重
# # 求指标在不同主成分线性组合中的系数
# j = 0
# Weights = []
# for j in range(len(k1_spss)):
#     for i in range(len(pca.explained_variance_)):
#         Weights_coefficient = np.sum(100 * (pca.explained_variance_ratio_[i]) * (k1_spss[i][j])) / np.sum(
#             pca.explained_variance_ratio_)
#     j = j + 1
#     Weights.append(np.float(Weights_coefficient))
# print('Weights',Weights)
# # weighted_weight = np.round(Weights/np.sum(Weights), decimals=4)
# # print(weighted_weight)

# Weights=pd.DataFrame(Weights)
# Weights1 = preprocessing.MinMaxScaler().fit(Weights)
# Weights2 = Weights1.transform(Weights)
# print('Weights2',Weights2)

不对之处,敬请指正。

  • 14
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值