-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_grabber.py
104 lines (94 loc) · 2.52 KB
/
data_grabber.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
# © Anthony Krivonos 2019
# NBA ML
# data_grabber.py
# March 3rd, 2019
import csv
import requests
from nba_api.stats.static import teams
from nba_api.stats.endpoints import leaguegamelog
# Get list of games
games = leaguegamelog.LeagueGameLog(direction='ASC', league_id='00', player_or_team_abbreviation='T', season_all_time='2018-19', season_type_all_star='Regular Season', sorter='PTS')
# Get data and headers
dataDict = games.league_game_log.get_dict()
data, headers = dataDict['data'], dataDict['headers']
# List of NBA teams
nba_teams = teams.get_teams()
##
#
# Write Games List to CSV
#
##
# Store games in CSV
with open('data/gameslist.csv', mode='w') as games_file:
games_writer = csv.writer(games_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
# Write header
games_writer.writerow(headers)
# Write rows
for data_row in data:
games_writer.writerow(data_row)
##
#
# Create Training Data
#
##
# --------------------------------------
# Features:
# --------------------------------------
# x0: Points (PTS)
# x1: Field Goal Percentage: (FG_PCT)
# x2: Three-Point Percentage: (FG3_PCT)
# x3: Free Throw Percentage: (FT_PCT)
# x4: Rebounds (REB)
# x5: Assists (AST)
# x6: Steals (STL)
# x7: Blocks (BLK)
# x8: Turnovers (TOV)
# --------------------------------------
# Class:
# --------------------------------------
# y: 0 (lose), 1 (win)
# --------------------------------------
# List of training headers
training_headers = [
'MIN',
'FGM',
'FGA',
'FG_PCT',
'FG3M',
'FG3A',
'FG3_PCT',
'FTM',
'FTA',
'FT_PCT',
'OREB',
'DREB',
'REB',
'AST',
'STL',
'BLK',
'TOV',
'PF',
'PTS',
'PLUS_MINUS',
'WL' # Make sure WL is last
]
training_indices = {}
for header in training_headers:
training_indices[header] = headers.index(header)
# Store training data in CSV
with open('data/trainingdata.csv', mode='w') as training_file:
training_writer = csv.writer(training_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
# Write header
training_writer.writerow(training_headers)
# Write rows
for data_row in data:
train_row = []
for header in training_headers:
index = training_indices[header]
if (header == 'WL'):
# -1 for loss, 1 for win
raw = data_row[index]
train_row.append(1 if raw == 'W' else -1)
else:
train_row.append(data_row[index])
training_writer.writerow(train_row)