{"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":"Credit to @ulrich07 - I simply took his notebook and filtered out all but 2021 player targets and then averaged for each player.\n\nHere is his noteboook that I forked.\n\nhttps://www.kaggle.com/ulrich07/baseline-model-player-mean-or-median","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport gc\nfrom tqdm import tqdm\n\nimport mlb\nenv = mlb.make_env() # initialize the environment\niter_test = env.iter_test() # iterator which loops over each date in test set","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-15T12:20:39.807694Z","iopub.execute_input":"2021-06-15T12:20:39.808432Z","iopub.status.idle":"2021-06-15T12:20:39.837317Z","shell.execute_reply.started":"2021-06-15T12:20:39.808300Z","shell.execute_reply":"2021-06-15T12:20:39.836264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR = \"../input/mlb-player-digital-engagement-forecasting\"","metadata":{"execution":{"iopub.status.busy":"2021-06-15T12:20:39.839079Z","iopub.execute_input":"2021-06-15T12:20:39.839723Z","iopub.status.idle":"2021-06-15T12:20:39.844645Z","shell.execute_reply.started":"2021-06-15T12:20:39.839671Z","shell.execute_reply":"2021-06-15T12:20:39.843219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr = pd.read_csv(f\"{ROOT_DIR}/train.csv\")\nprint(tr.shape)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-06-15T12:20:39.846169Z","iopub.execute_input":"2021-06-15T12:20:39.846485Z","iopub.status.idle":"2021-06-15T12:21:53.254760Z","shell.execute_reply.started":"2021-06-15T12:20:39.846454Z","shell.execute_reply":"2021-06-15T12:21:53.254059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## COMPUTING PLAYER MEAN","metadata":{}},{"cell_type":"code","source":"N_DATES = tr.shape[0]\nd = []\nfor idx in tqdm(range(N_DATES)):\n    u = eval(tr.iloc[idx, 1])\n    d += u\n#================","metadata":{"execution":{"iopub.status.busy":"2021-06-15T12:21:53.255989Z","iopub.execute_input":"2021-06-15T12:21:53.256469Z","iopub.status.idle":"2021-06-15T12:22:55.265171Z","shell.execute_reply.started":"2021-06-15T12:21:53.256436Z","shell.execute_reply":"2021-06-15T12:22:55.264006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tgt_df = pd.DataFrame(d)\nprint(tgt_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-15T12:22:55.266780Z","iopub.execute_input":"2021-06-15T12:22:55.267083Z","iopub.status.idle":"2021-06-15T12:23:00.836222Z","shell.execute_reply.started":"2021-06-15T12:22:55.267054Z","shell.execute_reply":"2021-06-15T12:23:00.834231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tgt_df['year'] = pd.DatetimeIndex(tgt_df['engagementMetricsDate']).year\ntgt_df['month'] = pd.DatetimeIndex(tgt_df['engagementMetricsDate']).month","metadata":{"execution":{"iopub.status.busy":"2021-06-15T12:23:00.839670Z","iopub.execute_input":"2021-06-15T12:23:00.840124Z","iopub.status.idle":"2021-06-15T12:23:02.685552Z","shell.execute_reply.started":"2021-06-15T12:23:00.840075Z","shell.execute_reply":"2021-06-15T12:23:02.684487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tgt_df","metadata":{"execution":{"iopub.status.busy":"2021-06-15T12:23:02.687604Z","iopub.execute_input":"2021-06-15T12:23:02.688065Z","iopub.status.idle":"2021-06-15T12:23:02.721422Z","shell.execute_reply.started":"2021-06-15T12:23:02.688015Z","shell.execute_reply":"2021-06-15T12:23:02.720301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df = tgt_df[tgt_df['year'] == 2021]\nnew_df = new_df[tgt_df['month'] >= 4]\nnew_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-15T12:44:26.770888Z","iopub.execute_input":"2021-06-15T12:44:26.771292Z","iopub.status.idle":"2021-06-15T12:44:26.815204Z","shell.execute_reply.started":"2021-06-15T12:44:26.771248Z","shell.execute_reply":"2021-06-15T12:44:26.813893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### group by year?\nplayer_avg = new_df.groupby([\"playerId\"])[[\"target1\",\"target2\",\"target3\",\"target4\"]].mean().reset_index()\nplayer_median = new_df.groupby([\"playerId\"])[[\"target1\",\"target2\",\"target3\",\"target4\"]].median().reset_index()\nplayer_mean=pd.concat([player_avg, player_median], axis=1).groupby(axis=1, level=0).mean()\nplayer_mean[\"target1\"] = .85 * player_mean[\"target1\"]\nplayer_mean[\"target2\"] = .85 * player_mean[\"target2\"]\nplayer_mean[\"target3\"] = .85 * player_mean[\"target3\"]\nplayer_mean[\"target4\"] = .85 * player_mean[\"target4\"]\ngc.collect()\nprint(player_mean.shape)\nplayer_mean","metadata":{"execution":{"iopub.status.busy":"2021-06-15T13:22:13.506843Z","iopub.execute_input":"2021-06-15T13:22:13.507409Z","iopub.status.idle":"2021-06-15T13:22:13.715393Z","shell.execute_reply.started":"2021-06-15T13:22:13.507366Z","shell.execute_reply":"2021-06-15T13:22:13.714237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_pred(df):\n    df[\"playerId\"] = df[\"date_playerId\"].apply(lambda x: int( x.split(\"_\")[1] ) )\n    df.drop([\"target1\",\"target2\",\"target3\",\"target4\"], axis=1, inplace=True)\n    df = df.merge(player_mean, on=\"playerId\", how=\"left\")\n    df.drop(\"playerId\", axis=1, inplace=True)\n    df = df.fillna(0.)\n    return df\n#===================","metadata":{"execution":{"iopub.status.busy":"2021-06-15T12:24:28.608795Z","iopub.execute_input":"2021-06-15T12:24:28.609201Z","iopub.status.idle":"2021-06-15T12:24:28.614977Z","shell.execute_reply.started":"2021-06-15T12:24:28.609151Z","shell.execute_reply":"2021-06-15T12:24:28.614260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (test_df, sample_prediction_df) in iter_test:\n    sample_prediction_df = process_pred(sample_prediction_df)\n    env.predict(sample_prediction_df)","metadata":{"execution":{"iopub.status.busy":"2021-06-15T13:04:20.460590Z","iopub.execute_input":"2021-06-15T13:04:20.460943Z","iopub.status.idle":"2021-06-15T13:04:20.465911Z","shell.execute_reply.started":"2021-06-15T13:04:20.460912Z","shell.execute_reply":"2021-06-15T13:04:20.464510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_prediction_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-15T13:04:22.382492Z","iopub.execute_input":"2021-06-15T13:04:22.382898Z","iopub.status.idle":"2021-06-15T13:04:22.397796Z","shell.execute_reply.started":"2021-06-15T13:04:22.382863Z","shell.execute_reply":"2021-06-15T13:04:22.396647Z"},"trusted":true},"execution_count":null,"outputs":[]}]}