{"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":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2021-07-13T16:33:09.082729Z","iopub.execute_input":"2021-07-13T16:33:09.08309Z","iopub.status.idle":"2021-07-13T16:33:09.08769Z","shell.execute_reply.started":"2021-07-13T16:33:09.083059Z","shell.execute_reply":"2021-07-13T16:33:09.086881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR = Path('../input/mlb-pdef-train-dataset')\nBASE_DIR = Path('../input/mlb-player-digital-engagement-forecasting')\ntargets = pd.read_pickle(TRAIN_DIR / 'nextDayPlayerEngagement_train.pkl')\nplayers = pd.read_csv(BASE_DIR / 'players.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-13T16:33:11.618237Z","iopub.execute_input":"2021-07-13T16:33:11.618851Z","iopub.status.idle":"2021-07-13T16:33:11.800099Z","shell.execute_reply.started":"2021-07-13T16:33:11.61881Z","shell.execute_reply":"2021-07-13T16:33:11.799272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Short analysis to see if age has any impact on the Social Media noise a Player generates","metadata":{}},{"cell_type":"code","source":"players['DOB'] = pd.to_datetime(players['DOB'],\n                                format = '%Y-%m-%d')\n\ntargets['date'] = pd.to_datetime(targets['date'],\n                                format = '%Y%m%d')\ntargets['year'] = targets['date'].dt.year\n\n\nplayers_target = pd.merge(targets,\n                          players,\n                          on = 'playerId',\n                          how = 'left')\n\nplayers_target['PLAYER_AGE'] = (players_target['date'] - pd.to_datetime(players_target['DOB'])).dt.days/365","metadata":{"execution":{"iopub.status.busy":"2021-07-13T16:33:14.319287Z","iopub.execute_input":"2021-07-13T16:33:14.319814Z","iopub.status.idle":"2021-07-13T16:33:16.027065Z","shell.execute_reply.started":"2021-07-13T16:33:14.319764Z","shell.execute_reply":"2021-07-13T16:33:16.025937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean=np.ceil(players_target['PLAYER_AGE'].mean())\nmedian=np.ceil(players_target['PLAYER_AGE'].median())","metadata":{"execution":{"iopub.status.busy":"2021-07-13T16:33:16.276022Z","iopub.execute_input":"2021-07-13T16:33:16.276505Z","iopub.status.idle":"2021-07-13T16:33:16.31902Z","shell.execute_reply.started":"2021-07-13T16:33:16.276463Z","shell.execute_reply":"2021-07-13T16:33:16.317953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nsns.set_style('white')\nhist_plot = sns.histplot(players_target['PLAYER_AGE'], )\nhist_plot.axvline(mean, color='r', linestyle='--', linewidth = 4, label = f'mean-{mean}')\nhist_plot.axvline(median, color='g', linestyle='-', linewidth = 4, label = f'median-{median}')\nplt.suptitle(\"Players Age Distribution\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2021-07-13T16:33:17.69313Z","iopub.execute_input":"2021-07-13T16:33:17.693727Z","iopub.status.idle":"2021-07-13T16:33:20.454082Z","shell.execute_reply.started":"2021-07-13T16:33:17.693674Z","shell.execute_reply":"2021-07-13T16:33:20.452938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players_target['PLAYER_AGE'] = round(players_target['PLAYER_AGE']).astype('int')\nfig, axs = plt.subplots(4,2, figsize = (20, 12))\nsns.set_style('white')\nsns.set(font_scale = 1)\ni = 0\nfor target in ['target1', 'target2', 'target3', 'target4']:\n    target_median = players_target.groupby('PLAYER_AGE')[target].agg(['median', 'sum']).reset_index()   \n    sns.set_style('white')\n    bar_plot = sns.barplot(ax=axs[i, 0], x = target_median['PLAYER_AGE'], y = target_median['median'])\n    axs[i, 0].set(ylabel = f\"{target} \"+ \"MEDIAN\")\n    sns.barplot(ax=axs[i, 1], x = target_median['PLAYER_AGE'], y = target_median['sum'])\n    axs[i, 1].set(ylabel = f\"{target} \"+ \"SUM\")\n    i = i + 1\n    plt.suptitle(\"Player Age vs Target Median/Sum\", y = 1.03)\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-07-13T16:35:14.316632Z","iopub.execute_input":"2021-07-13T16:35:14.317008Z","iopub.status.idle":"2021-07-13T16:35:19.796377Z","shell.execute_reply.started":"2021-07-13T16:35:14.316974Z","shell.execute_reply":"2021-07-13T16:35:19.795664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Players making more noise\")\nfor name in players_target[players_target['PLAYER_AGE'] >= 45]['playerName'].unique():\n    print(name)","metadata":{"execution":{"iopub.status.busy":"2021-07-13T16:31:58.49853Z","iopub.execute_input":"2021-07-13T16:31:58.498894Z","iopub.status.idle":"2021-07-13T16:31:58.512953Z","shell.execute_reply.started":"2021-07-13T16:31:58.498865Z","shell.execute_reply":"2021-07-13T16:31:58.511858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Other than those players above, rest of the age groups does not seem interesting, becase the differences in medians are not significant by visual inspection, and sums have similar distribution of distribution of age.","metadata":{}}]}