Skip to content

Commit

Permalink
updated season and def stats
Browse files Browse the repository at this point in the history
  • Loading branch information
skuan committed Oct 4, 2019
1 parent a6fdccf commit 6b10fd5
Show file tree
Hide file tree
Showing 2 changed files with 415 additions and 9 deletions.
386 changes: 386 additions & 0 deletions NBA Stats 2019 - 2020.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,386 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from nba_api.stats.endpoints import commonplayerinfo\n",
"from nba_api.stats.endpoints._base import Endpoint\n",
"from nba_api.stats.library.http import NBAStatsHTTP\n",
"from nba_api.stats.endpoints import playerdashboardbyteamperformance\n",
"from nba_api.stats.endpoints import leaguedashplayerstats\n",
"from nba_api.stats.endpoints import teamdashboardbyteamperformance\n",
"from nba_api.stats.endpoints import playerdashboardbylastngames\n",
"from nba_api.stats.endpoints import playerdashboardbyyearoveryear\n",
"from nba_api.stats.endpoints import teamdashboardbylastngames\n",
"from nba_api.stats.endpoints import teamplayerdashboard\n",
"from nba_api.stats.static import teams\n",
"from nba_api.stats.static import players\n",
"import pandas\n",
"import inspect"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter player 1: lebron james\n",
"lebron james\n",
"W_PCT 0.35 GP 20 PTS 553 PLUS_MINUS 19\n"
]
}
],
"source": [
"#Player Input\n",
"\n",
"player1_input = input('Enter player 1: ')\n",
"print(player1_input)\n",
"\n",
"player1 = players.find_players_by_full_name(player1_input)[0]['id']\n",
"player1_name = players.find_players_by_full_name(player1_input)[0]['full_name']\n",
"\n",
"player_info = playerdashboardbylastngames.PlayerDashboardByLastNGames(player_id=player1, season='2018-19')\n",
"\n",
"# player_headers = player_info.by_year_player_dashboard.get_dict().get('headers')\n",
"# player_data = player_info.overall_player_dashboard.get_dict().get('data')[0]\n",
"\n",
"# #last 20 games\n",
"player_headers = player_info.last20_player_dashboard.get_dict().get('headers')\n",
"player_data = player_info.last20_player_dashboard.get_dict().get('data')[0]\n",
"\n",
"print(player_headers[5], player_data[5], \n",
"\tplayer_headers[2], player_data[2], \n",
"\tplayer_headers[26], player_data[26],\n",
"\tplayer_headers[27], player_data[27])"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter team 1: warriors\n",
"warriors\n",
"1610612744 PACE 101.42 OFF_RATING 113.1 DEF_RATING 106.2 GP 20 W 14 L 6 TS_PCT 0.599 MIN 965.0\n"
]
}
],
"source": [
"#Team Input\n",
"\n",
"team1_input = input('Enter team 1: ')\n",
"print(team1_input)\n",
"team1 = teams.find_teams_by_full_name(team1_input)[0]['id']\n",
"\n",
"teamdashboard = teamdashboardbylastngames.TeamDashboardByLastNGames(team_id=team1, season='2018-19')\n",
"teamdashboard_adv = teamdashboardbylastngames.TeamDashboardByLastNGames(team_id=team1, season='2018-19', \n",
" measure_type_detailed_defense='Advanced')\n",
"\n",
"#team_headers = teamdashboard.overall_team_dashboard.get_dict().get('headers')\n",
"#team_data = teamdashboard.overall_team_dashboard.get_dict().get('data')[0]\n",
"\n",
"##last 20 games\n",
"team_headers_basic = teamdashboard.last20_team_dashboard.get_dict().get('headers')\n",
"team_data_basic = teamdashboard.last20_team_dashboard.get_dict().get('data')[0]\n",
"team_headers = teamdashboard_adv.last20_team_dashboard.get_dict().get('headers')\n",
"team_data = teamdashboard_adv.last20_team_dashboard.get_dict().get('data')[0]\n",
"\n",
"# print(team1, team_headers, team_data)\n",
"print(team1, team_headers[23], team_data[23], team_headers[8], team_data[8], team_headers[10], team_data[10], \n",
" team_headers[2], team_data[2], team_headers[3], team_data[3], \n",
" team_headers[4], team_data[4], team_headers[21], team_data[21], team_headers[6], team_data[6])"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Alfonzo McKinnie GP: 20 id: 1628035 Min 274.8433333333333\n",
"Andre Iguodala GP: 14 id: 2738 Min 294.525\n",
"Andrew Bogut GP: 11 id: 101106 Min 134.36333333333334\n",
"Damion Lee GP: 12 id: 1627814 Min 154.26333333333332\n",
"DeMarcus Cousins GP: 15 id: 202326 Min 397.00166666666667\n",
"Draymond Green GP: 19 id: 203110 Min 603.2016666666667\n",
"Jacob Evans GP: 7 id: 1628980 Min 104.08666666666667\n",
"Jonas Jerebko GP: 16 id: 201973 Min 193.31666666666666\n",
"Jordan Bell GP: 17 id: 1628395 Min 207.17333333333335\n",
"Kevin Durant GP: 17 id: 201142 Min 535.505\n",
"Kevon Looney GP: 18 id: 1626172 Min 248.05833333333334\n",
"Klay Thompson GP: 17 id: 202691 Min 556.8016666666666\n",
"Marcus Derrickson GP: 2 id: 1629094 Min 13.166666666666666\n",
"Quinn Cook GP: 20 id: 1626188 Min 314.12\n",
"Shaun Livingston GP: 15 id: 2733 Min 217.76\n",
"Stephen Curry GP: 18 id: 201939 Min 576.8133333333334\n",
"['Alfonzo McKinnie', 'Andre Iguodala', 'Andrew Bogut', 'Damion Lee', 'DeMarcus Cousins', 'Draymond Green', 'Jacob Evans', 'Jonas Jerebko', 'Jordan Bell', 'Kevin Durant', 'Kevon Looney', 'Klay Thompson', 'Marcus Derrickson', 'Quinn Cook', 'Shaun Livingston', 'Stephen Curry']\n"
]
}
],
"source": [
"roster1 = teamplayerdashboard.TeamPlayerDashboard(team_id=team1, last_n_games=20, season='2018-19').get_dict()#.PlayersSeasonTotals(team_id=team1, last_n_games=20)\n",
"team1_roster_last20 = []\n",
"\n",
"#print(roster1)['resultSets'][1])#['rowSet'])\n",
"for n in roster1['resultSets'][1]['rowSet']:\n",
" print(n[2], 'GP:', n[3], 'id:', n[1], 'Min', n[7])\n",
" team1_roster_last20.append(n[2])\n",
" \n",
"print(team1_roster_last20)\n",
"#(PER * MP)/n_players"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def per_calculator(player, team):\n",
" \n",
" agg_PER = 0 \n",
" agg_adj_PER = 0\n",
" num_players = 0\n",
" PER_MP = 0\n",
" \n",
" for n in player:\n",
" p_id = players.find_players_by_full_name(n)[0]['id']\n",
" p_info = playerdashboardbylastngames.PlayerDashboardByLastNGames(player_id=p_id, season='2018-19')\n",
" ##last 20 games \n",
" player_headers = p_info.last20_player_dashboard.get_dict().get('headers')\n",
" player_data = p_info.last20_player_dashboard.get_dict().get('data')[0]\n",
" # player_headers = player_info.overall_player_dashboard.get_dict().get('headers')\n",
" # player_data = player_info.overall_player_dashboard.get_dict().get('data')[0]\n",
"\n",
" \n",
" #####PER player stats####\n",
" MP = player_data[6]\n",
" ThrP = player_data[10] \n",
" AST = player_data[19]\n",
" TOV = player_data[20]\n",
" FGA = player_data[8]\n",
" FG = player_data[7] + player_data[10] \n",
" FTA = player_data[14]\n",
" FT = player_data[13]\n",
" TRB = player_data[18]\n",
" ORB = player_data[16]\n",
" STL = player_data[21]\n",
" BLK = player_data[22]\n",
" PF = player_data[24]\n",
"\n",
" ###PER league stats (2018-19)###\n",
" lg_AST = 24.6\n",
" lg_FG = 41.1\n",
" lg_PTS = 111.2\n",
" lg_FGA = 89.2\n",
" lg_FT = 17.7\n",
" lg_FTA = 23.1\n",
" lg_TRB = 45.2\n",
" lg_ORB = 10.3\n",
" lg_TOV = 14.1\n",
" lg_PF = 20.9\n",
" lg_pace = 100\n",
" \n",
" ###PER team stats###\n",
" team_AST = team_data_basic[19]\n",
" team_FG = team_data_basic[7]+team_data_basic[10]\n",
" team_pace = team_data[23] \n",
" \n",
" factor = (2/3) - (0.5*(lg_AST/lg_FG)) / (2*(lg_FG/lg_FT)) \n",
" VOP = lg_PTS / (lg_FGA-lg_ORB+lg_TOV+0.44*lg_FTA) \n",
" DRB_perc = (lg_TRB -lg_ORB) / lg_TRB\n",
"\n",
" ####PER Calculation#### \n",
" PER_calc_player = (1 / MP) * ( ThrP + (2/3) * AST \n",
" + (2 - factor * (team_AST / team_FG)) * FG \n",
" + (FT *0.5 * (1 + (1 - (team_AST / team_FG)) + (2/3) * (team_AST / team_FG)))\n",
" - VOP * TOV - VOP * DRB_perc * (FGA - FG)\n",
" - VOP * 0.44 * (0.44 + (0.56 * DRB_perc)) * (FTA - FT)\n",
" + VOP * (1 - DRB_perc) * (TRB - ORB) + VOP * DRB_perc * ORB \n",
" + VOP * STL + VOP * DRB_perc * BLK - PF \n",
" * ((lg_FT / lg_PF) - 0.44 * (lg_FTA / lg_PF) * VOP) )\n",
"\n",
" adj_PER_calc_player = PER_calc_player * (lg_pace /team_pace)\n",
" \n",
" agg_PER += PER_calc_player \n",
" agg_adj_PER += adj_PER_calc_player\n",
" num_players +=1\n",
" PER_MP += (adj_PER_calc_player * MP)\n",
" PER_MP_n = PER_MP/num_players\n",
"\n",
" \n",
" print(n, 'Min', MP, PER_calc_player, adj_PER_calc_player, 'PER * MP', \n",
" PER_MP)\n",
" \n",
" print('Total PER', agg_PER, 'Total adj_PER', agg_adj_PER, 'PERxMP/n', PER_MP_n)\n",
" print('team ast', team_AST, 'team FG', team_FG, 'team_pace', team_pace)\n",
"# print('league stats:', factor, VOP, DRB_perc)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Alfonzo McKinnie Min 274.8433333333333 0.39539311420380246 0.3898571427763779 PER * MP 107.14963664446894\n",
"Andre Iguodala Min 450.7416666666667 0.343089953318288 0.33828628802828636 PER * MP 259.6293619208188\n",
"Andrew Bogut Min 134.36333333333334 0.29901186886644804 0.2948253489118991 PER * MP 299.2430785517846\n",
"Damion Lee Min 210.19666666666666 0.47555311365770786 0.4688948073927311 PER * MP 397.8032040830454\n",
"DeMarcus Cousins Min 541.9016666666666 0.5710236741140374 0.5630286670420405 PER * MP 702.9093771342389\n",
"Draymond Green Min 641.9516666666667 0.3535842139212086 0.3486336165659718 PER * MP 926.7153083447921\n",
"Jacob Evans Min 143.59666666666666 0.1721844473815655 0.1697736613898299 PER * MP 951.0942402081671\n",
"Jonas Jerebko Min 246.43333333333334 0.4388123989212201 0.43266850613411567 PER * MP 1057.718182403151\n",
"Jordan Bell Min 257.43833333333333 0.25539451849822015 0.2518186930568134 PER * MP 1122.5459670458754\n",
"Kevin Durant Min 648.11 0.6111160955975645 0.602559747187502 PER * MP 1513.0709647955673\n",
"Kevon Looney Min 268.89166666666665 0.44065073844496566 0.4344811067294081 PER * MP 1629.8993137192158\n",
"Klay Thompson Min 667.555 0.639432214130078 0.6304794065569691 PER * MP 2050.778993963353\n",
"Marcus Derrickson Min 67.32166666666667 0.8098689829557822 0.7985298589585705 PER * MP 2104.5373549515425\n",
"Quinn Cook Min 314.12 0.5571714508683491 0.5493703913117226 PER * MP 2277.1055822703806\n",
"Shaun Livingston Min 292.22833333333335 0.21878081054718512 0.21571762033838013 PER * MP 2340.144382932498\n",
"Stephen Curry Min 648.58 0.8739014920702461 0.8616658371822581 PER * MP 2899.003611612167\n",
"Total PER 7.45496908749667 Total adj_PER 7.350590699562875 PERxMP/n 181.18772572576043\n",
"team ast 603 team FG 1160 team_pace 101.42\n"
]
}
],
"source": [
"per_calculator(team1_roster_last20, team1)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"headers: ['PLAYER_ID', 'PLAYER_NAME', 'TEAM_ID', 'TEAM_ABBREVIATION', 'AGE', 'GP', 'W', 'L', 'W_PCT', 'MIN', 'DEF_RATING', 'DREB', 'DREB_PCT', 'PCT_DREB', 'STL', 'PCT_STL', 'BLK', 'PCT_BLK', 'OPP_PTS_OFF_TOV', 'OPP_PTS_2ND_CHANCE', 'OPP_PTS_FB', 'OPP_PTS_PAINT', 'DEF_WS', 'GP_RANK', 'W_RANK', 'L_RANK', 'W_PCT_RANK', 'MIN_RANK', 'DEF_RATING_RANK', 'DREB_RANK', 'DREB_PCT_RANK', 'PCT_DREB_RANK', 'STL_RANK', 'PCT_STL_RANK', 'BLK_RANK', 'PCT_BLK_RANK', 'OPP_PTS_OFF_TOV_RANK', 'OPP_PTS_2ND_CHANCE_RANK', 'OPP_PTS_FB_RANK', 'OPP_PTS_PAINT_RANK', 'DEF_WS_RANK', 'CFID', 'CFPARAMS']\n",
"Alfonzo McKinnie \n",
" Def Rating 106.5 Min 274.8433333333333\n",
"Andre Iguodala \n",
" Def Rating 105.8 Min 294.525\n",
"Andrew Bogut \n",
" Def Rating 110.5 Min 134.36333333333334\n",
"Damion Lee \n",
" Def Rating 105.7 Min 154.26333333333332\n",
"DeMarcus Cousins \n",
" Def Rating 104.0 Min 397.00166666666667\n",
"Draymond Green \n",
" Def Rating 101.5 Min 603.2016666666667\n",
"Jacob Evans \n",
" Def Rating 102.3 Min 104.08666666666667\n",
"Jonas Jerebko \n",
" Def Rating 117.1 Min 193.31666666666666\n",
"Jordan Bell \n",
" Def Rating 112.0 Min 207.17333333333335\n",
"Kevin Durant \n",
" Def Rating 105.2 Min 535.505\n",
"Kevon Looney \n",
" Def Rating 101.9 Min 248.05833333333334\n",
"Klay Thompson \n",
" Def Rating 102.8 Min 556.8016666666666\n",
"Marcus Derrickson \n",
" Def Rating 81.5 Min 13.166666666666666\n",
"Quinn Cook \n",
" Def Rating 111.5 Min 314.12\n",
"Shaun Livingston \n",
" Def Rating 111.7 Min 217.76\n",
"Stephen Curry \n",
" Def Rating 99.1 Min 576.8133333333334\n"
]
}
],
"source": [
"#Individual Player Defense Stats\n",
"\n",
"player_info_def = leaguedashplayerstats.LeagueDashPlayerStats(last_n_games=20, season='2018-19', \n",
" measure_type_detailed_defense='Defense')\n",
"\n",
"player_def_headers = player_info_def.league_dash_player_stats.get_dict().get('headers')\n",
"player_def_data = player_info_def.league_dash_player_stats.get_dict().get('data')\n",
"\n",
"print('headers:', player_def_headers)\n",
"#print('data:', player_def_data)\n",
"\n",
"for p in team1_roster_last20:\n",
" for n in player_def_data:\n",
" if p in n:\n",
" print(n[1], '\\n', 'Def Rating', n[10], 'Min', n[9])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#Theory: If second game of back-to-back is a road game, odds of winning decrease 7-8%, multiply ratings by 0.93."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 6b10fd5

Please sign in to comment.