# _*_ coding : utf-8 _*_
# @Time : 2022/6/4 8:40
# @Author : Lang Jian
# @File : Classification.py
# @Project : 南威杯数模校赛
import pandas as pd
import numpy as np
import math
from numpy import array
df = pd.read_csv("性平.csv")
df.dropna()
def cal_weight(x):
x = x.apply(lambda x: ((x - np.min(x)) / (np.max(x) - np.min(x))))
rows = x.index.size
cols = x.columns.size
k = 1.0 / math.log(rows)
lnf = [[None] * cols for i in range(rows)]
x = array(x)
lnf = [[None] * cols for i in range(rows)]
lnf = array(lnf)
for i in range(0, rows):
for j in range(0, cols):
if x[i][j] == 0:
lnfij = 0.0
else:
p = x[i][j] / x.sum(axis=0)[j]
lnfij = math.log(p) * p * (-k)
lnf[i][j] = lnfij
lnf = pd.DataFrame(lnf)
E = lnf
d = 1 - E.sum(axis=0)
w = [[None] * 1 for i in range(cols)]
for j in range(0, cols):
wj = d[j] / sum(d)
w[j] = wj
w = pd.DataFrame(w)
return w
if __name__ == '__main__':
w = cal_weight(df)
w.index = df.columns
w.columns = ['weight']
print(w)
11-05
03-17
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交