在业务中经常遇到需要对多个对象进行打分的任务,如何针对不同属性赋予权重是关键一步。为了既满足数据客观性又满足主管需求,一般通过熵值法结合AHP层次分析法(即专家打分法)赋予权重并进行评分。
主要方法是先根据熵值法计算一轮,需要给定正负向指标以及对象列。
在此基础上,允许对各层权重、独立权重进行调整,以符合实际需求。主要由函数cal_Weight实现。
代码如下:
import pandas as pd
class EmtropyMethod:
def __init__(self, index, positive, negative, city):
if len(index) != len(city):
raise Exception('数据指标行数与行名称数不符')
if sorted(index.columns) != sorted(positive + negative):
raise Exception('正项指标加负向指标不等于数据指标的条目数')
self.index = index.copy().astype('float64')
self.positive = positive
self.negative = negative
self.city = city.copy()
def uniform(self):
uniform_mat = self.index.copy()
min_index = {column: min(uniform_mat[column]) for column in uniform_mat.columns}
max_index = {column: max(uniform_mat[column]) for column in uniform_mat.columns}
for i in range(len(uniform_mat)):
for column in uniform_mat.columns:
if column in self.positive:
uniform_mat[column][i] = (uniform_mat[column][i] - min_index[column]) / (
m