In [2]:
import nba_py
import datetime
import time
import nba_py.game
import nba_py.player
import nba_py.team
import pandas as pd
import numpy as np
import functools
import time
%matplotlib inline
today = datetime.date.today()
tomorrow = today + datetime.timedelta(days = 1)
someday = datetime.date(2017, 1, 1)
In [2]:
def get_games(date):
'''
return a pandas.Series of all the games available on this day.
'''
return nba_py.Scoreboard(month = date.month,
day = date.day,
year = date.year,
league_id = '00',
offset = 0).game_header()[['GAME_ID', 'HOME_TEAM_ID', 'VISITOR_TEAM_ID']]
games = get_games(someday)
games
Out[2]:
In [ ]:
def get_teams(games):
'''
return a pandas.DataFrame including every games' home team id and away team id.
'''
teams = [nba_py.game.BoxscoreSummary(g).game_summary().loc[0, ['HOME_TEAM_ID', 'VISITOR_TEAM_ID']]
for g in games]
return pd.DataFrame(teams, index = [g for g in games])
teams = get_teams(games)
teams
In [30]:
all_players = nba_py.player.PlayerList(season = '2017-18').info()
def get_players_new(games):
home_team_player = all_players[all_players['TEAM_ID'].isin(games['HOME_TEAM_ID'])][['PERSON_ID', 'TEAM_ID']]
home_team_player['Location'] = 'HOME'
away_team_player = all_players[all_players['TEAM_ID'].isin(games['VISITOR_TEAM_ID'])][['PERSON_ID', 'TEAM_ID']]
away_team_player['Location'] = 'AWAY'
players = pd.concat([home_team_player, away_team_player])
game_team = pd.concat([games[['HOME_TEAM_ID', 'GAME_ID']].rename(columns = {'HOME_TEAM_ID': 'TEAM_ID'}),
games[['VISITOR_TEAM_ID', 'GAME_ID']].rename(columns = {'VISITOR_TEAM_ID': 'TEAM_ID'})])
players = pd.merge(players, game_team, on = 'TEAM_ID')
team_team = pd.concat([games[['HOME_TEAM_ID', 'VISITOR_TEAM_ID']].\
rename(columns = {'HOME_TEAM_ID': 'TEAM_ID', 'VISITOR_TEAM_ID': 'Against_Team_ID'}),
games[['VISITOR_TEAM_ID', 'HOME_TEAM_ID']].\
rename(columns = {'VISITOR_TEAM_ID': 'TEAM_ID', 'HOME_TEAM_ID': 'Against_Team_ID'})])
return pd.merge(players, team_team, on = 'TEAM_ID')
players = get_players_new(games)
players
Out[30]:
In [4]:
def get_players(teams):
'''
return a pandas.DataFrame of all the teams' players.
teams must be a pandas.DataFrame created by the function get_teams().
'''
player_list = nba_py.player.PlayerList().info()
all_teams = teams['HOME_TEAM_ID'].append(teams['VISITOR_TEAM_ID'])
players = functools.reduce(pd.Series.append,
[nba_py.team.TeamPlayers(team).season_totals()['PLAYER_ID']
for team in all_teams])
players = players.to_frame()
players['TEAM_ID'] = [int(player_list[player_list['PERSON_ID'] == p]['TEAM_ID'])
for p in players['PLAYER_ID']]
players = players[players['TEAM_ID'].isin(all_teams.values)]
players['AGAINST_TEAM_ID'] = [int(teams[teams['HOME_TEAM_ID'] == t]['VISITOR_TEAM_ID']
if t in teams['HOME_TEAM_ID'].values
else teams[teams['VISITOR_TEAM_ID'] == t]['HOME_TEAM_ID'])
for t in players['TEAM_ID']]
return players
get_players(teams)
Out[4]:
In [7]:
def get_last_n_game_logs(player_ID, game_ID, n):
'''
retrun a pandas.DataFrame of a player's last n game logs before a specific game.
'''
#game_logs = nba_py.player.PlayerGameLogs(player_ID).info()
game_logs = game_logs_201939
game_index = game_logs[game_logs['Game_ID'] < game_ID].index[:n]
return game_logs.loc[game_index, ['MIN', 'PTS', 'AST', 'OREB', 'DREB', 'STL', 'BLK',
'TOV', 'FGM', 'FGA', 'FG3M']]
last_n_game_201939 = get_last_n_game_logs('201939', '0021600437', 20)
last_n_game_201939
Out[7]:
In [8]:
def get_score_36(game_logs):
'''
return the Fantasy score of the given game logs(pandas.DataFrame).
'''
convert_to_36 = lambda x: x[['PTS', 'AST', 'OREB', 'DREB', 'STL', 'BLK',
'TOV', 'FGM', 'FGA', 'FG3M']] * 36 / x['MIN']
stats = game_logs.apply(convert_to_36, axis = 1).mean()
score = stats['PTS'] * 1 + stats['AST'] * 1.5 + \
stats['OREB'] * 1 + stats['DREB'] * 0.7 + \
stats['STL'] * 2 + stats['BLK'] * 1.8 + stats['TOV'] * -1 + \
stats['FGM'] * 0.4 + (stats['FGA'] - stats['FGM']) * -1 + stats['FG3M'] * 0.5
return score
get_score_36(last_n_game_201939)
Out[8]:
In [9]:
def predict_score_36(player_ID, game_ID):
'''
return a list of last 20/10/5 games' average score.
'''
ma_20 = get_score_36(get_last_n_game_logs(player_ID, game_ID, 20))
ma_10 = get_score_36(get_last_n_game_logs(player_ID, game_ID, 10))
ma_5 = get_score_36(get_last_n_game_logs(player_ID, game_ID, 5))
return [round(float(ma_20), 2), round(float(ma_10), 2), round(float(ma_5), 2)]
predict_score_36('201939', '0021601211')
Out[9]:
In [10]:
play_stats_cols = ['GROUP_VALUE', 'GROUP_SET', 'GP',
'MIN', 'PTS', 'AST', 'OREB', 'DREB', 'STL', 'BLK', 'TOV',
'FGM', 'FGA', 'FG3M']
In [11]:
def cal_score(stats):
play_stats_cols = ['GROUP_VALUE', 'GROUP_SET', 'GP',
'MIN', 'PTS', 'AST', 'OREB', 'DREB', 'STL', 'BLK', 'TOV',
'FGM', 'FGA', 'FG3M']
stats = stats[play_stats_cols].iloc[0]
score = stats['PTS'] * 1 + stats['AST'] * 1.5 + \
stats['OREB'] * 1 + stats['DREB'] * 0.7 + \
stats['STL'] * 2 + stats['BLK'] * 1.8 + stats['TOV'] * -1 + \
stats['FGM'] * 0.4 + (stats['FGA'] - stats['FGM']) * -1 + stats['FG3M'] * 0.5
return score
round(float(cal_score(stats_201939)), 2)
In [12]:
game_logs_201939 = nba_py.player.PlayerGameLogs('201939').info()
game_logs_201939[game_logs_201939['Game_ID'] == '0021601229'][['MIN', 'PTS', 'AST', 'OREB', 'DREB',
'STL', 'BLK', 'TOV', 'FGM', 'FGA', 'FG3M']]
Out[12]:
In [16]:
game_ID_list = game_logs_201939['Game_ID']
game_ID_list
Out[16]:
In [ ]:
In [82]:
prediction = []
for i in game_ID_list:
stats = get_score_36(game_logs_201939[game_logs_201939['Game_ID'] == i]\
[['MIN', 'PTS', 'AST', 'OREB', 'DREB',
'STL', 'BLK', 'TOV', 'FGM', 'FGA', 'FG3M']])
ma = predict_score_36('201939', i)
ma.append(round(float(stats), 2))
prediction.append(ma)
#time.sleep(2)
df = pd.DataFrame(prediction, columns = ['ma_20', 'ma_10', 'ma_5', 'comp'])[::-1]
df = df[1:]
df
Out[82]:
In [83]:
mid = lambda x: x[['ma_20', 'ma_10', 'ma_5']].quantile(0.5)
df['wtd_avg'] = df['ma_5'] * 0.5 + df['ma_10'] * 0.3 + df['ma_20'] * 0.2
df['avg'] = (df['ma_5'] + df['ma_10'] + df['ma_20']) / 3
df['mid'] = df.apply(mid, axis = 1)
df
Out[83]:
In [84]:
df['comp'].cov(df['wtd_avg'])
Out[84]:
In [85]:
df['comp'].cov(df['avg'])
Out[85]:
In [86]:
df['comp'].cov(df['mid'])
Out[86]:
In [87]:
df[['ma_20', 'ma_10', 'ma_5', 'comp']].plot(title = 'Testing', figsize = (17, 12), grid = True)
Out[87]:
In [8]:
all_players = nba_py.player.PlayerList(season = '2017-18').info()
In [12]:
home_team_player = all_players[all_players['TEAM_ID'].isin(games['HOME_TEAM_ID'])][['PERSON_ID', 'TEAM_ID']]
home_team_player['Location'] = 'HOME'
In [18]:
game_team = pd.concat([games[['HOME_TEAM_ID', 'GAME_ID']].rename(columns = {'HOME_TEAM_ID': 'TEAM_ID'}),
games[['VISITOR_TEAM_ID', 'GAME_ID']].rename(columns = {'VISITOR_TEAM_ID': 'TEAM_ID'})])
game_team
Out[18]:
In [29]:
team_team = pd.concat([games[['HOME_TEAM_ID', 'VISITOR_TEAM_ID']].rename(columns = {'HOME_TEAM_ID': 'TEAM_ID', 'VISITOR_TEAM_ID': 'Against_Team_ID'}),
games[['VISITOR_TEAM_ID', 'HOME_TEAM_ID']].rename(columns = {'VISITOR_TEAM_ID': 'TEAM_ID', 'HOME_TEAM_ID': 'Against_Team_ID'})])
team_team
Out[29]:
In [3]:
df_1 = pd.DataFrame(np.random.randn(12).reshape(3, 4))
df_1
Out[3]:
In [4]:
df_1['4'], df_1['5'] = ['four', 'five']
df_1
Out[4]:
In [14]:
nba_py.player.PlayerLastNGamesSplits('200768').last10()
Out[14]:
In [5]:
nba_py.team.TeamGameLogs('1610612759', season_type = 'Playoffs').info()
Out[5]:
In [17]:
nba_py.game.Boxscore('0041600314').player_stats()
Out[17]:
In [15]:
nba_py.player.PlayerGeneralSplits(2738).location()
Out[15]:
In [ ]: