{"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\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-07-03T07:04:20.226924Z","iopub.execute_input":"2021-07-03T07:04:20.227644Z","iopub.status.idle":"2021-07-03T07:04:20.243239Z","shell.execute_reply.started":"2021-07-03T07:04:20.227587Z","shell.execute_reply":"2021-07-03T07:04:20.241612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_targets = '/kaggle/input/playerid-and-targets/results/targets_df_'\ntrain_df = pd.read_csv('/kaggle/input/mlb-player-digital-engagement-forecasting/train.csv')\ndef unpack_json(json_str):\n    return np.nan if pd.isna(json_str) else pd.read_json(json_str)\ntrain_df.tail()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:04:20.288556Z","iopub.execute_input":"2021-07-03T07:04:20.288934Z","iopub.status.idle":"2021-07-03T07:05:46.288187Z","shell.execute_reply.started":"2021-07-03T07:04:20.288884Z","shell.execute_reply":"2021-07-03T07:05:46.286546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_1211 = unpack_json(train_df['nextDayPlayerEngagement'].iloc[1211])\nplayerBoxScores = unpack_json(train_df['playerBoxScores'].iloc[1211])","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.290765Z","iopub.execute_input":"2021-07-03T07:05:46.291197Z","iopub.status.idle":"2021-07-03T07:05:46.397400Z","shell.execute_reply.started":"2021-07-03T07:05:46.291148Z","shell.execute_reply":"2021-07-03T07:05:46.396152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_1211","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.399898Z","iopub.execute_input":"2021-07-03T07:05:46.400254Z","iopub.status.idle":"2021-07-03T07:05:46.420916Z","shell.execute_reply.started":"2021-07-03T07:05:46.400223Z","shell.execute_reply":"2021-07-03T07:05:46.419605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerBoxScores","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.422841Z","iopub.execute_input":"2021-07-03T07:05:46.423517Z","iopub.status.idle":"2021-07-03T07:05:46.482213Z","shell.execute_reply.started":"2021-07-03T07:05:46.423471Z","shell.execute_reply":"2021-07-03T07:05:46.481031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerBoxScores.info()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.485035Z","iopub.execute_input":"2021-07-03T07:05:46.485429Z","iopub.status.idle":"2021-07-03T07:05:46.518836Z","shell.execute_reply.started":"2021-07-03T07:05:46.485392Z","shell.execute_reply":"2021-07-03T07:05:46.518053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"object형 전부 코드가 있으므로 버려도 될듯, 날짜는 우선 버리기, playerId를 기준으로 취합\n\n\nobject : gameDate, gameTimeUTC, teamName, playerName, positionName, positionType","metadata":{}},{"cell_type":"code","source":"playerScore_target = pd.merge(targets_1211,playerBoxScores, how='outer',on='playerId')","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.520002Z","iopub.execute_input":"2021-07-03T07:05:46.520407Z","iopub.status.idle":"2021-07-03T07:05:46.545356Z","shell.execute_reply.started":"2021-07-03T07:05:46.520375Z","shell.execute_reply":"2021-07-03T07:05:46.544315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('max_rows', 10)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.546622Z","iopub.execute_input":"2021-07-03T07:05:46.546931Z","iopub.status.idle":"2021-07-03T07:05:46.554397Z","shell.execute_reply.started":"2021-07-03T07:05:46.546887Z","shell.execute_reply":"2021-07-03T07:05:46.553126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.556026Z","iopub.execute_input":"2021-07-03T07:05:46.556351Z","iopub.status.idle":"2021-07-03T07:05:46.618577Z","shell.execute_reply.started":"2021-07-03T07:05:46.556320Z","shell.execute_reply":"2021-07-03T07:05:46.617209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"게임 날짜가 null인 것은 없으므로 'gameDate'가 NaN이면 그 행 삭제하기","metadata":{}},{"cell_type":"code","source":"playerScore_target = playerScore_target.dropna(subset=['gameDate'])","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.621247Z","iopub.execute_input":"2021-07-03T07:05:46.621583Z","iopub.status.idle":"2021-07-03T07:05:46.633398Z","shell.execute_reply.started":"2021-07-03T07:05:46.621551Z","shell.execute_reply":"2021-07-03T07:05:46.631753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"object형 제거","metadata":{}},{"cell_type":"code","source":"playerScore_target.drop(['gameDate', 'gameTimeUTC', 'teamName', 'playerName', 'positionName', 'positionType'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.637695Z","iopub.execute_input":"2021-07-03T07:05:46.638083Z","iopub.status.idle":"2021-07-03T07:05:46.651407Z","shell.execute_reply.started":"2021-07-03T07:05:46.638030Z","shell.execute_reply":"2021-07-03T07:05:46.650032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.653388Z","iopub.execute_input":"2021-07-03T07:05:46.653938Z","iopub.status.idle":"2021-07-03T07:05:46.708664Z","shell.execute_reply.started":"2021-07-03T07:05:46.653870Z","shell.execute_reply":"2021-07-03T07:05:46.707460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target = playerScore_target.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.710254Z","iopub.execute_input":"2021-07-03T07:05:46.710564Z","iopub.status.idle":"2021-07-03T07:05:46.715475Z","shell.execute_reply.started":"2021-07-03T07:05:46.710533Z","shell.execute_reply":"2021-07-03T07:05:46.714508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('max_columns', None)\nplayerScore_target","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:09:04.096569Z","iopub.execute_input":"2021-07-03T07:09:04.097144Z","iopub.status.idle":"2021-07-03T07:09:04.256303Z","shell.execute_reply.started":"2021-07-03T07:09:04.097090Z","shell.execute_reply":"2021-07-03T07:09:04.255275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target.info()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.778303Z","iopub.execute_input":"2021-07-03T07:05:46.778722Z","iopub.status.idle":"2021-07-03T07:05:46.803939Z","shell.execute_reply.started":"2021-07-03T07:05:46.778678Z","shell.execute_reply":"2021-07-03T07:05:46.803041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target_corr = playerScore_target.corr()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.805128Z","iopub.execute_input":"2021-07-03T07:05:46.805588Z","iopub.status.idle":"2021-07-03T07:05:46.816489Z","shell.execute_reply.started":"2021-07-03T07:05:46.805552Z","shell.execute_reply":"2021-07-03T07:05:46.815621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in ['groundIntoTriplePlay','pickoffs','pickoffsPitching']:\n    print(playerScore_target[i].value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:11:52.694458Z","iopub.execute_input":"2021-07-03T07:11:52.694812Z","iopub.status.idle":"2021-07-03T07:11:52.705548Z","shell.execute_reply.started":"2021-07-03T07:11:52.694782Z","shell.execute_reply":"2021-07-03T07:11:52.703986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target_corr['target1'].sort_values(ascending = False)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:05:46.817641Z","iopub.execute_input":"2021-07-03T07:05:46.818118Z","iopub.status.idle":"2021-07-03T07:05:46.828684Z","shell.execute_reply.started":"2021-07-03T07:05:46.818081Z","shell.execute_reply":"2021-07-03T07:05:46.827675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"rbi(득점타)가 target1에 어느정도 영향을 미치는 것 같음, \n1211 index에서 target1과 target2간의 연관도가 높게 측정됨 => 날짜에 따라서 각 target값들간의 연관도가 달라지는 것이 아닌가 하는 의심이 듬\n\n\ntarget값과 관련된 NaN이 3개 있음 => 해결할 필요가 있어보임  => 모든 값이 0이어서 그럼 어쩔수 없다고 느껴짐","metadata":{}},{"cell_type":"code","source":"'''\n한개의 corr_matrix를 만들기\n\ntargets_1211 = unpack_json(train_df['nextDayPlayerEngagement'].iloc[1211])\nplayerBoxScores = unpack_json(train_df['playerBoxScores'].iloc[1211])\nplayerScore_target = pd.merge(targets_1211,playerBoxScores, how='outer',on='playerId')\nplayerScore_target = playerScore_target.dropna(subset=['gameDate'])\nplayerScore_target.drop(['gameDate', 'gameTimeUTC', 'teamName', 'playerName', 'positionName', 'positionType'], axis = 1, inplace = True)\nplayerScore_target = playerScore_target.fillna(0)\nplayerScore_target_corr = playerScore_target.corr()\n\n\n'''\n","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:16:07.978059Z","iopub.execute_input":"2021-07-03T07:16:07.978531Z","iopub.status.idle":"2021-07-03T07:16:08.078837Z","shell.execute_reply.started":"2021-07-03T07:16:07.978493Z","shell.execute_reply":"2021-07-03T07:16:08.077637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# train_df속 playerBoxScores가 NaN인 값 걸러내기\ntrain_df = train_df.dropna(subset=['playerBoxScores'])","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:45:38.057371Z","iopub.execute_input":"2021-07-03T07:45:38.057797Z","iopub.status.idle":"2021-07-03T07:45:38.072514Z","shell.execute_reply.started":"2021-07-03T07:45:38.057758Z","shell.execute_reply":"2021-07-03T07:45:38.071109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:45:57.941404Z","iopub.execute_input":"2021-07-03T07:45:57.941809Z","iopub.status.idle":"2021-07-03T07:45:57.960560Z","shell.execute_reply.started":"2021-07-03T07:45:57.941776Z","shell.execute_reply":"2021-07-03T07:45:57.959156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=train_df.reset_index()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:56:51.669840Z","iopub.execute_input":"2021-07-03T07:56:51.670220Z","iopub.status.idle":"2021-07-03T07:56:51.677360Z","shell.execute_reply.started":"2021-07-03T07:56:51.670188Z","shell.execute_reply":"2021-07-03T07:56:51.676231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop(['index'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:56:53.539952Z","iopub.execute_input":"2021-07-03T07:56:53.540414Z","iopub.status.idle":"2021-07-03T07:56:53.549629Z","shell.execute_reply.started":"2021-07-03T07:56:53.540367Z","shell.execute_reply":"2021-07-03T07:56:53.548141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:56:58.696763Z","iopub.execute_input":"2021-07-03T07:56:58.697147Z","iopub.status.idle":"2021-07-03T07:56:59.093298Z","shell.execute_reply.started":"2021-07-03T07:56:58.697115Z","shell.execute_reply":"2021-07-03T07:56:59.092007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df 전처리 먼저하기\ntrain_df = pd.read_csv('/kaggle/input/mlb-player-digital-engagement-forecasting/train.csv')\ntrain_df = train_df.dropna(subset=['playerBoxScores'])\ntrain_df=train_df.reset_index()\ntrain_df.drop(['index'], axis = 1, inplace = True)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-03T07:59:07.804457Z","iopub.execute_input":"2021-07-03T07:59:07.804872Z","iopub.status.idle":"2021-07-03T08:00:22.496991Z","shell.execute_reply.started":"2021-07-03T07:59:07.804836Z","shell.execute_reply":"2021-07-03T08:00:22.496054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-07-03T08:02:01.225036Z","iopub.execute_input":"2021-07-03T08:02:01.225601Z","iopub.status.idle":"2021-07-03T08:02:01.567354Z","shell.execute_reply.started":"2021-07-03T08:02:01.225554Z","shell.execute_reply":"2021-07-03T08:02:01.566042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef playerBoxScores_targets_corr(train_df, start, last):\n    plus = lambda a, b : a + b\n    for i in range(start, last):\n        targets = unpack_json(train_df['nextDayPlayerEngagement'].iloc[i])\n        playerBoxScores = unpack_json(train_df['playerBoxScores'].iloc[i])\n        playerScore_targets = pd.merge(targets, playerBoxScores, how='outer',on='playerId')\n        playerScore_targets = playerScore_targets.dropna(subset=['gameDate'])\n        playerScore_targets.drop(['gameDate', 'gameTimeUTC', 'teamName', 'playerName', 'positionName', 'positionType'], axis = 1, inplace = True)\n        playerScore_targets = playerScore_targets.fillna(0)\n        playerScore_target_corr = playerScore_targets.corr()\n        if i == start:\n            p_t_c = playerScore_target_corr\n        else:\n            p_t_c = p_t_c.combine(playerScore_target_corr, plus)\n        if i%50 == 0:\n            print(i)\n    return p_t_c\n ","metadata":{"execution":{"iopub.status.busy":"2021-07-03T08:08:45.857062Z","iopub.execute_input":"2021-07-03T08:08:45.857478Z","iopub.status.idle":"2021-07-03T08:08:45.866842Z","shell.execute_reply.started":"2021-07-03T08:08:45.857443Z","shell.execute_reply":"2021-07-03T08:08:45.865704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target_corr = playerBoxScores_targets_corr(train_df, 0, 538)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T08:09:00.183805Z","iopub.execute_input":"2021-07-03T08:09:00.184212Z","iopub.status.idle":"2021-07-03T08:10:08.135849Z","shell.execute_reply.started":"2021-07-03T08:09:00.184173Z","shell.execute_reply":"2021-07-03T08:10:08.134523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target_corr = playerScore_target_corr.apply(lambda x : x/538)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T08:10:10.115876Z","iopub.execute_input":"2021-07-03T08:10:10.116267Z","iopub.status.idle":"2021-07-03T08:10:10.149395Z","shell.execute_reply.started":"2021-07-03T08:10:10.116236Z","shell.execute_reply":"2021-07-03T08:10:10.148345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target_corr_1 = playerScore_target_corr.loc['target1'].sort_values(ascending = False)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T08:16:20.969846Z","iopub.execute_input":"2021-07-03T08:16:20.970502Z","iopub.status.idle":"2021-07-03T08:16:20.975443Z","shell.execute_reply.started":"2021-07-03T08:16:20.970461Z","shell.execute_reply":"2021-07-03T08:16:20.974616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target_corr_1[:10]","metadata":{"execution":{"iopub.status.busy":"2021-07-03T08:17:54.418071Z","iopub.execute_input":"2021-07-03T08:17:54.418716Z","iopub.status.idle":"2021-07-03T08:17:54.427884Z","shell.execute_reply.started":"2021-07-03T08:17:54.418653Z","shell.execute_reply":"2021-07-03T08:17:54.427070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target_corr_2 = playerScore_target_corr.loc['target2'].sort_values(ascending = False)\nplayerScore_target_corr_2[:10]","metadata":{"execution":{"iopub.status.busy":"2021-07-03T08:19:50.046387Z","iopub.execute_input":"2021-07-03T08:19:50.046778Z","iopub.status.idle":"2021-07-03T08:19:50.054546Z","shell.execute_reply.started":"2021-07-03T08:19:50.046742Z","shell.execute_reply":"2021-07-03T08:19:50.053820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target_corr_3 = playerScore_target_corr.loc['target3'].sort_values(ascending = False)\nplayerScore_target_corr_3[:10]","metadata":{"execution":{"iopub.status.busy":"2021-07-03T08:20:04.529126Z","iopub.execute_input":"2021-07-03T08:20:04.529667Z","iopub.status.idle":"2021-07-03T08:20:04.539144Z","shell.execute_reply.started":"2021-07-03T08:20:04.529630Z","shell.execute_reply":"2021-07-03T08:20:04.537914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playerScore_target_corr_4 = playerScore_target_corr.loc['target4'].sort_values(ascending = False)\nplayerScore_target_corr_4[:10]","metadata":{"execution":{"iopub.status.busy":"2021-07-03T08:20:16.965658Z","iopub.execute_input":"2021-07-03T08:20:16.966144Z","iopub.status.idle":"2021-07-03T08:20:16.975531Z","shell.execute_reply.started":"2021-07-03T08:20:16.966100Z","shell.execute_reply":"2021-07-03T08:20:16.974516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"playerBoxScores가 NaN인 날을 제외하고 corr을 했더니 target간의 연관성이 높아짐\n\n타겟을 제외한 경우 0.2정도의 연관성을 보이는 것들이 몇개 있음 => 그러한 특징들이 게임 관련이므로 선수 포지션과 연관지어 다시한번 연관성을 책정하면 유의미한 결과가 나올것으로 예상","metadata":{}}]}