熵值法的python实现方法

1. 数据

import pandas as pd
import numpy as np
import math
from numpy import array

data = pd.read_excel(r'C:\经过熵值法的问卷数据.xlsx')
Poverty_Alleviation_Satisfaction = data[data.columns[0:14]]
Poverty_Alleviation_Satisfaction

2.算法主体部分

# Poverty_Alleviation_Satisfaction = data[['Q15','Q16','Q17']]
Poverty_Alleviation_Satisfaction = data[data.columns[0:14]]
Poverty_Alleviation_Satisfaction

# 1.归一化,算K值
row_no = Poverty_Alleviation_Satisfaction.shape[0] # 样本
col_no = Poverty_Alleviation_Satisfaction.shape[1] # 指标

Normalization_data = Poverty_Alleviation_Satisfaction.apply(lambda x: x / sum(x))
K = 1/np.log(row_no) #593个样本
# 2.计算Ej,每个属性的贡献度
P_matrix = Normalization_data.apply(lambda x:x*np.log(x))
Ej = -K*P_matrix.apply(lambda x: x.sum())
# 3.求Dj
Dj = 1-Ej
# 4.求Wj
Wj = Dj/sum(Dj)
print(Wj)
# 5.计算每个样本的总体评分
Poverty_Alleviation_Satisfaction_New = Poverty_Alleviation_Satisfaction * Wj
Poverty_Alleviation_Satisfaction_New.to_excel(r'C:熵值法计算结果.xlsx')
Poverty_Alleviation_Satisfaction_New.apply(lambda x:x.sum(),1)

你可能感兴趣的:(数学基础,python基础,python,机器学习,开发语言)