forked from PercyJaiswal/Colley_Rankings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Colley.py
51 lines (40 loc) · 1.42 KB
/
Colley.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
# Importing packages
import numpy as np
import pandas as pd
# Reading 'teams' and 'results' data
teams = pd.read_csv('teams.txt', header = None)
num_of_teams = len(teams.index)
data = pd.read_csv('results.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]
c[(t1-1)][(t1-1)] = c[(t1-1)][(t1-1)] + 1 # Updating diagonal element
c[(t2-1)][(t2-1)] = c[(t2-1)][(t2-1)] + 1 # Updating diagonal element
c[(t1-1)][(t2-1)] = c[(t1-1)][(t2-1)] - 1 # Updating off - diagonal element
c[(t2-1)][(t1-1)] = c[(t2-1)][(t1-1)] - 1 # Updating off - diagonal element
# Updating vecotr b based on result of each game
if row[4] > row[7]:
b[(t1-1)] += 1
b[(t2-1)] -= 1
elif row[4] < row[7]:
b[(t1-1)] -= 1
b[(t2-1)] += 1
# 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)
print(c)
print(b)
# Displaying ranking for top 4 teams
top_teams = r.argsort()[-12:][::-1]
for i in top_teams:
print (str(r[i]) + " " + str(teams.iloc[i][1]))