Python implements comprehensive evaluation model TOPSIS

Original text: https://mp.weixin.qq.com/s/J9fZQ8T9TR1Ed7taPGYYjw

1 Introduction to TOPSIS

There are many methods of comprehensive evaluation, and there are two main categories based on different weighting methods: one is the subjective assignment method, such as the index method, the analytic hierarchy process , the fuzzy comprehensive evaluation method , etc.; the other is the objective assignment method, such as the factor Analytical method , TOPSIS method, artificial neural network method, etc.

This article mainly introduces the TOPSIS method. TOPSIS(Technique for Order Preference by Similarity to an Ideal Solution) method, that is, the ideal solution method , was first proposed by CLHwang and K. Yoon in 1981. It can be used in many fields such as benefit evaluation, decision-making, and management.

This method ranks each evaluation object according to its closeness to the ideal target , and evaluates the relative merits among existing research objects.

The basic principle is to sort by measuring the distance between each evaluation object and the optimal solution and the worst solution . If the evaluation object is closest to the optimal solution and farthest from the worst solution , it is optimal ; otherwise, it is not optimal.

The main evaluation steps (extremely large indicators) of the TOPSIS method are as follows:

According to the degree of proximity, that is, C i ( 0 < = C i < = 1 ) C_i (0<=C_i<=1)Ci(0<=Ci<=1 ) Sortby size,C i C_iCiThe larger the value , the closer the evaluation unit is to the ideal state and the better the evaluation result.

It can also be obtained according to the formula that when Si + S_i^+Si+When the value is larger, Z ij Z_{ij}ZijThe closer to the maximum value.

This method has no strict restrictions on data distribution and sample content, and data calculation is simple and easy.

In the above steps, when obtaining the indicator matrix, the indicators need to be forwarded according to the target , and then normalized. Next, we will explain the indicator forwarding in detail.

2. Index forwarding method

1) Extremely large indicators

The higher (larger) the better, and such indicators are called extremely large indicators (benefit indicators).

x = x − x m i n x m a x − x m i n x=\frac{x-x_{min}}{x_{max}-x_{min}} x=xmaxxminxxmin

Such as academic performance:

2) Very small indicators

The fewer (smaller) the better, such indicators are called extremely small indicators (cost indicators).

For this type of indicator, the normal step is to convert all indicators into extremely large indicators, that is, positive indicators.

The formula for converting very small indicators into very large indicators:

x m a x − x x_{max}-x xmaxx

If the data of all very small indicators are integers, they can also be converted using the following formula:

1 x \frac{1}{x} x1

The chart below shows the results of converting the very small metric "number of arguments with people" into a very large metric:

3) Intermediate indicators

Characteristics of intermediate indicators: The value of the indicator should neither be too large nor too small. A specific value is the best.

The method is also to convert it into extremely large indicators.

{ xi } \{x_i\}{ xi} is a set of intermediate indicator sequences, and the best value isxbest x_{best}xbest, then the forwarding formula:

M = max ⁡ { x i − x b e s t } , x i ~ = 1 − ∣ x i − x b e s t ∣ M M=\max\{x_i-x_{best}\},\tilde{x_i}=1-\frac{|x_i-x_{best}|}{M} M=max{ xixbest},xi~=1Mxixbest

For example: The PH value indicator of water quality assessment is positive. The water quality is best when the PH value is 7:

4) Interval indicators

Interval-type indicators: The indicator falls best within a certain range.

xi {x_i}xiis a set of interval index sequences, and the best interval is [a, b], then the forwarding formula is as follows:

For example: For example, human body temperature is best within the range of 36 degrees Celsius to 37 degrees Celsius:

3 python implementation

TOPSIS is generally used in conjunction with the entropy weight method .

import pandas as pd
import numpy as np

#逆向指标标准化
def normalization1(data):
    _range = np.max(data) - np.min(data)
    return (data - np.min(data)) / _range

#正向指标标准化
def normalization2(data):
    _range = np.max(data) - np.min(data)
    return (np.max(data) - data) / _range

#熵权法计算权重
def entropyWeight(data):
    P = np.array(data)
    # 计算熵值
    E = np.nansum(-P * np.log(P) / np.log(len(data)), axis=0)
    # 计算权系数
    return (1 - E) / (1 - E).sum()

def topsis(data, weight=None):  
    
    # 权重
    weight = entropyWeight(data) if weight is None else np.array(weight)
    
    # 最优最劣方案
    Z = pd.DataFrame([(data*weight.T).min(), (data*weight.T).max()], index=['负理想解', '正理想解'])
    #Z = pd.DataFrame([data.min(), data.max()], index=['负理想解', '正理想解'])
    
    # 距离
    Result = data.copy()
    #Result['正理想解'] = np.sqrt(((data - Z.loc['正理想解']) ** 2 * weight).sum(axis=1))
    #Result['负理想解'] = np.sqrt(((data - Z.loc['负理想解']) ** 2 * weight).sum(axis=1))
    Result['正理想解'] = np.sqrt(((weight*data - Z.loc['正理想解']) ** 2 ).sum(axis=1))
    Result['负理想解'] = np.sqrt(((weight*data - Z.loc['负理想解']) ** 2 ).sum(axis=1))

    # 综合得分指数
    Result['综合得分指数'] = Result['负理想解'] / (Result['负理想解'] + Result['正理想解'])
    Result['排序'] = Result.rank(ascending=False)['综合得分指数']

    return Result, Z, weight

if __name__=='__main__':
    data = pd.read_csv('testdata.csv',sep = ',',encoding='gbk',header=None)
    data1 = data.copy()
    data1[0] = normalization1(data1[0])
    data1[1] = normalization1(data1[1])
    data1[2] = normalization1(data1[2])
    data1[3] = normalization1(data1[3])
    [result,z1,weight] = topsis(data1)

The final scoring results (part), positive and negative ideal solutions and weights are as follows:

Reference:
https://blog.csdn.net/qq_36384657/article/details/98188769
https://zhuanlan.zhihu.com/p/345513712
https://blog.csdn.net/qq_36384657/article/details/98188769

Guess you like

Origin blog.csdn.net/mengjizhiyou/article/details/128025774