{"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 numpy as np\n\nfrom datetime import timedelta\nfrom tensorflow.keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2021-06-27T13:03:48.920877Z","iopub.execute_input":"2021-06-27T13:03:48.921331Z","iopub.status.idle":"2021-06-27T13:03:50.896349Z","shell.execute_reply.started":"2021-06-27T13:03:48.921236Z","shell.execute_reply":"2021-06-27T13:03:50.895408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_engagement = pd.read_csv('../input/mlb-forecasting-ann/player_engagement.csv')\nplayer_engagement = player_engagement.drop(columns=['engagementMetricsDate', 'year'])\nplayer_engagement['date'] = pd.to_datetime(player_engagement['date'])\nplayer_engagement.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-27T13:03:50.897888Z","iopub.execute_input":"2021-06-27T13:03:50.898364Z","iopub.status.idle":"2021-06-27T13:03:54.287071Z","shell.execute_reply.started":"2021-06-27T13:03:50.898318Z","shell.execute_reply":"2021-06-27T13:03:54.285802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_df = pd.read_csv('../input/mlb-forecasting-ann/player_engagement_mean_yearly.csv')\nmean_df['year'] = mean_df['year'].astype('period[A-DEC]')\nmean_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-27T13:03:54.289676Z","iopub.execute_input":"2021-06-27T13:03:54.290145Z","iopub.status.idle":"2021-06-27T13:03:54.484864Z","shell.execute_reply.started":"2021-06-27T13:03:54.290095Z","shell.execute_reply":"2021-06-27T13:03:54.483703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lag = 17\n\nn_fold = 5\n\ndef prediction(df):\n    df = df.reset_index()\n    df['date'] = pd.to_datetime(df['date'], format='%Y%m%d')\n    df['playerId'] = df['date_playerId'].apply(lambda x: x.split('_')[1]).astype(int)\n    df['year'] = df['date'].dt.to_period('Y')\n    \n    for x in range(lag):\n        df['date'] = df['date'] - timedelta(days=1)\n        df = df.merge(player_engagement, how='left', on=['date', 'playerId'], suffixes=['',f'_{x+1}'])\n        df = df.fillna(0.)\n        \n    df = df.merge(mean_df, how='left', on=['playerId', 'year'])\n    df = df.fillna(0.)\n    df = df.drop(columns=['date', 'playerId', 'year'])\n    \n    feature_columns = [x for x in df.columns[5:]]\n\n    target_columns = [x for x in df.columns[1:5]]\n    \n    pred = np.zeros(df[target_columns].shape)\n    for x in range(n_fold):\n        model = load_model(f'../input/mlb-forecasting-ann/best_model_fold{x+1}.h5')\n        \n        pred += model.predict(df[feature_columns].to_numpy())\n    \n    pred = pred / n_fold\n    \n    return pred","metadata":{"execution":{"iopub.status.busy":"2021-06-27T13:03:54.486880Z","iopub.execute_input":"2021-06-27T13:03:54.487365Z","iopub.status.idle":"2021-06-27T13:03:54.499773Z","shell.execute_reply.started":"2021-06-27T13:03:54.487313Z","shell.execute_reply":"2021-06-27T13:03:54.498525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import mlb\n\nenv = mlb.make_env() # initialize the environment\niter_test = env.iter_test() # iterator which loops over each date in test set\n\ntarget_columns = ['target1', 'target2', 'target3', 'target4']\n\nfor (test_df, sample_prediction_df) in iter_test:\n    targets = prediction(sample_prediction_df)\n    sample_prediction_df[target_columns] = np.clip(targets, 0, 100)\n    sample_prediction_df = sample_prediction_df.fillna(0.)\n    env.predict(sample_prediction_df)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-27T13:03:54.501500Z","iopub.execute_input":"2021-06-27T13:03:54.502002Z","iopub.status.idle":"2021-06-27T13:04:37.331146Z","shell.execute_reply.started":"2021-06-27T13:03:54.501957Z","shell.execute_reply":"2021-06-27T13:04:37.330074Z"},"trusted":true},"execution_count":null,"outputs":[]}]}