{"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-02T14:02:45.447732Z","iopub.execute_input":"2021-07-02T14:02:45.448201Z","iopub.status.idle":"2021-07-02T14:02:45.481097Z","shell.execute_reply.started":"2021-07-02T14:02:45.448159Z","shell.execute_reply":"2021-07-02T14:02:45.479432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_targets = '/kaggle/input/playerid-and-targets/results/targets_df_'","metadata":{"execution":{"iopub.status.busy":"2021-07-02T14:03:56.900966Z","iopub.execute_input":"2021-07-02T14:03:56.901338Z","iopub.status.idle":"2021-07-02T14:03:56.906657Z","shell.execute_reply.started":"2021-07-02T14:03:56.901308Z","shell.execute_reply":"2021-07-02T14:03:56.905819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_targets_0_100 = dir_targets + '0_100.csv'\ntargets_df_0_100 = pd.read_csv(dir_targets_0_100)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T14:03:58.579815Z","iopub.execute_input":"2021-07-02T14:03:58.5806Z","iopub.status.idle":"2021-07-02T14:03:59.171787Z","shell.execute_reply.started":"2021-07-02T14:03:58.580555Z","shell.execute_reply":"2021-07-02T14:03:59.17092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_df_0_100.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:01:56.596673Z","iopub.execute_input":"2021-07-01T10:01:56.597275Z","iopub.status.idle":"2021-07-01T10:01:56.626556Z","shell.execute_reply.started":"2021-07-01T10:01:56.597239Z","shell.execute_reply":"2021-07-01T10:01:56.625693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"날짜 기준 : targets값과 피처값들관의 연관관계 조사하기\n","metadata":{}},{"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-07-02T14:04:02.101139Z","iopub.execute_input":"2021-07-02T14:04:02.101814Z","iopub.status.idle":"2021-07-02T14:05:24.303211Z","shell.execute_reply.started":"2021-07-02T14:04:02.101761Z","shell.execute_reply":"2021-07-02T14:05:24.302339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:17:29.955091Z","iopub.execute_input":"2021-07-02T07:17:29.955617Z","iopub.status.idle":"2021-07-02T07:17:29.995848Z","shell.execute_reply.started":"2021-07-02T07:17:29.955557Z","shell.execute_reply":"2021-07-02T07:17:29.994696Z"},"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-07-02T14:05:24.305161Z","iopub.execute_input":"2021-07-02T14:05:24.305652Z","iopub.status.idle":"2021-07-02T14:05:24.310232Z","shell.execute_reply.started":"2021-07-02T14:05:24.305609Z","shell.execute_reply":"2021-07-02T14:05:24.309219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_date_0 = unpack_json(train_df['nextDayPlayerEngagement'].iloc[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:30:18.006499Z","iopub.execute_input":"2021-07-02T07:30:18.006859Z","iopub.status.idle":"2021-07-02T07:30:18.040094Z","shell.execute_reply.started":"2021-07-02T07:30:18.006827Z","shell.execute_reply":"2021-07-02T07:30:18.038207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_date_0","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:30:20.250726Z","iopub.execute_input":"2021-07-02T07:30:20.251098Z","iopub.status.idle":"2021-07-02T07:30:20.275791Z","shell.execute_reply.started":"2021-07-02T07:30:20.251069Z","shell.execute_reply":"2021-07-02T07:30:20.274755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rosters_date_0 = unpack_json(train_df['rosters'].iloc[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:30:22.772456Z","iopub.execute_input":"2021-07-02T07:30:22.772831Z","iopub.status.idle":"2021-07-02T07:30:22.788344Z","shell.execute_reply.started":"2021-07-02T07:30:22.772799Z","shell.execute_reply":"2021-07-02T07:30:22.787179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rosters_date_0","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:30:25.4156Z","iopub.execute_input":"2021-07-02T07:30:25.416009Z","iopub.status.idle":"2021-07-02T07:30:25.434047Z","shell.execute_reply.started":"2021-07-02T07:30:25.415973Z","shell.execute_reply":"2021-07-02T07:30:25.432998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roster_targets_0 = pd.merge(targets_date_0,rosters_date_0, how='outer',on='playerId')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:30:27.789765Z","iopub.execute_input":"2021-07-02T07:30:27.790124Z","iopub.status.idle":"2021-07-02T07:30:27.802639Z","shell.execute_reply.started":"2021-07-02T07:30:27.790091Z","shell.execute_reply":"2021-07-02T07:30:27.801619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roster_targets_0","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:30:30.087099Z","iopub.execute_input":"2021-07-02T07:30:30.087476Z","iopub.status.idle":"2021-07-02T07:30:30.116797Z","shell.execute_reply.started":"2021-07-02T07:30:30.087442Z","shell.execute_reply":"2021-07-02T07:30:30.115959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"왜 null 값이 있지???? => 선수가 아닌가??","metadata":{}},{"cell_type":"code","source":"player_csv = pd.read_csv('/kaggle/input/mlb-player-digital-engagement-forecasting/players.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:12:25.97168Z","iopub.execute_input":"2021-07-01T10:12:25.972045Z","iopub.status.idle":"2021-07-01T10:12:25.989469Z","shell.execute_reply.started":"2021-07-01T10:12:25.972016Z","shell.execute_reply":"2021-07-01T10:12:25.988445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_csv.index = player_csv['playerId']\nplayer_csv.drop(['playerId'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:13:03.534109Z","iopub.execute_input":"2021-07-01T10:13:03.534533Z","iopub.status.idle":"2021-07-01T10:13:03.54094Z","shell.execute_reply.started":"2021-07-01T10:13:03.534499Z","shell.execute_reply":"2021-07-01T10:13:03.540122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_csv","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:14:43.973573Z","iopub.execute_input":"2021-07-01T10:14:43.974171Z","iopub.status.idle":"2021-07-01T10:14:43.998142Z","shell.execute_reply.started":"2021-07-01T10:14:43.97413Z","shell.execute_reply":"2021-07-01T10:14:43.997028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_csv.loc[656744]","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:15:51.195429Z","iopub.execute_input":"2021-07-01T10:15:51.19582Z","iopub.status.idle":"2021-07-01T10:15:51.248354Z","shell.execute_reply.started":"2021-07-01T10:15:51.195784Z","shell.execute_reply":"2021-07-01T10:15:51.245893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"'642667'이 선수에 없다.???","metadata":{}},{"cell_type":"code","source":"player_csv.loc[547989]","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:15:40.486087Z","iopub.execute_input":"2021-07-01T10:15:40.486739Z","iopub.status.idle":"2021-07-01T10:15:40.495176Z","shell.execute_reply.started":"2021-07-01T10:15:40.486682Z","shell.execute_reply":"2021-07-01T10:15:40.494014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"선수가 아닌 것으로 추정하고 => targets 이NAN이면 drop","metadata":{}},{"cell_type":"code","source":"roster_targets_0.dropna(subset=['target1','target2','target3','target4'])","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:30:40.653433Z","iopub.execute_input":"2021-07-02T07:30:40.653976Z","iopub.status.idle":"2021-07-02T07:30:40.683372Z","shell.execute_reply.started":"2021-07-02T07:30:40.65394Z","shell.execute_reply":"2021-07-02T07:30:40.682592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"roster가 NAN인 것은 그날 경기를 안 뛴것.\n1. roster NAN값 살려둔 상태에서 corr\n2. NaN 제거 후 corr","metadata":{}},{"cell_type":"code","source":"corr_NaN_matrix = roster_targets_0.corr()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:26:16.031294Z","iopub.execute_input":"2021-07-01T10:26:16.031713Z","iopub.status.idle":"2021-07-01T10:26:16.037548Z","shell.execute_reply.started":"2021-07-01T10:26:16.031676Z","shell.execute_reply":"2021-07-01T10:26:16.036509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_NaN_matrix['target1'].sort_values(ascending = False)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:27:11.732941Z","iopub.execute_input":"2021-07-01T10:27:11.733317Z","iopub.status.idle":"2021-07-01T10:27:11.741423Z","shell.execute_reply.started":"2021-07-01T10:27:11.733286Z","shell.execute_reply":"2021-07-01T10:27:11.740467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"status는 없애고 status code는 Active 면 1, 아니면 0을 준후 다시 해보기","metadata":{}},{"cell_type":"code","source":"roster_targets_0.drop(['status'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:30:47.806299Z","iopub.execute_input":"2021-07-02T07:30:47.806799Z","iopub.status.idle":"2021-07-02T07:30:47.811655Z","shell.execute_reply.started":"2021-07-02T07:30:47.806758Z","shell.execute_reply":"2021-07-02T07:30:47.810937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roster_targets_0['statusCode'] = roster_targets_0['statusCode'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:31:00.200112Z","iopub.execute_input":"2021-07-02T07:31:00.200602Z","iopub.status.idle":"2021-07-02T07:31:00.205717Z","shell.execute_reply.started":"2021-07-02T07:31:00.200569Z","shell.execute_reply":"2021-07-02T07:31:00.204887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roster_targets_0['statusCode'].replace('A', 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:31:05.372223Z","iopub.execute_input":"2021-07-02T07:31:05.372714Z","iopub.status.idle":"2021-07-02T07:31:05.379292Z","shell.execute_reply.started":"2021-07-02T07:31:05.372682Z","shell.execute_reply":"2021-07-02T07:31:05.378502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roster_targets_0","metadata":{"execution":{"iopub.status.busy":"2021-07-01T10:37:28.092927Z","iopub.execute_input":"2021-07-01T10:37:28.093457Z","iopub.status.idle":"2021-07-01T10:37:28.119326Z","shell.execute_reply.started":"2021-07-01T10:37:28.093394Z","shell.execute_reply":"2021-07-01T10:37:28.118344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roster_targets_0.corr()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T07:31:09.429843Z","iopub.execute_input":"2021-07-02T07:31:09.430324Z","iopub.status.idle":"2021-07-02T07:31:09.450734Z","shell.execute_reply.started":"2021-07-02T07:31:09.430292Z","shell.execute_reply":"2021-07-02T07:31:09.450001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# roster_targets_0 전체 코드\n'''\ntargets_date_0 = unpack_json(train_df['nextDayPlayerEngagement'].iloc[0])\nrosters_date_0 = unpack_json(train_df['rosters'].iloc[0])\nroster_targets_0 = pd.merge(targets_date_0,rosters_date_0, how='outer',on='playerId')\nplayer_csv = pd.read_csv('/kaggle/input/mlb-player-digital-engagement-forecasting/players.csv')\nplayer_csv.index = player_csv['playerId']\nplayer_csv.drop(['playerId'], axis = 1, inplace = True)\nroster_targets_0.dropna(subset=['target1','target2','target3','target4'])\nroster_targets_0.drop(['status'], axis = 1, inplace = True)\nroster_targets_0['statusCode'] = roster_targets_0['statusCode'].fillna(0)\nroster_targets_0['statusCode'].replace('A', 1, inplace = True)\nroster_targets_0.corr()\n\n\n=> 전체 날짜로 각자 corr한 후 평균 내기\n\n=> 각 피처 별로 더했다가 한번에 나눠주기\n\n'''\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = pd.DataFrame([1,2],[3,4])","metadata":{"execution":{"iopub.status.busy":"2021-07-02T04:43:09.227829Z","iopub.execute_input":"2021-07-02T04:43:09.228113Z","iopub.status.idle":"2021-07-02T04:43:09.239182Z","shell.execute_reply.started":"2021-07-02T04:43:09.228083Z","shell.execute_reply":"2021-07-02T04:43:09.238281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"위와 같은 방식으로 모든 피처 값을 연관관계 찾기\n날짜, playerId, TeamId,","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder\nordinal_encoder = OrdinalEncoder()\n\n\ndef roster_targets_corr(train_df, start, last):\n    plus = lambda a, b : a + b\n    for i in range(start, last):\n        targets_date = unpack_json(train_df['nextDayPlayerEngagement'].iloc[i])\n        rosters_date = unpack_json(train_df['rosters'].iloc[i])\n        roster_targets = pd.merge(targets_date,rosters_date, how='outer',on='playerId')\n        roster_targets.dropna(subset=['target1','target2','target3','target4'], axis = 0, inplace = True)\n        roster_targets.drop(['status'], axis = 1, inplace = True)\n        roster_targets['statusCode'] = roster_targets['statusCode'].fillna(0)\n        roster_targets['statusCode'] = roster_targets['statusCode'].apply(lambda x : 1 if x == 'A' else 0)\n        roster_targets_corr = roster_targets.corr()\n        if i == start :\n            r_t_c = roster_targets_corr\n        else :\n            r_t_c = r_t_c.combine(roster_targets_corr, plus)\n        if i%100 == 0:\n            print(i)\n    return r_t_c\n            ","metadata":{"execution":{"iopub.status.busy":"2021-07-02T14:16:14.234561Z","iopub.execute_input":"2021-07-02T14:16:14.235011Z","iopub.status.idle":"2021-07-02T14:16:14.245073Z","shell.execute_reply.started":"2021-07-02T14:16:14.23497Z","shell.execute_reply":"2021-07-02T14:16:14.244095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T14:14:42.554016Z","iopub.execute_input":"2021-07-02T14:14:42.554364Z","iopub.status.idle":"2021-07-02T14:14:42.562208Z","shell.execute_reply.started":"2021-07-02T14:14:42.554333Z","shell.execute_reply":"2021-07-02T14:14:42.560481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roster_targets_corr = roster_targets_corr(train_df, 0, 1216)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T14:16:18.826375Z","iopub.execute_input":"2021-07-02T14:16:18.826789Z","iopub.status.idle":"2021-07-02T14:17:16.822539Z","shell.execute_reply.started":"2021-07-02T14:16:18.826754Z","shell.execute_reply":"2021-07-02T14:17:16.821552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roster_targets_corr = roster_targets_corr.apply(lambda x : x/1216)\nroster_targets_corr","metadata":{"execution":{"iopub.status.busy":"2021-07-02T14:18:40.514391Z","iopub.execute_input":"2021-07-02T14:18:40.514833Z","iopub.status.idle":"2021-07-02T14:18:40.542287Z","shell.execute_reply.started":"2021-07-02T14:18:40.514798Z","shell.execute_reply":"2021-07-02T14:18:40.540093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"43과 44에서 corr을 했을 때, statusCode가 있기도 하고 없기도 함 => statusCode가 object이다. => 문자열에 대한 처리가 필요함... => 뭐하나 쉬운게 없냐 => Active만 2 나머지는 0으로 대체\n","metadata":{}},{"cell_type":"markdown","source":"### 결론 ###\n그나마 target2랑 target4랑 연관관계가 높음, \n\nroster는 버려도 될듯 함\n\n","metadata":{}}]}