根据上述代码用python实现熵值法
时间: 2023-09-25 14:09:21 浏览: 47
熵值法可以用以下代码实现:
```python
import numpy as np
def entropy_weight(matrix):
"""
熵值法计算权重
:param matrix: 数据矩阵,每一行代表一个样本,每一列代表一个指标
:return: 权重列表
"""
# 1.标准化矩阵
m, n = matrix.shape
matrix = matrix.astype(float)
for i in range(n):
matrix[:, i] = (matrix[:, i] - np.min(matrix[:, i])) / (np.max(matrix[:, i]) - np.min(matrix[:, i]))
# 2.计算熵值
k = 1.0 / np.log(m)
lnf = [[None] * n for i in range(m)]
for i in range(m):
for j in range(n):
if matrix[i][j] == 0:
lnfij = 0.0
else:
lnfij = np.log(matrix[i][j])
lnf[i][j] = lnfij
f = np.array(lnf)
lnf_sum = []
for j in range(n):
lnf_sum.append(np.sum(f[:, j]))
lnf_sum = np.array(lnf_sum)
# 计算信息熵
e = - k * lnf_sum
# 3.计算权重
w = (1 - e) / np.sum(1 - e)
return w.tolist()
```
其中,输入参数 `matrix` 是一个 $m \times n$ 的矩阵,每一行代表一个样本,每一列代表一个指标。输出是一个长度为 $n$ 的权重列表,表示每个指标的重要程度。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)