-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelo_features.py
33 lines (31 loc) · 1.19 KB
/
elo_features.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
def compute_elo_rankings(data):
"""
Given the list on matches in chronological order, for each match, computes
the elo ranking of the 2 players at the beginning of the match
"""
print("Elo rankings computing...")
players=list(pd.Series(list(data.Winner)+list(data.Loser)).value_counts().index)
elo=pd.Series(np.ones(len(players))*1500,index=players)
ranking_elo=[(1500,1500)]
for i in range(1,len(data)):
w=data.iloc[i-1,:].Winner
l=data.iloc[i-1,:].Loser
elow=elo[w]
elol=elo[l]
pwin=1 / (1 + 10 ** ((elol - elow) / 400))
K_win=32
K_los=32
new_elow=elow+K_win*(1-pwin)
new_elol=elol-K_los*(1-pwin)
elo[w]=new_elow
elo[l]=new_elol
ranking_elo.append((elo[data.iloc[i,:].Winner],elo[data.iloc[i,:].Loser]))
if i%5000==0:
print(str(i)+" matches computed...")
ranking_elo=pd.DataFrame(ranking_elo,columns=["elo_winner","elo_loser"])
ranking_elo["proba_elo"]=1 / (1 + 10 ** ((ranking_elo["elo_loser"] - ranking_elo["elo_winner"]) / 400))
return ranking_elo