# python 实现信息熵、条件熵、信息增益、基尼系数

```            ```
import pandas as pd
import numpy as np
import math
## 计算信息熵
def getEntropy(s):
# 找到各个不同取值出现的次数
if not isinstance(s, pd.core.series.Series):
s = pd.Series(s)
prt_ary = pd.groupby(s , by = s).count().values / float(len(s))
return -(np.log2(prt_ary) * prt_ary).sum()
## 计算条件熵: 条件s1下s2的条件熵
def getCondEntropy(s1 , s2):
d = dict()
for i in list(range(len(s1))):
d[s1[i]] = d.get(s1[i] , []) + [s2[i]]
return sum([getEntropy(d[k]) * len(d[k]) / float(len(s1)) for k in d])

## 计算信息增益
def getEntropyGain(s1, s2):
return getEntropy(s2) - getCondEntropy(s1, s2)

## 计算增益率
return getEntropyGain(s1, s2) / getEntropy(s2)

## 衡量离散值的相关性
import  math
def getDiscreteCorr(s1, s2):
return getEntropyGain(s1,s2) / math.sqrt(getEntropy(s1) * getEntropy(s2))

# ######## 计算概率平方和
def getProbSS(s):
if not isinstance(s, pd.core.series.Series):
s = pd.Series(s)
prt_ary = pd.groupby(s, by = s).count().values / float(len(s))
return sum(prt_ary ** 2)
######## 计算基尼系数
def getGini(s1, s2):
d = dict()
for i in list(range(len(s1))):
d[s1[i]] = d.get(s1[i] , []) + [s2[i]]
return 1-sum([getProbSS(d[k]) * len(d[k]) / float(len(s1)) for k in d])
## 对离散型变量计算相关系数，并画出热力图, 返回相关性矩阵
def DiscreteCorr(C_data):
## 对离散型变量(C_data)进行相关系数的计算
C_data_column_names =  C_data.columns.tolist()
## 存储C_data相关系数的矩阵
import numpy as np
dp_corr_mat = np.zeros([len(C_data_column_names) , len(C_data_column_names)])
for i in range(len(C_data_column_names)):
for j in range(len(C_data_column_names)):
# 计算两个属性之间的相关系数
temp_corr = getDiscreteCorr(C_data.iloc[:,i] , C_data.iloc[:,j])
dp_corr_mat[i][j] = temp_corr
# 画出相关系数图
fig = plt.figure()
sns.heatmap(dp_corr_mat ,vmin= - 1, vmax= 1, cmap= sns.color_palette('RdBu' , n_colors= 128) , xticklabels= C_data_column_names , yticklabels= C_data_column_names)
return pd.DataFrame(dp_corr_mat)

if __name__ == "__main__":
s1 = pd.Series(['X1' , 'X1' , 'X2' , 'X2' , 'X2' , 'X2'])
s2 = pd.Series(['Y1' , 'Y1' , 'Y1' , 'Y2' , 'Y2' , 'Y2'])
print('CondEntropy:',getCondEntropy(s1, s2))
print('EntropyGain:' , getEntropyGain(s1, s2))
print('DiscreteCorr:' , getDiscreteCorr(s1, s1))
print('Gini' , getGini(s1, s2))

```
```

QQ号联系： 360901061

【本文对您有帮助就好】