读取一个excel表格,第一列为1,第二列为1和2时,每列生成一个数组,将生成的数组利用熵权法合并特征,并输出合并结果
时间: 2023-12-14 16:05:57 浏览: 70
首先,我们可以使用 pandas 库来读取 Excel 表格,具体方法如下:
```python
import pandas as pd
# 读取 Excel 表格
df = pd.read_excel('example.xlsx', sheet_name='Sheet1')
```
假设 example.xlsx 表格中的第一列为特征 A,第二列为特征 B,我们需要将这两列分别生成数组,可以使用下面的代码:
```python
# 取出特征 A 和特征 B 列
feature_A = df['A']
feature_B = df['B']
# 生成 A 和 B 的数组
array_A = feature_A.values
array_B = feature_B.values
```
接下来,我们可以使用熵权法将特征 A 和特征 B 合并,可以按照以下步骤进行:
1. 将特征 A 和特征 B 的数据进行归一化处理,这里我们可以使用 Min-Max 归一化方法。
```python
from sklearn.preprocessing import MinMaxScaler
# 对 A 和 B 进行归一化处理
scaler = MinMaxScaler()
norm_A = scaler.fit_transform(array_A.reshape(-1, 1))
norm_B = scaler.fit_transform(array_B.reshape(-1, 1))
```
2. 计算每个特征的熵值,以及每个特征对应每个样本的熵值。
```python
import numpy as np
# 计算 A 和 B 的熵值
ent_A = -norm_A * np.log(norm_A)
ent_B = -norm_B * np.log(norm_B)
# 计算每个样本的熵值
ent_sample = np.sum(np.concatenate((ent_A, ent_B), axis=1), axis=1)
```
3. 计算每个特征的权重值,以及每个样本的权重值。
```python
# 计算 A 和 B 的权重值
weight_A = (1 - np.sum(ent_A, axis=0) / np.sum(ent_A)) / (2 - np.sum(ent_A, axis=0) / np.sum(ent_A))
weight_B = (1 - np.sum(ent_B, axis=0) / np.sum(ent_B)) / (2 - np.sum(ent_B, axis=0) / np.sum(ent_B))
# 计算每个样本的权重值
weight_sample = np.concatenate((weight_A * ent_A, weight_B * ent_B), axis=1)
weight_sample = weight_sample / np.sum(weight_sample, axis=1, keepdims=True)
```
4. 计算每个样本的综合权重值,即最终的特征权重。
```python
# 计算每个样本的综合权重值
weight_final = np.sum(weight_sample * np.log(weight_sample), axis=1)
```
最后,我们可以将特征 A 和特征 B 的综合权重值合并起来,得到最终的特征权重值,并输出结果。
```python
# 合并特征 A 和特征 B 的综合权重值
feature_weight = np.concatenate((weight_final[:len(array_A)], weight_final[len(array_A):]), axis=1)
# 输出合并结果
print('特征 A 和特征 B 的综合权重值:')
print(feature_weight)
```
完整的代码示例如下:
```python
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import numpy as np
# 读取 Excel 表格
df = pd.read_excel('example.xlsx', sheet_name='Sheet1')
# 取出特征 A 和特征 B 列
feature_A = df['A']
feature_B = df['B']
# 生成 A 和 B 的数组
array_A = feature_A.values
array_B = feature_B.values
# 对 A 和 B 进行归一化处理
scaler = MinMaxScaler()
norm_A = scaler.fit_transform(array_A.reshape(-1, 1))
norm_B = scaler.fit_transform(array_B.reshape(-1, 1))
# 计算 A 和 B 的熵值
ent_A = -norm_A * np.log(norm_A)
ent_B = -norm_B * np.log(norm_B)
# 计算每个样本的熵值
ent_sample = np.sum(np.concatenate((ent_A, ent_B), axis=1), axis=1)
# 计算 A 和 B 的权重值
weight_A = (1 - np.sum(ent_A, axis=0) / np.sum(ent_A)) / (2 - np.sum(ent_A, axis=0) / np.sum(ent_A))
weight_B = (1 - np.sum(ent_B, axis=0) / np.sum(ent_B)) / (2 - np.sum(ent_B, axis=0) / np.sum(ent_B))
# 计算每个样本的权重值
weight_sample = np.concatenate((weight_A * ent_A, weight_B * ent_B), axis=1)
weight_sample = weight_sample / np.sum(weight_sample, axis=1, keepdims=True)
# 计算每个样本的综合权重值
weight_final = np.sum(weight_sample * np.log(weight_sample), axis=1)
# 合并特征 A 和特征 B 的综合权重值
feature_weight = np.concatenate((weight_final[:len(array_A)], weight_final[len(array_A):]), axis=1)
# 输出合并结果
print('特征 A 和特征 B 的综合权重值:')
print(feature_weight)
```
阅读全文