forked from PercyJaiswal/Colley_Rankings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Colley_with_weights.py
55 lines (44 loc) · 1.68 KB
/
Colley_with_weights.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
# Importing packages
import numpy as np
import pandas as pd
# Reading 'teams' and 'scores' data
teams = pd.read_csv('teams.txt', header = None)
num_of_teams = len(teams.index)
data = pd.read_csv('scores_with_weights.txt', header = None)
# Initializing Colley Matrix 'c'and vector 'b'
c = np.zeros([num_of_teams, num_of_teams])
b = np.zeros(num_of_teams)
# Iterating through rows and populating Colley matrix values
for index, row in data.iterrows():
t1 = row[2]
t2 = row[5]
if row[0] <= 6: #For first round matches
weight = 0.9
elif row[0] > 6 and row[0] <=12: #For second round matches
weight = 1.0
else:
weight = 1.1 #For third round matches
c[(t1-1)][(t1-1)] = c[(t1-1)][(t1-1)] + 1 * weight # Updating diagonal element
c[(t2-1)][(t2-1)] = c[(t2-1)][(t2-1)] + 1 * weight # Updating diagonal element
c[(t1-1)][(t2-1)] = c[(t1-1)][(t2-1)] - 1 * weight # Updating off - diagonal element
c[(t2-1)][(t1-1)] = c[(t2-1)][(t1-1)] - 1 * weight # Updating off - diagonal element
# Updating vecotr b based on result of each game
if row[4] > row[7]:
b[(t1-1)] += 1 * weight
b[(t2-1)] -= 1 * weight
elif row[4] < row[7]:
b[(t1-1)] -= 1 * weight
b[(t2-1)] += 1 * weight
# Adding 2 to diagonal elements (total number of games) of Colley matrix
diag = c.diagonal() + 2
np.fill_diagonal(c, diag)
# Dividing by 2 and adding one to vector b
for i, value in enumerate(b):
b[i] = b[i] / 2
b[i] += 1
# Solving N variable linear equation
r = np.linalg.solve(c, b)
# Displaying ranking for top 4 teams
top_teams = r.argsort()[-4:][::-1]
for i in top_teams:
print (str(r[i]) + " " + str(teams.iloc[i][1]))