{"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 pandas as pd\nimport os\nimport gc\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-26T21:21:15.963970Z","iopub.execute_input":"2021-06-26T21:21:15.964531Z","iopub.status.idle":"2021-06-26T21:21:15.972600Z","shell.execute_reply.started":"2021-06-26T21:21:15.964447Z","shell.execute_reply":"2021-06-26T21:21:15.971631Z"},"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-26T21:21:17.430935Z","iopub.execute_input":"2021-06-26T21:21:17.431266Z","iopub.status.idle":"2021-06-26T21:21:17.435072Z","shell.execute_reply.started":"2021-06-26T21:21:17.431237Z","shell.execute_reply":"2021-06-26T21:21:17.434181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## COMPUTING PLAYER MEAN","metadata":{}},{"cell_type":"code","source":"tgt_df = pd.read_csv(\"../input/mlb-data/target.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-06-26T21:21:18.113953Z","iopub.execute_input":"2021-06-26T21:21:18.114304Z","iopub.status.idle":"2021-06-26T21:21:21.895872Z","shell.execute_reply.started":"2021-06-26T21:21:18.114269Z","shell.execute_reply":"2021-06-26T21:21:21.894928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tgt_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T21:21:21.897216Z","iopub.execute_input":"2021-06-26T21:21:21.897547Z","iopub.status.idle":"2021-06-26T21:21:21.930694Z","shell.execute_reply.started":"2021-06-26T21:21:21.897514Z","shell.execute_reply":"2021-06-26T21:21:21.929728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_mean = tgt_df.groupby(\"playerId\")[[\"target1\",\"target2\",\"target3\",\"target4\"]].median().reset_index()\ngc.collect()\nprint(player_mean.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T21:21:23.186207Z","iopub.execute_input":"2021-06-26T21:21:23.186557Z","iopub.status.idle":"2021-06-26T21:21:23.698518Z","shell.execute_reply.started":"2021-06-26T21:21:23.186527Z","shell.execute_reply":"2021-06-26T21:21:23.697536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_mean.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T21:21:26.337312Z","iopub.execute_input":"2021-06-26T21:21:26.337712Z","iopub.status.idle":"2021-06-26T21:21:26.350779Z","shell.execute_reply.started":"2021-06-26T21:21:26.337676Z","shell.execute_reply":"2021-06-26T21:21:26.349712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TGTCOLS = [\"target1\",\"target2\",\"target3\",\"target4\"]\nimp = tgt_df[TGTCOLS].median()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T21:21:27.246242Z","iopub.execute_input":"2021-06-26T21:21:27.246624Z","iopub.status.idle":"2021-06-26T21:21:27.423649Z","shell.execute_reply.started":"2021-06-26T21:21:27.246591Z","shell.execute_reply":"2021-06-26T21:21:27.422639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_pred(df):\n    df = df.reset_index()\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\",\"date\"], axis=1, inplace=True)\n    #df = df.fillna(0.)\n    for col in TGTCOLS:\n        df[col] = df[col].fillna(imp[col])\n    return df\n#===================","metadata":{"execution":{"iopub.status.busy":"2021-06-26T21:22:31.412058Z","iopub.execute_input":"2021-06-26T21:22:31.412657Z","iopub.status.idle":"2021-06-26T21:22:31.419228Z","shell.execute_reply.started":"2021-06-26T21:22:31.412605Z","shell.execute_reply":"2021-06-26T21:22:31.418242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import mlb\nenv = mlb.make_env() # initialize the environment\niter_test = env.iter_test() # iterator which loops over each date in test set\n\nfor (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-26T21:21:29.860635Z","iopub.execute_input":"2021-06-26T21:21:29.861124Z","iopub.status.idle":"2021-06-26T21:21:30.620684Z","shell.execute_reply.started":"2021-06-26T21:21:29.861092Z","shell.execute_reply":"2021-06-26T21:21:30.618923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_prediction_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T21:22:57.077362Z","iopub.execute_input":"2021-06-26T21:22:57.077698Z","iopub.status.idle":"2021-06-26T21:22:57.090967Z","shell.execute_reply.started":"2021-06-26T21:22:57.077667Z","shell.execute_reply":"2021-06-26T21:22:57.089895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_prediction_df[\"target1\"] = sample_prediction_df[\"target1\"].fillna(imp[\"target1\"])","metadata":{"execution":{"iopub.status.busy":"2021-06-26T21:23:40.220195Z","iopub.execute_input":"2021-06-26T21:23:40.220554Z","iopub.status.idle":"2021-06-26T21:23:40.225712Z","shell.execute_reply.started":"2021-06-26T21:23:40.220524Z","shell.execute_reply":"2021-06-26T21:23:40.224701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}