昨天学习了Topsis法的基本概念,今天就来一起实践一下,用python实现topsis法
代码分块解释
1、引入numpy库
python
numpy as np
2、输入参评与指标数目
python
# 用户输入参评数目和指标数目,将输入的字符串转换为数值
print("请输入参评数目:")
n = int(input()) # 接收参评数目,输入的数转换成int
print("请输入指标数目:")
m = int(input()) # 接收指标数目
print("请输入类型矩阵:1.极大型,2.极小型,3.中间型,4.区间型")
kind = input().split(" ")# 将输入的字符串按空格分割,形成列表
3、接收输入的矩阵并转换成numpy数组
python
# 接收输入的矩阵并转换成numpy数组
print("请输入矩阵:")
A = np.zeros(shape=(n,m)) # 初始化一个n行m列的全零矩阵A
for i in range(n):
A[i] = input().split(" ") # 接收每行输入的数据
A[i] = list(map(float, A[i])) # 将接收到的字符串转换为浮点型列表
print("输入矩阵为,\n{}".format(A))
4.转化函数
极小型转化 为极大型指标
python
def minTomax(maxx, x):
x = list(x)
ans = [[(maxx-e)] for e in x] #列表推导式
return np.array(ans)
中间型指标转化为极大型指标
python
def midTomax(bestx,x):
x = list(x)
h = [abs(e-bestx) for e in x]
M = max(h)
if M == 0:
M = 1
ans = [[(1-e/M)] for e in h]
return np.array(ans)
区间型指标转化为极大型指标
python
def regTomax(lowx, highx, x):
x = list(x)
M = max(lowx-min(x), max(x)-highx)
if M == 0:
M = 1
ans = []
for i in range(len(x)):
if x[i]<lowx:
ans.append([1-(lowx-x[i])/M])
elif x[i]>highx:
ans.append([1-(x[i]-highx)/M])
else:
ans.append([1])
return np.array(ans)
计算统一指标后的矩阵
python
X = np.zeros(shape=(n, 1))
for i in range(m):
if kind[i] == "1":
v = np.array(A[:, i])
elif kind[i] =="2":
maxA = max(A[:, i])
v = minTomax(maxA, A[:, i])
elif kind[i] == "3":
print("类型三:请输入最优值:")
bestA = eval(input())
v = midTomax(bestA, A[:, i])
elif kind[i] == "4":
print("类型四:请输入区间【a,b】值a:")
lowA = eval(input())
print("类型四:请输入区间【a,b】值b:")
highA = eval(input())
v = regTomax(lowA, highA, A[:, i])
if i==0:
X = v.reshape(-1, 1)
else:
X = np.hstack([X, v.reshape(-1,1)])
print("统一指标后矩阵为:\n{}".format(X))
# 标准化
X = X.astype('float')
for j in range(m):
X[:, j] = X[:, j]/np.sqrt(sum(X[:, j]**2))# 少加了一个","导致报错
# 开根号
print("标准化矩阵为:\n{}".format(X))
计算距离
python
#最大值与最小值距离的计算
x_max = np.max(X, axis=0)
x_min = np.min(X, axis=0)
d_z = np.sqrt(np.sum(np.square((X - np.tile(x_max, (n, 1)))), axis=1))
d_f = np.sqrt(np.sum(np.square((X - np.tile(x_min, (n, 1)))), axis=1))
print('每个指标的最大值:',x_max)
print('每个指标的最小值:',x_min)
print('d+向量:',d_z)
print('d-向量:',d_f)
s = d_f/(d_z+d_f)
Score = 100*s/sum(s)
for i in range(len(Score)):
print(f"第{i+1}个标准化后百分制得分为:{Score[i]}")
完整代码
python
import numpy as np
# 用户输入参评数目和指标数目,将输入的字符串转换为数值
print("请输入参评数目:")
n = int(input()) # 接收参评数目,输入的数转换成int
print("请输入指标数目:")
m = int(input()) # 接收指标数目
print("请输入类型矩阵:1.极大型,2.极小型,3.中间型,4.区间型")
kind = input().split(" ")# 将输入的字符串按空格分割,形成列表
# 接收输入的矩阵并转换成numpy数组
print("请输入矩阵:")
A = np.zeros(shape=(n,m)) # 初始化一个n行m列的全零矩阵A
for i in range(n):
A[i] = input().split(" ") # 接收每行输入的数据
A[i] = list(map(float, A[i])) # 将接收到的字符串转换为浮点型列表
print("输入矩阵为,\n{}".format(A))
def minTomax(maxx, x):
x = list(x)
ans = [[(maxx-e)] for e in x] #列表推导式
return np.array(ans)
def midTomax(bestx,x):
x = list(x)
h = [abs(e-bestx) for e in x]
M = max(h)
if M == 0:
M = 1
ans = [[(1-e/M)] for e in h]
return np.array(ans)
def regTomax(lowx, highx, x):
x = list(x)
M = max(lowx-min(x), max(x)-highx)
if M == 0:
M = 1
ans = []
for i in range(len(x)):
if x[i]<lowx:
ans.append([1-(lowx-x[i])/M])
elif x[i]>highx:
ans.append([1-(x[i]-highx)/M])
else:
ans.append([1])
return np.array(ans)
X = np.zeros(shape=(n, 1))
for i in range(m):
if kind[i] == "1":
v = np.array(A[:, i])
elif kind[i] =="2":
maxA = max(A[:, i])
v = minTomax(maxA, A[:, i])
elif kind[i] == "3":
print("类型三:请输入最优值:")
bestA = eval(input())
v = midTomax(bestA, A[:, i])
elif kind[i] == "4":
print("类型四:请输入区间【a,b】值a:")
lowA = eval(input())
print("类型四:请输入区间【a,b】值b:")
highA = eval(input())
v = regTomax(lowA, highA, A[:, i])
if i==0:
X = v.reshape(-1, 1)
else:
X = np.hstack([X, v.reshape(-1,1)])
print("统一指标后矩阵为:\n{}".format(X))
# 标准化
X = X.astype('float')
for j in range(m):
X[:, j] = X[:, j]/np.sqrt(sum(X[:, j]**2))# 少加了一个","导致报错
# 开根号
print("标准化矩阵为:\n{}".format(X))
#最大值与最小值距离的计算
x_max = np.max(X, axis=0)
x_min = np.min(X, axis=0)
d_z = np.sqrt(np.sum(np.square((X - np.tile(x_max, (n, 1)))), axis=1))
d_f = np.sqrt(np.sum(np.square((X - np.tile(x_min, (n, 1)))), axis=1))
print('每个指标的最大值:',x_max)
print('每个指标的最小值:',x_min)
print('d+向量:',d_z)
print('d-向量:',d_f)
s = d_f/(d_z+d_f)
Score = 100*s/sum(s)
for i in range(len(Score)):
print(f"第{i+1}个标准化后百分制得分为:{Score[i]}")