-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjleague_rating.py
123 lines (108 loc) · 2.99 KB
/
jleague_rating.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import datetime
import csv
import operator
import sys
from collections import deque
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates
from matplotlib.font_manager import FontProperties
import pandas as pd
from brent_rating import *
def convert_date(d):
yy = int(d[0:4])
mm = int(d[4:6])
dd = int(d[6:8])
return dates.date2num(datetime.datetime(yy,mm,dd))
def run():
header = None
br = online_brent_rating()
last_date = None
pending = deque()
def _flush():
if len(pending) > 0:
for (home,away,S) in pending:
br.new_name(home)
br.new_name(away)
br.start()
for (home,away,S) in pending:
br.add(home,away,S)
br.commit(decay=0.99)
pending.clear()
data = pd.read_csv("japan_soccer.csv",dtype={
"date": str,
"league": str,
"home": str,
"away": str,
"neutral": int,
"full-home": int,
"full-away": int,
"half-home": int,
"half-away": int,
"corner-home": int,
"corner-away": int,
"redcard-home": int,
"redcard-away": int,
})
for _,row in data.iterrows():
today = row['date']
if today != last_date:
if last_date is not None:
_flush()
yield (last_date,br)
last_date = today
else:
home = row['home']
away = row['away']
home_score = row['full-home']
away_score = row['full-away']
if home_score > away_score:
S = 1
elif home_score < away_score:
S = -1
else:
S = 0
pending.append([home,away,S])
if last_date is not None:
_flush()
yield (last_date,br)
def main():
if len(sys.argv) > 1:
teams = sys.argv[1:]
try:
p = teams.index("--baseline")
except ValueError:
baseline = ""
else:
baseline = teams[p+1]
teams = teams[0:p] + teams[p+2:]
else:
teams = []
if len(teams) > 0:
ignore_first = 100
x = []
y = []
for (today,br) in run():
if ignore_first <= 0:
x.append(convert_date(today))
y.append([br[_] - br[baseline] for _ in teams])
else:
ignore_first -= 1
plt.figure()
plt.plot_date(x,y,'-')
# for CJK character
font_prop = FontProperties(family='Droid Sans Fallback')
plt.legend(teams,prop=font_prop)
plt.xlabel("Date")
plt.ylabel("rating")
plt.grid(True)
plt.show()
else:
for (today,br) in run():
pass
for (name,r) in sorted(br,key=operator.itemgetter(1)):
print("%s => %g" % (name,r))
if __name__ == "__main__":
main()