{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# MLB Player Digital Engagement Forecasting","metadata":{}},{"cell_type":"markdown","source":"Ideas\n* Do rivalry games create more digital content for players?\n* Are the best players on the best teams the most followed on twitter?\n* Do other sporting events impact the digital content for MLB?\n* Does digital engagement change during the innings?\n* Does the all-star event impact players performances?\n* Do awards benefit players digital content?\n* Do twitter followers engage with the best players?","metadata":{}},{"cell_type":"markdown","source":"### 1. Importing the data","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport plotly.express as px\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        print(filename)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:36.536384Z","iopub.execute_input":"2021-06-20T09:39:36.536896Z","iopub.status.idle":"2021-06-20T09:39:38.130850Z","shell.execute_reply.started":"2021-06-20T09:39:36.536773Z","shell.execute_reply":"2021-06-20T09:39:38.129482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Review each of the csv files to understand what is available\ndir_name = '/kaggle/input/mlb-player-digital-engagement-forecasting/'\ndata = ['players', 'teams', 'seasons', 'awards']\n# Create a list of dataframes\ncsvs = [pd.read_csv(f'{dir_name}{d}.csv') for d in data]","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:38.132787Z","iopub.execute_input":"2021-06-20T09:39:38.133194Z","iopub.status.idle":"2021-06-20T09:39:38.214771Z","shell.execute_reply.started":"2021-06-20T09:39:38.133162Z","shell.execute_reply":"2021-06-20T09:39:38.213417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2. Exploratory data analysis","metadata":{}},{"cell_type":"markdown","source":"#### 2a. Awards data","metadata":{}},{"cell_type":"code","source":"# Import the awards data and set date\nawards = pd.read_csv(f'{dir_name}awards.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:38.216959Z","iopub.execute_input":"2021-06-20T09:39:38.217317Z","iopub.status.idle":"2021-06-20T09:39:38.244430Z","shell.execute_reply.started":"2021-06-20T09:39:38.217278Z","shell.execute_reply":"2021-06-20T09:39:38.243181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Variable data types\nawards.dtypes","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:38.247090Z","iopub.execute_input":"2021-06-20T09:39:38.247615Z","iopub.status.idle":"2021-06-20T09:39:38.267260Z","shell.execute_reply.started":"2021-06-20T09:39:38.247567Z","shell.execute_reply":"2021-06-20T09:39:38.266155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Describe the key variables within the awards DataFrame\n# Including the 'all' parameter allows the string variables to be included in the output\nawards.describe(include='all', datetime_is_numeric=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:38.269297Z","iopub.execute_input":"2021-06-20T09:39:38.269816Z","iopub.status.idle":"2021-06-20T09:39:38.356007Z","shell.execute_reply.started":"2021-06-20T09:39:38.269766Z","shell.execute_reply":"2021-06-20T09:39:38.354662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sample of the dataframe\nawards.sample(10)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:38.357529Z","iopub.execute_input":"2021-06-20T09:39:38.357854Z","iopub.status.idle":"2021-06-20T09:39:38.380324Z","shell.execute_reply.started":"2021-06-20T09:39:38.357821Z","shell.execute_reply":"2021-06-20T09:39:38.378657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#word cloud visualisation to show the popular neighbourhoods\nfrom wordcloud import WordCloud\n\nplt.subplots(figsize=(20,15))\nwordcloud = WordCloud(\n                          width=1920,\n                          height=1080\n                         ).generate(\" \".join(awards.playerName))\nplt.imshow(wordcloud)\nplt.title('Word Cloud for Player Name Awards')\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:38.382118Z","iopub.execute_input":"2021-06-20T09:39:38.382507Z","iopub.status.idle":"2021-06-20T09:39:44.095782Z","shell.execute_reply.started":"2021-06-20T09:39:38.382467Z","shell.execute_reply":"2021-06-20T09:39:44.094240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Understand the 10 largest award winners\ntop_players = awards.groupby('playerName')['playerName'].count().nlargest(n=10, keep='all')\ntop_players","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:44.098774Z","iopub.execute_input":"2021-06-20T09:39:44.099251Z","iopub.status.idle":"2021-06-20T09:39:44.118765Z","shell.execute_reply.started":"2021-06-20T09:39:44.099204Z","shell.execute_reply":"2021-06-20T09:39:44.117645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For the 10 largest award winners. Lets understand the number of unique awards won\n# Did one player dominate a single award\nplayer = awards.groupby('playerName').agg(\n    {\n        'playerName' : 'count',\n        'awardSeason' : ['min', 'max'],\n        'awardId' : pd.Series.nunique\n    }\n).nlargest(10, ('playerName', 'count'))\nplayer","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:44.120687Z","iopub.execute_input":"2021-06-20T09:39:44.121324Z","iopub.status.idle":"2021-06-20T09:39:44.467448Z","shell.execute_reply.started":"2021-06-20T09:39:44.121277Z","shell.execute_reply":"2021-06-20T09:39:44.466727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert date variable from object to datetime\nawards['awardDate'] = pd.to_datetime(awards['awardDate'])\nawards.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:44.468634Z","iopub.execute_input":"2021-06-20T09:39:44.469126Z","iopub.status.idle":"2021-06-20T09:39:44.492964Z","shell.execute_reply.started":"2021-06-20T09:39:44.469052Z","shell.execute_reply":"2021-06-20T09:39:44.491820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Animated scattergraph to review player awards by time for the largest award winners\ntp_array = np.array(awards['playerName'].isin(player.index))\nawards_tp = awards.loc[(tp_array)]","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:44.494431Z","iopub.execute_input":"2021-06-20T09:39:44.494738Z","iopub.status.idle":"2021-06-20T09:39:44.502577Z","shell.execute_reply.started":"2021-06-20T09:39:44.494709Z","shell.execute_reply":"2021-06-20T09:39:44.501306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"awards_tp.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:44.504692Z","iopub.execute_input":"2021-06-20T09:39:44.505186Z","iopub.status.idle":"2021-06-20T09:39:44.529943Z","shell.execute_reply.started":"2021-06-20T09:39:44.505137Z","shell.execute_reply":"2021-06-20T09:39:44.528940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"awards_sum = awards_tp.groupby(['playerName', 'awardSeason'])['playerId'].count()\nawards_sum1 = awards_sum.reset_index()\nawards_sum1","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:44.531427Z","iopub.execute_input":"2021-06-20T09:39:44.531929Z","iopub.status.idle":"2021-06-20T09:39:44.561206Z","shell.execute_reply.started":"2021-06-20T09:39:44.531878Z","shell.execute_reply":"2021-06-20T09:39:44.560092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar(awards_sum1, x='playerName', y='playerId', color='playerName',\n            animation_frame='awardSeason')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:39:44.562968Z","iopub.execute_input":"2021-06-20T09:39:44.563452Z","iopub.status.idle":"2021-06-20T09:39:46.353278Z","shell.execute_reply.started":"2021-06-20T09:39:44.563405Z","shell.execute_reply":"2021-06-20T09:39:46.351689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 2b. Players","metadata":{}},{"cell_type":"code","source":"# Import the players data and set date\ndf_p = pd.read_csv(f'{dir_name}players.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:41:40.564259Z","iopub.execute_input":"2021-06-20T09:41:40.564654Z","iopub.status.idle":"2021-06-20T09:41:40.582464Z","shell.execute_reply.started":"2021-06-20T09:41:40.564621Z","shell.execute_reply":"2021-06-20T09:41:40.580962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_p.sample(10)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:41:49.716833Z","iopub.execute_input":"2021-06-20T09:41:49.717250Z","iopub.status.idle":"2021-06-20T09:41:49.741299Z","shell.execute_reply.started":"2021-06-20T09:41:49.717215Z","shell.execute_reply":"2021-06-20T09:41:49.739897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Distribution of players by country\ndf_p_s1 = df_p.groupby('birthCountry').agg(\n    {\n        'playerName' : 'count'\n    }\n)\n\n# Bar chart\nfig = px.bar(df_p_s1, x=df_p_s1.index, y=\"playerName\", title=\"Distribution by Country\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:49:36.383550Z","iopub.execute_input":"2021-06-20T09:49:36.383984Z","iopub.status.idle":"2021-06-20T09:49:36.456869Z","shell.execute_reply.started":"2021-06-20T09:49:36.383942Z","shell.execute_reply":"2021-06-20T09:49:36.455699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert the mlbDebutDate and DOB to datetime\ndf_p['mlbDebutDate'] = pd.to_datetime(df_p['mlbDebutDate'])\ndf_p['DOB'] = pd.to_datetime(df_p['DOB'])\n","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:55:07.777199Z","iopub.execute_input":"2021-06-20T09:55:07.777981Z","iopub.status.idle":"2021-06-20T09:55:07.800250Z","shell.execute_reply.started":"2021-06-20T09:55:07.777926Z","shell.execute_reply":"2021-06-20T09:55:07.798527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Debut year and birth year\ndf_p['mlbDebutYear'] = df_p['mlbDebutDate'].dt.year\ndf_p['DOBYear'] = df_p['DOB'].dt.year\n\n# What age is average for starting in MLB\ndf_p['DebutAge'] = df_p['mlbDebutYear'] - df_p['DOBYear']","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:55:09.191490Z","iopub.execute_input":"2021-06-20T09:55:09.191932Z","iopub.status.idle":"2021-06-20T09:55:09.207274Z","shell.execute_reply.started":"2021-06-20T09:55:09.191893Z","shell.execute_reply":"2021-06-20T09:55:09.205228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# summary of the numeric values\ndf_p.describe()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:55:22.226848Z","iopub.execute_input":"2021-06-20T09:55:22.227537Z","iopub.status.idle":"2021-06-20T09:55:22.267559Z","shell.execute_reply.started":"2021-06-20T09:55:22.227491Z","shell.execute_reply":"2021-06-20T09:55:22.266803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Has the distribution of new players got younger over time?\nage_sum = df_p.groupby(['mlbDebutYear', 'DebutAge'])['playerName'].count()\nage_sum = age_sum.reset_index()\nage_sum\nfig = px.bar(age_sum, x=\"mlbDebutYear\", y=\"playerName\", color=\"DebutAge\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T10:05:08.564198Z","iopub.execute_input":"2021-06-20T10:05:08.564860Z","iopub.status.idle":"2021-06-20T10:05:08.643348Z","shell.execute_reply.started":"2021-06-20T10:05:08.564819Z","shell.execute_reply":"2021-06-20T10:05:08.642451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This output will be impacted by players who have left the baseball dataset over time. The players that remain are only included. Therefore the most recent years provide a fairer reflection of the age distribution for MLB debut's. ","metadata":{}},{"cell_type":"code","source":"# Review a scatter plot\nfig = px.scatter(\n    age_sum, x='mlbDebutYear', y='DebutAge', opacity=0.65, size=\"playerName\",\n    trendline='ols', trendline_color_override='darkblue'\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T10:15:54.923378Z","iopub.execute_input":"2021-06-20T10:15:54.923780Z","iopub.status.idle":"2021-06-20T10:15:55.014036Z","shell.execute_reply.started":"2021-06-20T10:15:54.923746Z","shell.execute_reply":"2021-06-20T10:15:55.012850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}