{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\n\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-30T11:39:40.090225Z","iopub.execute_input":"2021-06-30T11:39:40.090601Z","iopub.status.idle":"2021-06-30T11:39:40.102699Z","shell.execute_reply.started":"2021-06-30T11:39:40.090569Z","shell.execute_reply":"2021-06-30T11:39:40.101264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir = '/kaggle/input/mlb-player-digital-engagement-forecasting'","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:39:40.104635Z","iopub.execute_input":"2021-06-30T11:39:40.105032Z","iopub.status.idle":"2021-06-30T11:39:40.113563Z","shell.execute_reply.started":"2021-06-30T11:39:40.104992Z","shell.execute_reply":"2021-06-30T11:39:40.112237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/mlb-player-digital-engagement-forecasting/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:39:40.115073Z","iopub.execute_input":"2021-06-30T11:39:40.115515Z","iopub.status.idle":"2021-06-30T11:40:28.179853Z","shell.execute_reply.started":"2021-06-30T11:39:40.115481Z","shell.execute_reply":"2021-06-30T11:40:28.177624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.tail()","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:40:28.181786Z","iopub.execute_input":"2021-06-30T11:40:28.182296Z","iopub.status.idle":"2021-06-30T11:40:28.375977Z","shell.execute_reply.started":"2021-06-30T11:40:28.182261Z","shell.execute_reply":"2021-06-30T11:40:28.375125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/chumajin/eda-of-mlb-for-starter-english-ver\n# Helper function to unpack json found in daily data\ndef unpack_json(json_str):\n    return np.nan if pd.isna(json_str) else pd.read_json(json_str)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:40:28.378836Z","iopub.execute_input":"2021-06-30T11:40:28.379328Z","iopub.status.idle":"2021-06-30T11:40:28.383477Z","shell.execute_reply.started":"2021-06-30T11:40:28.379296Z","shell.execute_reply":"2021-06-30T11:40:28.382573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**1. targets을 선수별로 나눠보기**\n\n1. 'nextDayPlayerEngagement'의 json 형식을 분해\n2.  분해한 것들을 playerId를 기준으로 다시 분해\n3.  playerId가 같은 것들을 취합\n4. \n ","metadata":{}},{"cell_type":"code","source":"# 각 dataFrame(targets_n)의 index를 playerId로 변환하는 함수\n\ndef playerId_to_index(dataFrame):\n    dataFrame.index = dataFrame['playerId']\n    dataFrame.drop(['playerId'], axis = 1, inplace = True)\n    return dataFrame","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:40:28.386299Z","iopub.execute_input":"2021-06-30T11:40:28.386924Z","iopub.status.idle":"2021-06-30T11:40:28.399459Z","shell.execute_reply.started":"2021-06-30T11:40:28.386876Z","shell.execute_reply":"2021-06-30T11:40:28.398235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 날짜 별 targets_n 을 만들어 내는 함수\ndef make_targets_df(train_df, start, end):\n    for i in range(start,end):\n        dataFrame = unpack_json(train_df['nextDayPlayerEngagement'].iloc[i])\n        dataFrame = playerId_to_index(dataFrame)\n        if i == start :\n            targets_df = dataFrame\n        else :\n            targets_df = pd.merge(targets_df, dataFrame, how='outer',on='playerId')\n        if i%5 == 0:\n            print(i)\n    return targets_df","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:40:28.401066Z","iopub.execute_input":"2021-06-30T11:40:28.401626Z","iopub.status.idle":"2021-06-30T11:40:28.410345Z","shell.execute_reply.started":"2021-06-30T11:40:28.401569Z","shell.execute_reply":"2021-06-30T11:40:28.409519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_0_100 = make_targets_df(train_df,0,100)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:40:28.411763Z","iopub.execute_input":"2021-06-30T11:40:28.412263Z","iopub.status.idle":"2021-06-30T11:41:12.886991Z","shell.execute_reply.started":"2021-06-30T11:40:28.412198Z","shell.execute_reply":"2021-06-30T11:41:12.885958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_0_100.to_csv('targets_df_0_100.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:41:12.888503Z","iopub.execute_input":"2021-06-30T11:41:12.888844Z","iopub.status.idle":"2021-06-30T11:41:14.484268Z","shell.execute_reply.started":"2021-06-30T11:41:12.888809Z","shell.execute_reply":"2021-06-30T11:41:14.483254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_100_200 = make_targets_df(train_df,100, 200)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:41:14.485679Z","iopub.execute_input":"2021-06-30T11:41:14.486043Z","iopub.status.idle":"2021-06-30T11:41:59.647207Z","shell.execute_reply.started":"2021-06-30T11:41:14.486008Z","shell.execute_reply":"2021-06-30T11:41:59.645800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_100_200.to_csv('targets_df_100_200.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:41:59.648970Z","iopub.execute_input":"2021-06-30T11:41:59.649339Z","iopub.status.idle":"2021-06-30T11:42:01.296110Z","shell.execute_reply.started":"2021-06-30T11:41:59.649296Z","shell.execute_reply":"2021-06-30T11:42:01.295041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_200_300 = make_targets_df(train_df, 200, 300)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:42:01.297613Z","iopub.execute_input":"2021-06-30T11:42:01.297960Z","iopub.status.idle":"2021-06-30T11:42:45.767800Z","shell.execute_reply.started":"2021-06-30T11:42:01.297925Z","shell.execute_reply":"2021-06-30T11:42:45.766322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_200_300.to_csv('targets_df_200_300.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:42:45.769767Z","iopub.execute_input":"2021-06-30T11:42:45.770271Z","iopub.status.idle":"2021-06-30T11:42:47.448039Z","shell.execute_reply.started":"2021-06-30T11:42:45.770220Z","shell.execute_reply":"2021-06-30T11:42:47.447139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_300_400 = make_targets_df(train_df, 300, 400)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:42:47.449453Z","iopub.execute_input":"2021-06-30T11:42:47.450105Z","iopub.status.idle":"2021-06-30T11:43:31.216640Z","shell.execute_reply.started":"2021-06-30T11:42:47.450055Z","shell.execute_reply":"2021-06-30T11:43:31.215407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_300_400.to_csv('targets_df_300_400.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:43:31.218154Z","iopub.execute_input":"2021-06-30T11:43:31.218472Z","iopub.status.idle":"2021-06-30T11:43:32.862546Z","shell.execute_reply.started":"2021-06-30T11:43:31.218441Z","shell.execute_reply":"2021-06-30T11:43:32.860980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_400_500 = make_targets_df(train_df, 400, 500)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:43:32.864384Z","iopub.execute_input":"2021-06-30T11:43:32.864707Z","iopub.status.idle":"2021-06-30T11:44:16.469090Z","shell.execute_reply.started":"2021-06-30T11:43:32.864678Z","shell.execute_reply":"2021-06-30T11:44:16.468178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_400_500.to_csv('targets_df_400_500.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:44:16.470457Z","iopub.execute_input":"2021-06-30T11:44:16.471106Z","iopub.status.idle":"2021-06-30T11:44:18.130583Z","shell.execute_reply.started":"2021-06-30T11:44:16.471059Z","shell.execute_reply":"2021-06-30T11:44:18.129516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_500_600 = make_targets_df(train_df, 500, 600)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:44:18.132298Z","iopub.execute_input":"2021-06-30T11:44:18.132621Z","iopub.status.idle":"2021-06-30T11:45:00.583784Z","shell.execute_reply.started":"2021-06-30T11:44:18.132584Z","shell.execute_reply":"2021-06-30T11:45:00.582415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_500_600.to_csv('targets_df_500_600.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:45:00.585247Z","iopub.execute_input":"2021-06-30T11:45:00.585554Z","iopub.status.idle":"2021-06-30T11:45:02.278020Z","shell.execute_reply.started":"2021-06-30T11:45:00.585525Z","shell.execute_reply":"2021-06-30T11:45:02.276416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_600_700 = make_targets_df(train_df, 600, 700)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:45:02.279465Z","iopub.execute_input":"2021-06-30T11:45:02.279811Z","iopub.status.idle":"2021-06-30T11:45:45.167221Z","shell.execute_reply.started":"2021-06-30T11:45:02.279780Z","shell.execute_reply":"2021-06-30T11:45:45.166169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_600_700.to_csv('targets_df_600_700.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:45:45.171206Z","iopub.execute_input":"2021-06-30T11:45:45.171552Z","iopub.status.idle":"2021-06-30T11:45:46.801642Z","shell.execute_reply.started":"2021-06-30T11:45:45.171521Z","shell.execute_reply":"2021-06-30T11:45:46.800482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_700_800 = make_targets_df(train_df, 700, 800)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:45:46.803177Z","iopub.execute_input":"2021-06-30T11:45:46.803515Z","iopub.status.idle":"2021-06-30T11:46:37.701583Z","shell.execute_reply.started":"2021-06-30T11:45:46.803474Z","shell.execute_reply":"2021-06-30T11:46:37.700555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_700_800.to_csv('targets_df_700_800.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:46:37.703514Z","iopub.execute_input":"2021-06-30T11:46:37.703852Z","iopub.status.idle":"2021-06-30T11:46:39.843278Z","shell.execute_reply.started":"2021-06-30T11:46:37.703820Z","shell.execute_reply":"2021-06-30T11:46:39.841416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_800_900 = make_targets_df(train_df, 800, 900)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:46:39.845025Z","iopub.execute_input":"2021-06-30T11:46:39.845363Z","iopub.status.idle":"2021-06-30T11:47:25.603382Z","shell.execute_reply.started":"2021-06-30T11:46:39.845331Z","shell.execute_reply":"2021-06-30T11:47:25.602233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_800_900.to_csv('targets_df_800_900.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:47:25.605516Z","iopub.execute_input":"2021-06-30T11:47:25.605911Z","iopub.status.idle":"2021-06-30T11:47:27.254981Z","shell.execute_reply.started":"2021-06-30T11:47:25.605876Z","shell.execute_reply":"2021-06-30T11:47:27.253776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_900_1000 = make_targets_df(train_df, 900, 1000)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:47:27.256578Z","iopub.execute_input":"2021-06-30T11:47:27.256922Z","iopub.status.idle":"2021-06-30T11:48:10.883632Z","shell.execute_reply.started":"2021-06-30T11:47:27.256858Z","shell.execute_reply":"2021-06-30T11:48:10.882799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_900_1000.to_csv('targets_df_900_1000.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:48:10.884770Z","iopub.execute_input":"2021-06-30T11:48:10.885222Z","iopub.status.idle":"2021-06-30T11:48:12.560718Z","shell.execute_reply.started":"2021-06-30T11:48:10.885189Z","shell.execute_reply":"2021-06-30T11:48:12.559883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_1000_1100 = make_targets_df(train_df, 1000, 1100)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:48:12.561903Z","iopub.execute_input":"2021-06-30T11:48:12.562333Z","iopub.status.idle":"2021-06-30T11:48:56.229532Z","shell.execute_reply.started":"2021-06-30T11:48:12.562301Z","shell.execute_reply":"2021-06-30T11:48:56.228342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_1000_1100.to_csv('targets_df_1000_1100.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:48:56.231016Z","iopub.execute_input":"2021-06-30T11:48:56.231349Z","iopub.status.idle":"2021-06-30T11:48:57.888089Z","shell.execute_reply.started":"2021-06-30T11:48:56.231318Z","shell.execute_reply":"2021-06-30T11:48:57.887060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_1100_1200 = make_targets_df(train_df, 1100, 1200)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:48:57.889751Z","iopub.execute_input":"2021-06-30T11:48:57.890110Z","iopub.status.idle":"2021-06-30T11:49:40.211943Z","shell.execute_reply.started":"2021-06-30T11:48:57.890076Z","shell.execute_reply":"2021-06-30T11:49:40.210196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_1100_1200.to_csv('targets_df_1100_1200.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:49:40.214039Z","iopub.execute_input":"2021-06-30T11:49:40.214486Z","iopub.status.idle":"2021-06-30T11:49:41.929629Z","shell.execute_reply.started":"2021-06-30T11:49:40.214438Z","shell.execute_reply":"2021-06-30T11:49:41.928329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_1200_1216 = make_targets_df(train_df, 1200, 1216)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:49:41.934245Z","iopub.execute_input":"2021-06-30T11:49:41.934734Z","iopub.status.idle":"2021-06-30T11:49:42.532958Z","shell.execute_reply.started":"2021-06-30T11:49:41.934699Z","shell.execute_reply":"2021-06-30T11:49:42.531810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_1200_1216.to_csv('targets_df_1200_1216.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:49:42.536317Z","iopub.execute_input":"2021-06-30T11:49:42.536708Z","iopub.status.idle":"2021-06-30T11:49:42.816208Z","shell.execute_reply.started":"2021-06-30T11:49:42.536669Z","shell.execute_reply":"2021-06-30T11:49:42.815201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = pd.read_csv('./targets_df_1200_1216.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:49:42.818698Z","iopub.execute_input":"2021-06-30T11:49:42.819063Z","iopub.status.idle":"2021-06-30T11:49:42.869348Z","shell.execute_reply.started":"2021-06-30T11:49:42.819017Z","shell.execute_reply":"2021-06-30T11:49:42.868094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:49:42.871953Z","iopub.execute_input":"2021-06-30T11:49:42.872494Z","iopub.status.idle":"2021-06-30T11:49:42.908069Z","shell.execute_reply.started":"2021-06-30T11:49:42.872446Z","shell.execute_reply":"2021-06-30T11:49:42.907083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = pd.read_csv('./targets_df_1100_1200.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:49:42.910115Z","iopub.execute_input":"2021-06-30T11:49:42.910723Z","iopub.status.idle":"2021-06-30T11:49:43.229385Z","shell.execute_reply.started":"2021-06-30T11:49:42.910681Z","shell.execute_reply":"2021-06-30T11:49:43.228450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-30T11:49:43.230761Z","iopub.execute_input":"2021-06-30T11:49:43.231124Z","iopub.status.idle":"2021-06-30T11:49:43.264987Z","shell.execute_reply.started":"2021-06-30T11:49:43.231084Z","shell.execute_reply":"2021-06-30T11:49:43.264138Z"},"trusted":true},"execution_count":null,"outputs":[]}]}