Python实现熵值法

函数功能

熵值法计算各项目综合得分

函数调用

The_Entropy_method.get_weights(data, name_list=None)

输入参数

data:二维数组,指标数据
name_list:指标正负向,1-代表正向,0-代表负向,默认是None,全为正向。

输出

ws:所有指标权重,一维数组
score:所有项目综合得分,一维数组
index:所有项目的名次,一维数组

代码实现

import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import numpy as np


# 数据标准化
def get_stand(data, name_list):
    scaler = MinMaxScaler()
    if name_list == None:
        x_sta = scaler.fit_transform(data) + 1e-10
    else:
        index_1 = [i for i, w in enumerate(name_list) if w == 1]
        # print(index_1)
        index_0 = [i for i, w in enumerate(name_list) if w == 0]
        # print(index_0)
        x_1 = data[:, index_1]
        x_sta1 = scaler.fit_transform(x_1) + 1e-10
        g = data[:, index_0]
        x_sta2 = (g.max(axis=0) - g) / (g.max(axis=0) - g.min(axis=0)) + 1e-10
        x_sta = np.hstack((x_sta1, x_sta2))

    return x_sta


def get_weights(data, name_list=None):
    """
    输入数据data,标准化后采用熵值法计算权重,综合得分以及排序位置
    """
    data = np.array(data)
    x_sta = get_stand(data, name_list)

    # 计算指标值占比
    w = x_sta/sum(x_sta)

    # 计算熵值
    k = 1/np.log(x_sta.shape[0])
    e = -k*(w*np.log(w)).sum(axis=0)
    print(e)
    d = 1 - e
    ws = d/sum(d)
    print(ws)

    score = np.matmul(np.array(data), ws)
    print(score)

    index = sorted(score, reverse=True)
    print(index)

    # 输出名次结果
    sor = [index.index(score[l]) + 1 for l in range(len(index))]
    print(sor)

    results = {}
    results['weights'] = str(list(ws))
    results['score'] = str(list(score))
    results['pos'] = str(sor)
    print(results)
    return str(results)


if __name__ == '__main__':
    data = pd.read_excel('./data/1234.xlsx', header=None)
    df = np.array(data)
    # print(df)
    get_weights(df)

你可能感兴趣的:(python基础,python,熵值法)