-
Notifications
You must be signed in to change notification settings - Fork 0
/
computeFeatures.py
94 lines (88 loc) · 4.42 KB
/
computeFeatures.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
from __future__ import division
import csv
# Load id numbers for each team
team_ids = dict()
with open('kaggledata/Teams.csv') as f:
reader = csv.DictReader(f)
for row in reader:
team_ids[row['Team_Name']] = row['Team_Id']
# Get list of seasons
seasons = ['2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016']
# Compute statistics for each team in each season to use as model features
with open('kaggledata/TeamFeatures.csv', 'w') as fout:
fieldnames = ['Season','Team','Id','WinPct','PPG','OppPPG','3PAPG','3PPct','FTPG','FTPct','OffReboundPct','DefReboundPct','AstPG','ToPG','StPG','BlkPG','FoulPG']
writer = csv.DictWriter(fout, fieldnames=fieldnames)
writer.writeheader()
for year in seasons:
with open('kaggledata/RegularSeasonDetailedResults.csv') as fin:
reader = csv.DictReader(fin)
season = [row for row in reader if row['Season'] == year]
for team, tid in team_ids.iteritems():
features = {'Season':year,'Team':team,'Id':tid}
won_games = [row for row in season if row['Wteam'] == tid]
lost_games = [row for row in season if row['Lteam'] == tid]
games_played = len(won_games) + len(lost_games)
if games_played == 0:
continue
features['WinPct'] = len(won_games) / games_played
team_points = 0
opp_points = 0
three_pt_att = 0
three_pt_made = 0
free_throw_att = 0
free_throw_made = 0
team_off_rebounds = 0
opp_off_rebounds = 0
team_def_rebounds = 0
opp_def_rebounds = 0
assists = 0
turnovers = 0
steals = 0
blocks = 0
fouls = 0
for game in won_games:
team_points += int(game['Wscore'])
opp_points += int(game['Lscore'])
three_pt_att += int(game['Wfga3'])
three_pt_made += int(game['Wfgm3'])
free_throw_att += int(game['Wfta'])
free_throw_made += int(game['Wftm'])
team_off_rebounds += int(game['Wor'])
team_def_rebounds += int(game['Wdr'])
assists += int(game['Wast'])
turnovers += int(game['Wto'])
steals += int(game['Wstl'])
blocks += int(game['Wblk'])
fouls += int(game['Wpf'])
opp_off_rebounds += int(game['Lor'])
opp_def_rebounds += int(game['Ldr'])
for game in lost_games:
team_points += int(game['Lscore'])
opp_points += int(game['Wscore'])
three_pt_att += int(game['Lfga3'])
three_pt_made += int(game['Lfgm3'])
free_throw_att += int(game['Lfta'])
free_throw_made += int(game['Lftm'])
team_off_rebounds += int(game['Lor'])
team_def_rebounds += int(game['Ldr'])
assists += int(game['Last'])
turnovers += int(game['Lto'])
steals += int(game['Lstl'])
blocks += int(game['Lblk'])
fouls += int(game['Lpf'])
opp_off_rebounds += int(game['Wor'])
opp_def_rebounds += int(game['Wdr'])
features['PPG'] = team_points / games_played
features['OppPPG'] = opp_points / games_played
features['3PAPG'] = three_pt_att / games_played
features['3PPct'] = three_pt_made / three_pt_att
features['FTPG'] = free_throw_att / games_played
features['FTPct'] = free_throw_made / free_throw_att
features['OffReboundPct'] = team_off_rebounds / (team_off_rebounds + opp_def_rebounds)
features['DefReboundPct'] = team_def_rebounds / (team_def_rebounds + opp_off_rebounds)
features['AstPG'] = assists / games_played
features['ToPG'] = turnovers / games_played
features['StPG'] = steals / games_played
features['BlkPG'] = blocks / games_played
features['FoulPG'] = fouls / games_played
writer.writerow(features)