-
Notifications
You must be signed in to change notification settings - Fork 0
/
topsis.py
64 lines (56 loc) · 1.85 KB
/
topsis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pandas as pd
import numpy as np
def topsis(file_name, weights, impacts):
w = list(int(i) for i in weights.split(','))
im = list(i for i in impacts.split(','))
if (len(w) != len(im)):
raise Exception('Number of elements in Weights and Impacts should be same')
try:
data = pd.read_csv(file_name)
except FileNotFoundError:
raise Exception('File not Found')
else:
df = data.iloc[:, 1:].values
m = len(data)
n = len(data.columns) - 1
if (n < 3):
raise Exception('Less than 3 Columns')
rss = []
for j in range(0, n):
s = 0
for i in range(0, m):
s += np.square(df[i, j])
rss.append(np.sqrt(s))
for j in range(0, n):
df[:, j] /= rss[j]
df[:, j] *= w[j]
best = []
worst = []
for j in range(0, n):
if (im[j] == '+'):
best.append(max(df[:, j]))
worst.append(min(df[:, j]))
elif (im[j] == '-'):
best.append(min(df[:, j]))
worst.append(max(df[:, j]))
else:
raise Exception('Signs in Impact can be either + or - only')
ebest = []
eworst = []
for i in range(0, m):
ssdb = 0
ssdw = 0
for j in range(0, n):
ssdb += np.square(df[i, j] - best[j])
ssdw += np.square(df[i, j] - worst[j])
rssdb = np.sqrt(ssdb)
rssdw = np.sqrt(ssdw)
ebest.append(rssdb)
eworst.append(rssdw)
p = []
for i in range(0, m):
measure = eworst[i] / (eworst[i] + ebest[i])
p.append(measure * 100)
data['Topsis Score'] = p
data['Rank'] = data['Topsis Score'].rank(ascending=False)
return data