{"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 data and API for forecasting\nStarting to get the data\nFrom the documentation and exemple<br>\nhttps://www.kaggle.com/chumajin/eda-of-mlb-for-starter-english-ver<br>\nhttps://www.kaggle.com/ryanholbrook/getting-started-with-mlb-player-digital-engagement<br>\n-I build 2 pickles files from the train.csv file","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom joblib import Parallel, delayed\nfrom pathlib import Path\nfrom datetime import datetime, timedelta\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import mean_squared_error,mean_absolute_error\n\nimport pickle\n\nfrom IPython.display import Image\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:48.432268Z","iopub.execute_input":"2021-07-29T08:53:48.432729Z","iopub.status.idle":"2021-07-29T08:53:48.440021Z","shell.execute_reply.started":"2021-07-29T08:53:48.432684Z","shell.execute_reply":"2021-07-29T08:53:48.43858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nImage(\"../input/pictures/johnny_automatic_old_time_pitcher.jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:48.577469Z","iopub.execute_input":"2021-07-29T08:53:48.577842Z","iopub.status.idle":"2021-07-29T08:53:48.589316Z","shell.execute_reply.started":"2021-07-29T08:53:48.577812Z","shell.execute_reply":"2021-07-29T08:53:48.588213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"../input/pickle-data/df_nextDayPlayer.pkl\", 'rb') as handle:\n    df_nextDayPlayer = pickle.load(handle)\n    \nwith open(\"../input/pickle-data/playerBoxScores.pkl\", 'rb') as handle:\n    df_playerBoxScores = pickle.load(handle)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:48.75861Z","iopub.execute_input":"2021-07-29T08:53:48.759079Z","iopub.status.idle":"2021-07-29T08:53:49.826701Z","shell.execute_reply.started":"2021-07-29T08:53:48.759046Z","shell.execute_reply":"2021-07-29T08:53:49.8256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_playerBoxScores.head(n=2)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:49.828131Z","iopub.execute_input":"2021-07-29T08:53:49.828435Z","iopub.status.idle":"2021-07-29T08:53:49.859597Z","shell.execute_reply.started":"2021-07-29T08:53:49.828409Z","shell.execute_reply":"2021-07-29T08:53:49.858379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_nextDayPlayer.head(n=2)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:49.862258Z","iopub.execute_input":"2021-07-29T08:53:49.862728Z","iopub.status.idle":"2021-07-29T08:53:49.87761Z","shell.execute_reply.started":"2021-07-29T08:53:49.86268Z","shell.execute_reply":"2021-07-29T08:53:49.876456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_playerBoxScores.shape)\nprint((df_nextDayPlayer.shape))","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:49.879398Z","iopub.execute_input":"2021-07-29T08:53:49.87975Z","iopub.status.idle":"2021-07-29T08:53:49.889885Z","shell.execute_reply.started":"2021-07-29T08:53:49.879705Z","shell.execute_reply":"2021-07-29T08:53:49.888996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#--------------------------prepare data sets-------------------------------\ndf_nextDayPlayer.nunique()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:49.891058Z","iopub.execute_input":"2021-07-29T08:53:49.891417Z","iopub.status.idle":"2021-07-29T08:53:50.784555Z","shell.execute_reply.started":"2021-07-29T08:53:49.891379Z","shell.execute_reply":"2021-07-29T08:53:50.783485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Player Box Scores","metadata":{}},{"cell_type":"code","source":"df_playerBoxScores.info()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:50.786102Z","iopub.execute_input":"2021-07-29T08:53:50.786544Z","iopub.status.idle":"2021-07-29T08:53:50.970341Z","shell.execute_reply.started":"2021-07-29T08:53:50.786499Z","shell.execute_reply":"2021-07-29T08:53:50.969017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = [\"target1\", \"target2\", \"target3\", \"target4\"]\nfeatures = [\n    \"hits\",\n    'doubles',\n    'triples',\n    \"strikeOuts\",\n    \"homeRuns\",\n    'atBats',\n    \"runsScored\",\n    \"stolenBases\",\n    'caughtStealing',\n    \"strikeOutsPitching\",\n    \"inningsPitched\",\n    \"strikes\",\n    \"flyOuts\",\n    \"groundOuts\",\n    \"errors\",\n    \"chances\",\n    'TotalBase',\n    'BattingAvg',\n    \n]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:50.972038Z","iopub.execute_input":"2021-07-29T08:53:50.972523Z","iopub.status.idle":"2021-07-29T08:53:50.978454Z","shell.execute_reply.started":"2021-07-29T08:53:50.972467Z","shell.execute_reply":"2021-07-29T08:53:50.977634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#----------------------adding calcul columns----------------------------\n#Adding Total bats columns\ndf_playerBoxScores['TotalBase']=df_playerBoxScores['hits']+(2*df_playerBoxScores['doubles'])+(3*df_playerBoxScores['triples'])+(4*df_playerBoxScores['homeRuns'])\n#Adding Batting Average\ndf_playerBoxScores['BattingAvg']=df_playerBoxScores['hits']/df_playerBoxScores['atBats']","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:50.98107Z","iopub.execute_input":"2021-07-29T08:53:50.981433Z","iopub.status.idle":"2021-07-29T08:53:51.001663Z","shell.execute_reply.started":"2021-07-29T08:53:50.981401Z","shell.execute_reply":"2021-07-29T08:53:51.000154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#----------prepare data player Box scores----------------------------------\ndf_playerBoxScores.rename(columns={\"gameDate\": \"date\"}, inplace=True)\ndf_playerBoxScores = df_playerBoxScores[['date', 'playerId'] + features]\n# Set dtypes\ndf_playerBoxScores = df_playerBoxScores.astype({name: np.float32 for name in features})\ndf_playerBoxScores = df_playerBoxScores.astype({'playerId': str})\ndf_playerBoxScores.groupby(['date', 'playerId'], as_index=False).sum()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:51.003526Z","iopub.execute_input":"2021-07-29T08:53:51.003829Z","iopub.status.idle":"2021-07-29T08:53:51.664202Z","shell.execute_reply.started":"2021-07-29T08:53:51.003801Z","shell.execute_reply":"2021-07-29T08:53:51.663115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# exploring data with some player Id","metadata":{}},{"cell_type":"code","source":"#Data seasonalite----------------------------------------------------------------\nseason_Global_2020=df_playerBoxScores[df_playerBoxScores.date.between('2018-01-02', '2020-12-31')]\nseason_Global_2020=season_Global_2020.sort_values(by='BattingAvg', ascending=False)\nseason_Global_2020=season_Global_2020.sort_values(by='date')\nseason_Global_2020.groupby(['playerId', 'date'], axis=0).sum()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:51.665635Z","iopub.execute_input":"2021-07-29T08:53:51.665942Z","iopub.status.idle":"2021-07-29T08:53:52.314959Z","shell.execute_reply.started":"2021-07-29T08:53:51.665913Z","shell.execute_reply":"2021-07-29T08:53:52.31356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"season_Global_2020_02=season_Global_2020[season_Global_2020['playerId']==\"543105\"]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:52.316722Z","iopub.execute_input":"2021-07-29T08:53:52.317082Z","iopub.status.idle":"2021-07-29T08:53:52.351608Z","shell.execute_reply.started":"2021-07-29T08:53:52.317049Z","shell.execute_reply":"2021-07-29T08:53:52.35034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ploting some performances\nfig, ax = plt.subplots(figsize=(24,7))\nax.plot(season_Global_2020_02.date,season_Global_2020_02.BattingAvg, label='BattingAvg', fillstyle=\"full\", linewidth=2 )\nax.plot(season_Global_2020_02.date,season_Global_2020_02.homeRuns, label='homeRuns', fillstyle=\"full\", linewidth=2 )\nplt.xlabel(\"date\", labelpad=10)\nplt.title('playerId 543105')\n#plt.xticks(rotation=45)\nplt.xticks([])\nplt.legend(frameon=False)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:52.35447Z","iopub.execute_input":"2021-07-29T08:53:52.354833Z","iopub.status.idle":"2021-07-29T08:53:52.57712Z","shell.execute_reply.started":"2021-07-29T08:53:52.3548Z","shell.execute_reply":"2021-07-29T08:53:52.576091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ploting some performances\nfig, ax = plt.subplots(figsize=(24,7))\nax.plot(season_Global_2020_02.date,season_Global_2020_02.runsScored,label='runsScored', fillstyle=\"full\", linewidth=2 )\nplt.xlabel(\"date\", labelpad=10)\nplt.title('playerId 543105')\n#plt.xticks(rotation=45)\nplt.xticks([])\nplt.legend(frameon=False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:52.578586Z","iopub.execute_input":"2021-07-29T08:53:52.578916Z","iopub.status.idle":"2021-07-29T08:53:52.760192Z","shell.execute_reply.started":"2021-07-29T08:53:52.578886Z","shell.execute_reply":"2021-07-29T08:53:52.759027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p>Next step is to prepare next day player to build a global data frame</p>\n","metadata":{}},{"cell_type":"code","source":"Image(\"../input/pictures/johnny_automatic_baseball_at_bat.jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:52.761676Z","iopub.execute_input":"2021-07-29T08:53:52.761995Z","iopub.status.idle":"2021-07-29T08:53:52.772754Z","shell.execute_reply.started":"2021-07-29T08:53:52.761967Z","shell.execute_reply":"2021-07-29T08:53:52.771736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Next Day Player","metadata":{}},{"cell_type":"code","source":"#---------------Prepare next Day Player data------------------------------------------------------------------------------------\ndf_nextDayPlayer.rename(columns={\"engagementMetricsDate\": \"date\"}, inplace=True)\ndf_nextDayPlayer = df_nextDayPlayer.astype({name: np.float32 for name in targets})\ndf_nextDayPlayer = df_nextDayPlayer.astype({'playerId': str})\ndf_nextDayPlayer=df_nextDayPlayer.groupby(['date', 'playerId'], as_index=False).sum()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:52.774242Z","iopub.execute_input":"2021-07-29T08:53:52.77459Z","iopub.status.idle":"2021-07-29T08:53:58.23687Z","shell.execute_reply.started":"2021-07-29T08:53:52.77456Z","shell.execute_reply":"2021-07-29T08:53:58.235813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_nextDayPlayer.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:58.238437Z","iopub.execute_input":"2021-07-29T08:53:58.238868Z","iopub.status.idle":"2021-07-29T08:53:58.25382Z","shell.execute_reply.started":"2021-07-29T08:53:58.238824Z","shell.execute_reply":"2021-07-29T08:53:58.252646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#----------------------------------------------Data seasonalite----------------------------------------------------------------\nseason_nextdayPlayer_2020=df_nextDayPlayer[df_nextDayPlayer.date.between('2019-01-01', '2021-12-31')]\nseason_nextdayPlayer_2020=season_nextdayPlayer_2020.sort_values(by='target1', ascending=False)\nseason_nextdayPlayer_2020=season_nextdayPlayer_2020.sort_values(by='date')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:53:58.255739Z","iopub.execute_input":"2021-07-29T08:53:58.25624Z","iopub.status.idle":"2021-07-29T08:54:02.653696Z","shell.execute_reply.started":"2021-07-29T08:53:58.256164Z","shell.execute_reply":"2021-07-29T08:54:02.652564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"season_nextdayPlayer_02=season_nextdayPlayer_2020[(season_nextdayPlayer_2020['playerId']==\"543105\")]\nseason_nextdayPlayer_03=season_nextdayPlayer_2020[(season_nextdayPlayer_2020['playerId']==\"282332\")]\nseason_nextdayPlayer_04=season_nextdayPlayer_2020[(season_nextdayPlayer_2020['playerId']==\"516969\")]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:02.655044Z","iopub.execute_input":"2021-07-29T08:54:02.655338Z","iopub.status.idle":"2021-07-29T08:54:03.47939Z","shell.execute_reply.started":"2021-07-29T08:54:02.65531Z","shell.execute_reply":"2021-07-29T08:54:03.478412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Understand the correlation between targets with differents player Id","metadata":{}},{"cell_type":"code","source":"#Heatmap1 player Id : 543105\nplt.figure(figsize=(17,11))\n#heatmap1\ncorr = season_nextdayPlayer_02.corr()\ncmap = sns.diverging_palette(230, 20, as_cmap=True)\nplt.subplot(1, 3, 1)\nsns.heatmap(corr, center=0,annot=True,\n            square=True, linewidths=.5, cbar_kws={\"shrink\": .5})\nplt.title('Heatmap player Id : 543105')\n#heatmap2 player Id : 282332\ncorr02 = season_nextdayPlayer_04.corr()\nplt.subplot(1, 3, 2)\ncmap = sns.diverging_palette(230, 20, as_cmap=True)\nsns.heatmap(corr02, center=0,annot=True,\n            square=True, linewidths=.5, cbar_kws={\"shrink\": .5})\nplt.title('Heatmap player Id : 282332')\n#heatmap3 player Id : 516969\ncorr03 = season_nextdayPlayer_03.corr()\nplt.subplot(1, 3, 3)\ncmap = sns.diverging_palette(230, 20, as_cmap=True)\nsns.heatmap(corr03, center=0,annot=True,\n            square=True, linewidths=.5, cbar_kws={\"shrink\": .5})\nplt.title('Heatmap player Id : 516969')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:03.480891Z","iopub.execute_input":"2021-07-29T08:54:03.48132Z","iopub.status.idle":"2021-07-29T08:54:04.781435Z","shell.execute_reply.started":"2021-07-29T08:54:03.481278Z","shell.execute_reply":"2021-07-29T08:54:04.780103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfig, ax = plt.subplots(figsize=(24,7))\nax.plot(season_nextdayPlayer_02.date,season_nextdayPlayer_02.target1, label='target1', fillstyle=\"full\", linewidth=2 )\nax.plot(season_nextdayPlayer_02.date,season_nextdayPlayer_02.target2, label='target2', fillstyle=\"full\", linewidth=2 )\nax.plot(season_nextdayPlayer_02.date,season_nextdayPlayer_02.target3, label='target3', fillstyle=\"full\", linewidth=2 )\nax.plot(season_nextdayPlayer_02.date,season_nextdayPlayer_02.target4, label='target4', fillstyle=\"full\", linewidth=2 )\nplt.xlabel(\"date\", labelpad=10)\nplt.title('playerId 543105 and targets')\nplt.xticks([])\nplt.legend(frameon=False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:04.782881Z","iopub.execute_input":"2021-07-29T08:54:04.78331Z","iopub.status.idle":"2021-07-29T08:54:05.431104Z","shell.execute_reply.started":"2021-07-29T08:54:04.783264Z","shell.execute_reply":"2021-07-29T08:54:05.43002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"season_nextdayPlayer_02[\"target1\"][-30:].plot(figsize=(12,4),label='target1', fillstyle=\"full\", linewidth=2);\nseason_nextdayPlayer_02[\"target2\"][-30:].plot(figsize=(12,4),label='target2', fillstyle=\"full\", linewidth=2);\nseason_nextdayPlayer_02[\"target3\"][-30:].plot(figsize=(12,4),label='target3', fillstyle=\"full\", linewidth=2);\nseason_nextdayPlayer_02[\"target4\"][-30:].plot(figsize=(12,4),label='target4', fillstyle=\"full\", linewidth=2);\nplt.title('playerId 543105 and targets')\nplt.legend(frameon=False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:05.432339Z","iopub.execute_input":"2021-07-29T08:54:05.432663Z","iopub.status.idle":"2021-07-29T08:54:05.730707Z","shell.execute_reply.started":"2021-07-29T08:54:05.432633Z","shell.execute_reply":"2021-07-29T08:54:05.729554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p>Making some graph to understand the way he seasonality can be different for player or season</p>","metadata":{}},{"cell_type":"code","source":"from statsmodels.tsa.seasonal import STL\n#function for trend seasonal and resid plot\ndef add_stl_plot(fig, res, legend):\n    axs = fig.get_axes()\n    comps = ['trend', 'seasonal', 'resid']\n    for ax, comp in zip(axs[1:], comps):\n        series = getattr(res, comp)\n        if comp == 'resid':\n            ax.plot(series, marker='o', linestyle='none')\n        else:\n            ax.plot(series)\n            if comp == 'trend':\n                ax.legend(legend, frameon=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:05.732108Z","iopub.execute_input":"2021-07-29T08:54:05.732574Z","iopub.status.idle":"2021-07-29T08:54:05.741246Z","shell.execute_reply.started":"2021-07-29T08:54:05.732538Z","shell.execute_reply":"2021-07-29T08:54:05.73967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pandas.plotting import register_matplotlib_converters\nregister_matplotlib_converters()\nsns.set_style('darkgrid')\nplt.rc('figure',figsize=(16,12))\nplt.rc('font',size=13)\nplt.figure(figsize=(17,11))\nstl = STL(season_nextdayPlayer_02.target1, period=7, robust=True)\nres_robust = stl.fit()\nfig = res_robust.plot()\nres_non_robust = STL(season_nextdayPlayer_02.target1, period=7, robust=False).fit()\nadd_stl_plot(fig, res_non_robust, ['Robust','Non-robust'])\nplt.title('playerId 543105 seasonal trend and resid')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:05.747539Z","iopub.execute_input":"2021-07-29T08:54:05.74795Z","iopub.status.idle":"2021-07-29T08:54:06.901939Z","shell.execute_reply.started":"2021-07-29T08:54:05.747911Z","shell.execute_reply":"2021-07-29T08:54:06.900926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from statsmodels.graphics.tsaplots import plot_acf, plot_pacf\nfig = plt.figure(figsize=(12,8))\nax1 = fig.add_subplot(211)\nfig = plot_acf(season_nextdayPlayer_02[\"target2\"][-395:], lags=40, ax=ax1)\nax2 = fig.add_subplot(212)\nfig = plot_pacf(season_nextdayPlayer_02[\"target2\"][-395:], lags=40, ax=ax2);","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:06.904023Z","iopub.execute_input":"2021-07-29T08:54:06.90435Z","iopub.status.idle":"2021-07-29T08:54:07.51808Z","shell.execute_reply.started":"2021-07-29T08:54:06.904317Z","shell.execute_reply":"2021-07-29T08:54:07.516574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Using Regular season start and end date\nseason_OnedayPlayer_2020=df_nextDayPlayer[df_nextDayPlayer.date.between('2021-02-28', '2021-10-31')]\n#exeample for one player \nseason_OnedayPlayer_2020=season_OnedayPlayer_2020[(season_OnedayPlayer_2020['playerId']==\"282332\")]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:07.51974Z","iopub.execute_input":"2021-07-29T08:54:07.520194Z","iopub.status.idle":"2021-07-29T08:54:08.221123Z","shell.execute_reply.started":"2021-07-29T08:54:07.520148Z","shell.execute_reply":"2021-07-29T08:54:08.219748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"season_OnedayPlayer_2020.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:08.22286Z","iopub.execute_input":"2021-07-29T08:54:08.223322Z","iopub.status.idle":"2021-07-29T08:54:08.233253Z","shell.execute_reply.started":"2021-07-29T08:54:08.223275Z","shell.execute_reply":"2021-07-29T08:54:08.232054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"series = np.log(season_OnedayPlayer_2020[\"target2\"])\nseries01 = np.log(season_OnedayPlayer_2020[\"target1\"])\nseries03 = np.log(season_OnedayPlayer_2020[\"target3\"])\nseries04 = np.log(season_OnedayPlayer_2020[\"target4\"])\n\nplt.figure(figsize=(12,8))\nplt.plot(series, label=\"Series target 2\")\nplt.plot(series01, label=\"Series target 1\")\nplt.plot(series03, label=\"Series target 3\")\nplt.plot(series04, label=\"Series target 4\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:08.234975Z","iopub.execute_input":"2021-07-29T08:54:08.235436Z","iopub.status.idle":"2021-07-29T08:54:08.530629Z","shell.execute_reply.started":"2021-07-29T08:54:08.235389Z","shell.execute_reply":"2021-07-29T08:54:08.529567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SARIMA for one player for season 2020","metadata":{}},{"cell_type":"code","source":"from statsmodels.tsa.statespace.sarimax import SARIMAX\n\nsize = int(len(season_OnedayPlayer_2020['target2'].dropna()) * 0.75)\ntrain, test = season_OnedayPlayer_2020['target2'].dropna()[0:size], season_OnedayPlayer_2020['target2'].dropna()[size:len(season_nextdayPlayer_02[\"target2\"].dropna())]\ntest = test.reset_index()['target2']\nhistory = [x for x in train]\npredictions = list()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:08.532141Z","iopub.execute_input":"2021-07-29T08:54:08.532492Z","iopub.status.idle":"2021-07-29T08:54:08.542541Z","shell.execute_reply.started":"2021-07-29T08:54:08.53246Z","shell.execute_reply":"2021-07-29T08:54:08.54157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:08.543777Z","iopub.execute_input":"2021-07-29T08:54:08.544197Z","iopub.status.idle":"2021-07-29T08:54:08.554604Z","shell.execute_reply.started":"2021-07-29T08:54:08.544152Z","shell.execute_reply":"2021-07-29T08:54:08.553615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for t in range(len(test)):\n    model = SARIMAX(history, order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))\n    model_fit = model.fit(disp=False)\n    output = model_fit.forecast()\n    yhat = output[0]\n    predictions.append(yhat)\n    obs = test[t]\n    history.append(yhat)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:08.556043Z","iopub.execute_input":"2021-07-29T08:54:08.5565Z","iopub.status.idle":"2021-07-29T08:54:15.843593Z","shell.execute_reply.started":"2021-07-29T08:54:08.556456Z","shell.execute_reply":"2021-07-29T08:54:15.842596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = [x for x in train]\n\nplt.figure(figsize=(12,8))\nplt.plot(np.concatenate([history, predictions]), label='Prediction')\nplt.plot(np.concatenate([history, test]), label='Test')\nplt.title('prediction for palyer Id 282332 and target2')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:15.845215Z","iopub.execute_input":"2021-07-29T08:54:15.845676Z","iopub.status.idle":"2021-07-29T08:54:16.09695Z","shell.execute_reply.started":"2021-07-29T08:54:15.845633Z","shell.execute_reply":"2021-07-29T08:54:16.096142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_squared_error(predictions, test)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:16.097875Z","iopub.execute_input":"2021-07-29T08:54:16.098137Z","iopub.status.idle":"2021-07-29T08:54:16.105862Z","shell.execute_reply.started":"2021-07-29T08:54:16.098112Z","shell.execute_reply":"2021-07-29T08:54:16.104755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# building a model with LSTM\n# Using sequence of targets ","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split \nfrom sklearn.preprocessing import StandardScaler \nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.feature_selection import SelectFromModel\nfrom sklearn import metrics \nfrom keras.layers import LSTM\nfrom keras.layers import Dropout\nfrom keras.callbacks import EarlyStopping\nfrom statsmodels.tsa.seasonal import STL\n\nfrom math import sqrt\n\nimport tensorflow as tf\nimport keras\nfrom keras import layers\nfrom keras import Model\nfrom keras.layers import Dense\nfrom keras.models import Sequential\nfrom keras.layers import Input, Dense, concatenate","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:16.107239Z","iopub.execute_input":"2021-07-29T08:54:16.107537Z","iopub.status.idle":"2021-07-29T08:54:16.119014Z","shell.execute_reply.started":"2021-07-29T08:54:16.10751Z","shell.execute_reply":"2021-07-29T08:54:16.118179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_nextDayPlayer['date'] = pd.to_datetime(df_nextDayPlayer['date'], format=\"%Y-%m-%d\")\ndf_nextDayPlayer = df_nextDayPlayer.set_index('date').to_period('D')\nprint(df_nextDayPlayer.info())","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:16.12016Z","iopub.execute_input":"2021-07-29T08:54:16.120485Z","iopub.status.idle":"2021-07-29T08:54:16.957906Z","shell.execute_reply.started":"2021-07-29T08:54:16.120457Z","shell.execute_reply":"2021-07-29T08:54:16.956699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Starting to make some preprocessing to make a train data\n## and scaling values for supervised learning","metadata":{}},{"cell_type":"code","source":"# convert series to supervised learning\nfrom pandas import concat\nfrom pandas import DataFrame\ndef series_to_supervised(data, n_in=1, n_out=1, dropnan=True):\n    n_vars = 1 if type(data) is list else data.shape[1]\n    df = DataFrame(data)\n    cols, names = list(), list()\n    # input sequence (t-n, ... t-1)\n    for i in range(n_in, 0, -1):\n        cols.append(df.shift(i))\n        names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]\n    # forecast sequence (t, t+1, ... t+n)\n    for i in range(0, n_out):\n        cols.append(df.shift(-i))\n        if i == 0:\n            names += [('var%d(t)' % (j+1)) for j in range(n_vars)]\n        else:\n            names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]\n    # put it all together\n    agg = concat(cols, axis=1)\n    agg.columns = names\n    # drop rows with NaN values\n    if dropnan:\n        agg.dropna(inplace=True)\n    return agg\n ","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:16.95914Z","iopub.execute_input":"2021-07-29T08:54:16.959433Z","iopub.status.idle":"2021-07-29T08:54:16.969785Z","shell.execute_reply.started":"2021-07-29T08:54:16.959406Z","shell.execute_reply":"2021-07-29T08:54:16.968562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_nextDayPlayer['playerId'].nunique()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:16.971239Z","iopub.execute_input":"2021-07-29T08:54:16.971582Z","iopub.status.idle":"2021-07-29T08:54:17.424002Z","shell.execute_reply.started":"2021-07-29T08:54:16.971551Z","shell.execute_reply":"2021-07-29T08:54:17.422953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols=['target1', 'target2', 'target3', 'target4']\n#using scaling for sampling the trainning set  \ndf_train=df_nextDayPlayer[cols].values\nscaler = MinMaxScaler(feature_range=(0, 1))\nscaled = scaler.fit_transform(df_train)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:17.425374Z","iopub.execute_input":"2021-07-29T08:54:17.425663Z","iopub.status.idle":"2021-07-29T08:54:17.499912Z","shell.execute_reply.started":"2021-07-29T08:54:17.425635Z","shell.execute_reply":"2021-07-29T08:54:17.498823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# frame as supervised learning\nreframed = series_to_supervised(scaled, 1, 1)\n# drop columns we don't want to predict\nreframed.drop(reframed.columns[[2,3,4,5]], axis=1, inplace=True)\nprint(reframed.head())","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:17.501202Z","iopub.execute_input":"2021-07-29T08:54:17.501518Z","iopub.status.idle":"2021-07-29T08:54:17.75379Z","shell.execute_reply.started":"2021-07-29T08:54:17.501489Z","shell.execute_reply":"2021-07-29T08:54:17.75261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values = reframed.values\nseq_time = 365*2061\ntrain = values[:seq_time, :]\ntest = values[seq_time:, :]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:17.755133Z","iopub.execute_input":"2021-07-29T08:54:17.755451Z","iopub.status.idle":"2021-07-29T08:54:17.760167Z","shell.execute_reply.started":"2021-07-29T08:54:17.755421Z","shell.execute_reply":"2021-07-29T08:54:17.759074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split into input and outputs\nX_train, y_train = train[:, :-1], train[:, -1]\nX_test, y_test = test[:, :-1], test[:, -1]\n# reshape input to be [samples, timesteps, features]\nX_train = X_train.reshape((X_train.shape[0], 1, X_train.shape[1]))\nX_test = X_test.reshape((X_test.shape[0], 1, X_test.shape[1]))\nprint(X_train.shape, y_train.shape, X_test.shape, y_test.shape) ","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:17.761583Z","iopub.execute_input":"2021-07-29T08:54:17.761939Z","iopub.status.idle":"2021-07-29T08:54:17.776925Z","shell.execute_reply.started":"2021-07-29T08:54:17.761908Z","shell.execute_reply":"2021-07-29T08:54:17.77584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Using LSTM model with inputs and outputs","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(LSTM(100,input_shape=(X_train.shape[1], X_train.shape[2]),activation=\"relu\", bias_initializer='zeros', return_sequences=True))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(50, activation='relu',bias_initializer='zeros',return_sequences=True))\nmodel.add(LSTM(10))\nmodel.add(Dense(32, kernel_regularizer=tf.keras.regularizers.l2(0.01)))\nmodel.add(Dense(1, activation='sigmoid'))\nmodel.compile( optimizer='adam', loss='mean_squared_error', metrics='mae')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:17.778223Z","iopub.execute_input":"2021-07-29T08:54:17.778524Z","iopub.status.idle":"2021-07-29T08:54:18.195912Z","shell.execute_reply.started":"2021-07-29T08:54:17.778497Z","shell.execute_reply":"2021-07-29T08:54:18.194735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:54:18.197316Z","iopub.execute_input":"2021-07-29T08:54:18.197678Z","iopub.status.idle":"2021-07-29T09:25:16.00681Z","shell.execute_reply.started":"2021-07-29T08:54:18.197639Z","shell.execute_reply":"2021-07-29T09:25:16.005483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Test prediction and inverse","metadata":{"execution":{"iopub.status.busy":"2021-07-21T15:13:33.681351Z","iopub.execute_input":"2021-07-21T15:13:33.68189Z","iopub.status.idle":"2021-07-21T15:13:33.687322Z","shell.execute_reply.started":"2021-07-21T15:13:33.681848Z","shell.execute_reply":"2021-07-21T15:13:33.685897Z"}}},{"cell_type":"code","source":"# plot history\nplt.figure(figsize=(12,8))\nplt.plot(history.history['loss'], label='train')\nplt.plot(history.history['val_loss'], label='test')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:25:16.010561Z","iopub.execute_input":"2021-07-29T09:25:16.011065Z","iopub.status.idle":"2021-07-29T09:25:16.272053Z","shell.execute_reply.started":"2021-07-29T09:25:16.011004Z","shell.execute_reply":"2021-07-29T09:25:16.270834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluation of the model\nloss, mean_squared_error= model.evaluate(X_test, y_test, verbose=0)\nprint('loss is:', loss)\nprint('mean squared error is:', mean_squared_error)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:25:16.273715Z","iopub.execute_input":"2021-07-29T09:25:16.274132Z","iopub.status.idle":"2021-07-29T09:26:32.83577Z","shell.execute_reply.started":"2021-07-29T09:25:16.274086Z","shell.execute_reply":"2021-07-29T09:26:32.83472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#save model \nmodel.save(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:26:32.837004Z","iopub.execute_input":"2021-07-29T09:26:32.837291Z","iopub.status.idle":"2021-07-29T09:26:32.871665Z","shell.execute_reply.started":"2021-07-29T09:26:32.837262Z","shell.execute_reply":"2021-07-29T09:26:32.870847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict=model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:26:32.872709Z","iopub.execute_input":"2021-07-29T09:26:32.873109Z","iopub.status.idle":"2021-07-29T09:27:45.31213Z","shell.execute_reply.started":"2021-07-29T09:26:32.873079Z","shell.execute_reply":"2021-07-29T09:27:45.310336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_X = X_test.reshape((X_test.shape[0], X_test.shape[2]))\n# invert scaling for forecast\ninv_yhat = np.concatenate((test_predict, test_X[:, -4:]), axis=1)\ninv_yhat = scaler.inverse_transform(inv_yhat)\ninv_yhat = inv_yhat[:,0]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:27:45.313972Z","iopub.execute_input":"2021-07-29T09:27:45.314268Z","iopub.status.idle":"2021-07-29T09:27:45.350917Z","shell.execute_reply.started":"2021-07-29T09:27:45.314239Z","shell.execute_reply":"2021-07-29T09:27:45.34992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# invert scaling for actual\ny_test = y_test.reshape((len(y_test), 1))\ninv_y = np.concatenate((y_test, test_X[:, -4:]), axis=1)\ninv_y = scaler.inverse_transform(inv_y)\ninv_y = inv_y[:,0]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:27:45.351988Z","iopub.execute_input":"2021-07-29T09:27:45.352258Z","iopub.status.idle":"2021-07-29T09:27:45.398781Z","shell.execute_reply.started":"2021-07-29T09:27:45.352231Z","shell.execute_reply":"2021-07-29T09:27:45.397571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,8))\naa=[x for x in range(365)]\nplt.plot(aa, inv_y[:365], marker='.', label=\"actual\")\nplt.plot(aa, inv_yhat[:365], 'r', label=\"prediction\")\nplt.ylabel('player activity', size=15)\nplt.xlabel('Time step', size=15)\nplt.legend(fontsize=15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:27:45.399946Z","iopub.execute_input":"2021-07-29T09:27:45.400229Z","iopub.status.idle":"2021-07-29T09:27:45.653082Z","shell.execute_reply.started":"2021-07-29T09:27:45.4002Z","shell.execute_reply":"2021-07-29T09:27:45.652012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### to make the submission i use the example of notebook\nhttps://www.kaggle.com/ruriarmandhani/mlb-submission<br>\nhttps://www.kaggle.com/ulrich07/mlb-debug-ann\n","metadata":{}},{"cell_type":"code","source":"#-----------------------------Next day Player--------------------------------------\nwith open(\"../input/pickle-data/df_nextDayPlayer.pkl\", 'rb') as handle:\n    df_nextDayPlayer = pickle.load(handle)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:27:45.654324Z","iopub.execute_input":"2021-07-29T09:27:45.654622Z","iopub.status.idle":"2021-07-29T09:27:46.698537Z","shell.execute_reply.started":"2021-07-29T09:27:45.654595Z","shell.execute_reply":"2021-07-29T09:27:46.69746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_nextDayPlayer.rename(columns={\"engagementMetricsDate\": \"date\"}, inplace=True)\ndf_nextDayPlayer['date'] = pd.to_datetime(df_nextDayPlayer['date'], format=\"%Y-%m-%d\")\ndf_nextDayPlayer= df_nextDayPlayer.reset_index()\n#df_nextDayPlayer = df_nextDayPlayer.set_index('date').to_period('D')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:27:46.699984Z","iopub.execute_input":"2021-07-29T09:27:46.700438Z","iopub.status.idle":"2021-07-29T09:27:47.477693Z","shell.execute_reply.started":"2021-07-29T09:27:46.700393Z","shell.execute_reply":"2021-07-29T09:27:47.476659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Sample data on mlb env","metadata":{}},{"cell_type":"code","source":"import mlb","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:27:47.479752Z","iopub.execute_input":"2021-07-29T09:27:47.480138Z","iopub.status.idle":"2021-07-29T09:27:47.485055Z","shell.execute_reply.started":"2021-07-29T09:27:47.480096Z","shell.execute_reply":"2021-07-29T09:27:47.483883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"env = mlb.make_env() # initialize the environment\niter_test = env.iter_test() # iterator which loops over each date in test set","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:27:47.486309Z","iopub.execute_input":"2021-07-29T09:27:47.486638Z","iopub.status.idle":"2021-07-29T09:27:47.544813Z","shell.execute_reply.started":"2021-07-29T09:27:47.486609Z","shell.execute_reply":"2021-07-29T09:27:47.542977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (test_df, sample_prediction_df) in iter_test:\n    \n   \n    #------sample_prediction-----\n    sample_prediction_df= sample_prediction_df.reset_index()\n    sample_prediction_df['date'] = pd.to_datetime(sample_prediction_df['date'], format='%Y%m%d')\n    sample_prediction_df['playerId'] = sample_prediction_df['date_playerId'].apply(lambda x: x.split('_')[1]).astype(int)\n    sample_prediction_df[targets]=df_nextDayPlayer[targets]\n    sample_prediction_df = sample_prediction_df.fillna(0.)\n    \n     #----sample data---------------------------\n    df_train=sample_prediction_df[targets].values\n    scaler = MinMaxScaler(feature_range=(0, 1))\n    scaled = scaler.fit_transform(df_train)\n    seq_time = 365*4\n    test = scaled[:seq_time, :]\n    train = scaled[seq_time:, :]\n    X_train, y_train = train[:, :-1], train[:, -1]\n    X_test, y_test = test[:, :-1], test[:, -1]\n    X_train = X_train.reshape((X_train.shape[0], 1, X_train.shape[1]))\n    X_test = X_test.reshape((X_test.shape[0], 1, X_test.shape[1]))\n    \n    \n    #----make predict----\n    model = keras.models.load_model(\"../input/modelplace/model .h5\")\n    pred= model.predict(X_test)\n    \n    \n    #----sample for submission-----\n    X_test = X_test.reshape((X_test.shape[0], X_test.shape[2]))\n    pred = np.concatenate((pred, X_test[:, -4:]), axis=1)\n    \n    sample_prediction_df=sample_prediction_df.drop(columns=['date','playerId'])\n    sample_prediction_df[targets] = np.clip(pred, 0, 100)\n    env.predict(sample_prediction_df)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:27:47.545816Z","iopub.status.idle":"2021-07-29T09:27:47.546299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_prediction_df","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:27:47.547439Z","iopub.status.idle":"2021-07-29T09:27:47.547904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}