def nest_data_for_all_players_season(season, fga_filter=500, override_file=False): shots_df = get_shots_for_all_players_season(season, override_file=override_file) shots_df['zone_area'] = shots_df['zone'] + ' ' + shots_df['area'] general_stats = GeneralPlayerStats().get_data({'Season': season, 'PerMode': 'Totals'}, override_file=override_file) general_stats = general_stats[general_stats['FGA'] >= fga_filter] general_stats = calculate_overall_stats(general_stats) general_stats = general_stats[['PLAYER_NAME', 'ppg', 'efg', 'efg_pct']] players = general_stats['PLAYER_NAME'].unique().tolist() general_stats = general_stats.set_index('PLAYER_NAME').T.to_dict() shots_df['x'] = shots_df['x'].apply(lambda lx: round(lx)) shots_df['y'] = shots_df['y'].apply(lambda ly: round(ly)) zone_areas = shots_df['zone_area'].unique().tolist() league_averages = calculate_league_averages_by_zone(shots_df, zone_areas) zone_map = generate_zone_map(shots_df) shot_data = {'zone_map': zone_map, 'players': {}} for player in players: player_df = shots_df[shots_df['shooter'] == player] print(player + ': ' + str(len(player_df))) shot_data['players'][player] = nest_shot_data_by_xy_for_entity(player_df, league_averages, zone_areas) shot_data['players'][player]['stats'] = general_stats[player] return shot_data
def get_team_logos(): teams = GeneralPlayerStats().get_data({})['TEAM_ABBREVIATION'].unique() logo_url = 'http://stats.nba.com/media/img/teams/logos/{}_logo.svg' logo_file_location = './img/{}.svg' for t in teams: team_logo_url = logo_url.format(t) team_logo_file_location = logo_file_location.format(t) response = requests.get(team_logo_url, stream=True) with open(team_logo_file_location, 'wb') as out_file: shutil.copyfileobj(response.raw, out_file) del response
def get_player_pictures(season): players = GeneralPlayerStats().get_data({'Season': season})['PLAYER_ID'].unique() image_url = 'https://ak-static.cms.nba.com/wp-content/uploads/headshots/nba/1610612740/2017/260x190/{}.png' image_file_location = './img/{}.svg' for p in players: player_url = image_url.format(p) player_file_location = image_file_location.format(p) response = requests.get(player_url, stream=True) with open(player_file_location, 'wb') as out_file: shutil.copyfileobj(response.raw, out_file) del response
def determine_from_listed_position(): general_stats_ep = GeneralPlayerStats() guards = general_stats_ep.get_data({ 'Season': '2017-18', 'PlayerPosition': 'G' }) forwards = general_stats_ep.get_data({ 'Season': '2017-18', 'PlayerPosition': 'F' }) centers = general_stats_ep.get_data({ 'Season': '2017-18', 'PlayerPosition': 'C' }) guards['G'] = 1 forwards['F'] = 1 centers['C'] = 1 merge_df = pd.merge( guards, forwards, on=['PLAYER_NAME', 'PLAYER_ID', 'TEAM_ABBREVIATION', 'TEAM_ID'], how='outer') merge_df = pd.merge( merge_df, centers, on=['PLAYER_NAME', 'PLAYER_ID', 'TEAM_ABBREVIATION', 'TEAM_ID'], how='outer') merge_df = merge_df[[ 'PLAYER_NAME', 'PLAYER_ID', 'TEAM_ABBREVIATION', 'TEAM_ID', 'G', 'F', 'C' ]] merge_df = merge_df.fillna(0) conditions = [ ((merge_df['G'] == 1) & (merge_df['F'] == 0) & (merge_df['C'] == 0)), ((merge_df['F'] == 1) & (merge_df['C'] == 0)), (merge_df['C'] == 1) ] choices = ['Guard', 'Wing', 'Big'] merge_df['POSITION'] = np.select(conditions, choices, default='None') return merge_df
def analyze_stint_data(stints_df, season): player_stats = GeneralPlayerStats().get_data( { 'Season': season, 'MeasureType': 'Base', 'PerMode': 'PerGame' }, override_file=True)[['PLAYER_NAME', 'MIN', 'GP']] player_stats = player_stats[(player_stats['MIN'] >= 20) & (player_stats['GP'] >= 20)] data = [] for ix, player in player_stats.iterrows(): player_stints = stints_df[stints_df['player'] == player.PLAYER_NAME] average_stint_time = player_stints['time'].mean() data.append({ 'player': player.PLAYER_NAME, 'average_stint': average_stint_time, 'seconds_per_game': player.MIN * 60, 'game': player.GP, 'stints_per_game': len(player_stints) / player.GP, '10_min_stints': len(player_stints[player_stints['time'] >= 10 * 60]), '15_min_stints': len(player_stints[player_stints['time'] >= 15 * 60]), '20_min_stints': len(player_stints[player_stints['time'] >= 20 * 60]), '25_min_stints': len(player_stints[player_stints['time'] >= 25 * 60]), '30_min_stints': len(player_stints[player_stints['time'] >= 30 * 60]), }) data_df = pd.DataFrame(data) data_df['mpg_to_stint'] = data_df['seconds_per_game'] / data_df[ 'average_stint'] data_df = data_df.sort_values(by='30_min_stints', ascending=False) return data_df
from util.data_scrappers.nba_stats import GeneralPlayerStats, TrackingStats from util.merge_shot_pbp import merge_shot_pbp_for_season from util.format import get_year_string, print_reddit_table import pandas as pd general_player_ep = GeneralPlayerStats() tracking_ep = TrackingStats() def get_stats_for_player_season(season, games_filter=50, assist_filter=5, override_file=False): shots_df = merge_shot_pbp_for_season(season, override_file=override_file) # Calculate average efficiency from each zone of the court shot_zones = { 'Above the Break 3': 0, 'Corner 3': 0, 'Mid-Range': 0, 'In The Paint \(Non-RA\)': 0, 'Restricted Area': 0 } for z in shot_zones: zone_df = shots_df[shots_df['SHOT_ZONE_BASIC'].str.contains(z)] fga = len(zone_df) fgm = zone_df.SHOT_MADE_FLAG.sum() val = 3 if '3' in z else 2 points = fgm * val shot_zones[z] = round(points / fga, 2)
from util.data_scrappers.nba_stats import GeneralPlayerStats from util.merge_shot_pbp import merge_shot_pbp_for_season import pandas as pd import plotly.plotly as py import plotly.graph_objs as go year = '2017-18' min_filter = 0 df = merge_shot_pbp_for_season(year) guards_df = GeneralPlayerStats().get_data({ 'Season': year, 'PlayerPosition': 'C', 'PerMode': 'Totals' }) guards_df = guards_df[guards_df['MIN'] >= min_filter] guards = guards_df['PLAYER_NAME'].tolist() data = [] for g in guards: g_df = df[df['PLAYER1_NAME'] == g] if len(g_df) == 0: continue team_name = g_df['PLAYER1_TEAM_ABBREVIATION'].iloc[0] total_attempts = len(g_df)
from util.data_scrappers.nba_stats import PlayerAdvancedGameLogs, GeneralPlayerStats from util.format import get_year_string, print_reddit_table import pandas as pd import plotly.plotly as py import plotly.graph_objs as go logs = PlayerAdvancedGameLogs() general_stats = GeneralPlayerStats() def get_data_for_year(season, stat_to_graph='PTS', num_games_filter=50, num_players_filter=10, min_played_filter=20, is_multi_year=False, normalize_for_possessions=False, data_override=False): base_log_df = logs.get_data({ 'Season': season, 'MeasureType': 'Base' }, override_file=data_override) advanced_log_df = logs.get_data( { 'Season': season, 'MeasureType': 'Advanced' }, override_file=data_override) advanced_log_df = advanced_log_df[[ 'PLAYER_ID', 'GAME_ID', 'PACE', 'TS_PCT'
from util.data_scrappers.nba_stats import GeneralPlayerStats, TrackingStats import pandas as pd data_override = False generalStats = GeneralPlayerStats() trackingStats = TrackingStats() def get_true_usage_for_year(year): base_cols = [ 'PLAYER_NAME', 'PLAYER_ID', 'TEAM_ABBREVIATION', 'TEAM_ID', 'MIN', 'PTS', 'FGA', 'FTA', 'TOV' ] base_df = generalStats.get_data( { 'Season': year, 'PerMode': 'Totals', 'MeasureType': 'Base' }, override_file=data_override)[base_cols] advanced_cols = ['PLAYER_ID', 'TEAM_ID', 'PACE', 'TS_PCT'] advanced_df = generalStats.get_data( { 'Season': year, 'PerMode': 'Totals', 'MeasureType': 'Advanced' }, override_file=data_override)[advanced_cols] passing_cols = ['PLAYER_ID', 'TEAM_ID', 'POTENTIAL_AST', 'AST_PTS_CREATED']
from util.data_scrappers.nba_stats import GeneralPlayerStats df = GeneralPlayerStats().get_data({}) None