{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"events2015 = pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MEvents2015.csv')\nevents2015.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"events2015['EventType'].value_counts().index","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"events2015.loc[events2015['EventType'] == 'sub']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### So you must be wondering why I am trying to look at substitution data\n\nBasketball is a game of lineups and matchups. Usually, the best lineup on the floor has the highest chance of winning, no matter the amount of coaching. This is probably not a common thought with college basketball as the coach is usually considered the superstar, but we are increasingly getting players like Ja Morant, Trae Young and Zion Williamson whose insane talent has carried teams."},{"metadata":{"trusted":true},"cell_type":"code","source":"players = pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MPlayers.csv')\nteams = pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MTeams.csv')\n\nplayers.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"players.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"teams.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now I want to calculate the PER (Player Efficiency Rating) for each player. https://www.basketball-reference.com/about/per.html\n\nFor this, I would need the season stats for each player. For this, I will be using the sports reference python package!"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install sportsreference","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sportsreference.ncaab.roster import Player\nfrom sportsreference.ncaab.teams import Teams","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp_player_df = players.loc[players['FirstName'] == 'Karl-Anthony'].loc[players['LastName'] == 'Towns'].iloc[0] #Looking at Kentucky star and No. 1 pick Karl-Anthony Towns \nprint(temp_player_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def GetReferenceIDForPlayer(playerdf):\n    teamid = playerdf['TeamID']\n    players_team = teams.loc[teams['TeamID'] == teamid]\n    allTeams = Teams(year=2015)\n    for team in allTeams:\n        if(team.name.startswith(players_team.iloc[0]['TeamName'])):\n            print('Team found')\n            print(team.name)\n            roster = team.roster  # Gets each team's roster\n            for player in roster.players:\n                if(playerdf['FirstName'] in player.name and playerdf['LastName'] in player.name):\n                    print(player.player_id)\n                    print(player.player_efficiency_rating)\n                    print(player.points)\n                    return","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"GetReferenceIDForPlayer(temp_player_df)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"There are a bunch of caveats with this process:\n- The queries to this api is really slow and takes about 20 seconds per request. Obviously we can't keep running the code above to get the player efficiency rating for every player at runtime. And the code isn't the most efficient\n- We are finding the player with a string compare which is again not very accurate. We could pre-prepare the data to have a direct reference to the sportreference stats\n- Sportsreference does not have the PER values for all the players. We could normalize this database with a random number in the average PER range"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}