{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport pickle\nimport gc\n\nfrom sklearn.metrics import mean_absolute_error\nimport lightgbm as lgbm\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-23T16:35:27.638239Z","iopub.execute_input":"2021-07-23T16:35:27.638718Z","iopub.status.idle":"2021-07-23T16:35:30.001797Z","shell.execute_reply.started":"2021-07-23T16:35:27.638621Z","shell.execute_reply":"2021-07-23T16:35:30.000469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trainの中身をcsv, pickleにしてくれている親切なデータセットがあったので使います\nbase_dir = '../input/mlb-player-digital-engagement-forecasting/'\ntrain_dir = '../input/mlb-pdef-train-dataset/'","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:36:14.613851Z","iopub.execute_input":"2021-07-23T16:36:14.614300Z","iopub.status.idle":"2021-07-23T16:36:14.618560Z","shell.execute_reply.started":"2021-07-23T16:36:14.614264Z","shell.execute_reply":"2021-07-23T16:36:14.617425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# このtarget1～4を予測するのがコンペの目的\ntarget_df = pd.read_pickle(train_dir + 'nextDayPlayerEngagement_train.pkl')\ntarget_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:36:23.115549Z","iopub.execute_input":"2021-07-23T16:36:23.116157Z","iopub.status.idle":"2021-07-23T16:36:25.420869Z","shell.execute_reply.started":"2021-07-23T16:36:23.116117Z","shell.execute_reply":"2021-07-23T16:36:25.420134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# target encoding以外の特徴量はこのnotebookを参照\n# https://www.kaggle.com/mlconsult/1-38-lb-lightgbm-with-target-statistics\n\nplayers = pd.read_csv(base_dir + 'players.csv')\nrosters = pd.read_pickle(train_dir + 'rosters_train.pkl')\nscores = pd.read_pickle(train_dir + 'playerBoxScores_train.pkl')\nscores = scores.groupby(['playerId', 'date']).sum().reset_index()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:36:30.688855Z","iopub.execute_input":"2021-07-23T16:36:30.689464Z","iopub.status.idle":"2021-07-23T16:36:33.736196Z","shell.execute_reply.started":"2021-07-23T16:36:30.689425Z","shell.execute_reply":"2021-07-23T16:36:33.735086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 効きそうなカラムを抽出\ntargets_cols = ['playerId', 'target1', 'target2', 'target3', 'target4', 'date']\nplayers_cols = ['playerId', 'primaryPositionName']\nrosters_cols = ['playerId', 'teamId', 'status', 'date']\nscores_cols = ['playerId', 'battingOrder', 'gamesPlayedBatting', 'flyOuts',\n               'groundOuts', 'runsScored', 'doubles', 'triples', 'homeRuns',\n               'strikeOuts', 'baseOnBalls', 'intentionalWalks', 'hits', 'hitByPitch',\n               'atBats', 'caughtStealing', 'stolenBases', 'groundIntoDoublePlay',\n               'groundIntoTriplePlay', 'plateAppearances', 'totalBases', 'rbi',\n               'leftOnBase', 'sacBunts', 'sacFlies', 'catchersInterference',\n               'pickoffs', 'gamesPlayedPitching', 'gamesStartedPitching',\n               'completeGamesPitching', 'shutoutsPitching', 'winsPitching',\n               'lossesPitching', 'flyOutsPitching', 'airOutsPitching',\n               'groundOutsPitching', 'runsPitching', 'doublesPitching',\n               'triplesPitching', 'homeRunsPitching', 'strikeOutsPitching',\n               'baseOnBallsPitching', 'intentionalWalksPitching', 'hitsPitching',\n               'hitByPitchPitching', 'atBatsPitching', 'caughtStealingPitching',\n               'stolenBasesPitching', 'inningsPitched', 'saveOpportunities',\n               'earnedRuns', 'battersFaced', 'outsPitching', 'pitchesThrown', 'balls',\n               'strikes', 'hitBatsmen', 'balks', 'wildPitches', 'pickoffsPitching',\n               'rbiPitching', 'gamesFinishedPitching', 'inheritedRunners',\n               'inheritedRunnersScored', 'catchersInterferencePitching',\n               'sacBuntsPitching', 'sacFliesPitching', 'saves', 'holds', 'blownSaves',\n               'assists', 'putOuts', 'errors', 'chances', 'date']\n\ntarget_stat_cols = ['playerId', 'target1_mean', 'target1_median', 'target1_std', 'target1_max', 'target1_min', \n                       'target2_mean', 'target2_median', 'target2_std', 'target2_max', 'target2_min', \n                       'target3_mean', 'target3_median', 'target3_std', 'target3_max', 'target3_min', \n                       'target4_mean', 'target4_median', 'target4_std', 'target4_max', 'target4_min']\n\nfeature_cols = ['label_playerId', 'label_primaryPositionName', 'label_teamId',\n                'label_status', 'battingOrder', 'gamesPlayedBatting', 'flyOuts',\n                'groundOuts', 'runsScored', 'doubles', 'triples', 'homeRuns',\n                'strikeOuts', 'baseOnBalls', 'intentionalWalks', 'hits', 'hitByPitch',\n                'atBats', 'caughtStealing', 'stolenBases', 'groundIntoDoublePlay',\n                'groundIntoTriplePlay', 'plateAppearances', 'totalBases', 'rbi',\n                'leftOnBase', 'sacBunts', 'sacFlies', 'catchersInterference',\n                'pickoffs', 'gamesPlayedPitching', 'gamesStartedPitching',\n                'completeGamesPitching', 'shutoutsPitching', 'winsPitching',\n                'lossesPitching', 'flyOutsPitching', 'airOutsPitching',\n                'groundOutsPitching', 'runsPitching', 'doublesPitching',\n                'triplesPitching', 'homeRunsPitching', 'strikeOutsPitching',\n                'baseOnBallsPitching', 'intentionalWalksPitching', 'hitsPitching',\n                'hitByPitchPitching', 'atBatsPitching', 'caughtStealingPitching',\n                'stolenBasesPitching', 'inningsPitched', 'saveOpportunities',\n                'earnedRuns', 'battersFaced', 'outsPitching', 'pitchesThrown', 'balls',\n                'strikes', 'hitBatsmen', 'balks', 'wildPitches', 'pickoffsPitching',\n                'rbiPitching', 'gamesFinishedPitching', 'inheritedRunners',\n                'inheritedRunnersScored', 'catchersInterferencePitching',\n                'sacBuntsPitching', 'sacFliesPitching', 'saves', 'holds', 'blownSaves',\n                'assists', 'putOuts', 'errors', 'chances', \n                'target1_mean', 'target1_median', 'target1_std', 'target1_max', 'target1_min', \n                'target2_mean', 'target2_median', 'target2_std', 'target2_max', 'target2_min', \n                'target3_mean', 'target3_median', 'target3_std', 'target3_max', 'target3_min', \n                'target4_mean', 'target4_median', 'target4_std', 'target4_max', 'target4_min']","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:36:39.663246Z","iopub.execute_input":"2021-07-23T16:36:39.663643Z","iopub.status.idle":"2021-07-23T16:36:39.678856Z","shell.execute_reply.started":"2021-07-23T16:36:39.663610Z","shell.execute_reply":"2021-07-23T16:36:39.677533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 結合\ndf = target_df[targets_cols]\n\ndf = df.merge(players[players_cols], on=['playerId'], how='left')\ndf = df.merge(rosters[rosters_cols], on=['playerId', 'date'], how='left')\ndf = df.merge(scores[scores_cols], on=['playerId', 'date'], how='left')\n\n# label encoding\nplayer2num = {c: i for i, c in enumerate(df['playerId'].unique())}\nposition2num = {c: i for i, c in enumerate(df['primaryPositionName'].unique())}\nteamid2num = {c: i for i, c in enumerate(df['teamId'].unique())}\nstatus2num = {c: i for i, c in enumerate(df['status'].unique())}\ndf['label_playerId'] = df['playerId'].map(player2num)\ndf['label_primaryPositionName'] = df['primaryPositionName'].map(position2num)\ndf['label_teamId'] = df['teamId'].map(teamid2num)\ndf['label_status'] = df['status'].map(status2num)\n\n\ndel rosters, scores\ngc.collect()\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:36:55.024664Z","iopub.execute_input":"2021-07-23T16:36:55.025084Z","iopub.status.idle":"2021-07-23T16:37:01.107606Z","shell.execute_reply.started":"2021-07-23T16:36:55.025048Z","shell.execute_reply":"2021-07-23T16:37:01.106470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('player2num.pkl', 'wb') as f:\n    pickle.dump(player2num, f)\n\nwith open('position2num.pkl', 'wb') as f:\n    pickle.dump(position2num, f)\n\nwith open('teamid2num.pkl', 'wb') as f:\n    pickle.dump(teamid2num, f)\n\nwith open('status2num.pkl', 'wb') as f:\n    pickle.dump(status2num, f)\n    \ndel player2num, position2num, teamid2num, status2num","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:37:05.404376Z","iopub.execute_input":"2021-07-23T16:37:05.404782Z","iopub.status.idle":"2021-07-23T16:37:05.421211Z","shell.execute_reply.started":"2021-07-23T16:37:05.404746Z","shell.execute_reply":"2021-07-23T16:37:05.420371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# target encoding用データセット作成\ntarget_stat_df = df.loc[:, ['playerId', 'target1', 'target2', 'target3', 'target4']].groupby('playerId').agg(['mean', 'median', 'std', 'max', 'min'])","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:37:13.600296Z","iopub.execute_input":"2021-07-23T16:37:13.600798Z","iopub.status.idle":"2021-07-23T16:37:16.428174Z","shell.execute_reply.started":"2021-07-23T16:37:13.600765Z","shell.execute_reply":"2021-07-23T16:37:16.427205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_stat_df","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:37:20.409341Z","iopub.execute_input":"2021-07-23T16:37:20.409859Z","iopub.status.idle":"2021-07-23T16:37:20.454029Z","shell.execute_reply.started":"2021-07-23T16:37:20.409809Z","shell.execute_reply":"2021-07-23T16:37:20.452859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_stat_df = target_stat_df.reset_index()\ntarget_stat_df.columns = target_stat_cols\ntarget_stat_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:37:28.470270Z","iopub.execute_input":"2021-07-23T16:37:28.470659Z","iopub.status.idle":"2021-07-23T16:37:28.508556Z","shell.execute_reply.started":"2021-07-23T16:37:28.470626Z","shell.execute_reply":"2021-07-23T16:37:28.507003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_stat_df.to_pickle('target_stat_df.pkl')","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:37:35.094029Z","iopub.execute_input":"2021-07-23T16:37:35.094449Z","iopub.status.idle":"2021-07-23T16:37:35.100724Z","shell.execute_reply.started":"2021-07-23T16:37:35.094417Z","shell.execute_reply":"2021-07-23T16:37:35.099884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.merge(target_stat_df[target_stat_cols], how='left', on='playerId')\n\ndel target_stat_df\ngc.collect()\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:37:44.392381Z","iopub.execute_input":"2021-07-23T16:37:44.392845Z","iopub.status.idle":"2021-07-23T16:37:49.710481Z","shell.execute_reply.started":"2021-07-23T16:37:44.392807Z","shell.execute_reply":"2021-07-23T16:37:49.709329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_X = df[feature_cols]\ndf_y = df[['target1', 'target2', 'target3', 'target4']]\n\n# 全期間を使ってみる\n# _index = (df['date'] < 20210401)\nx_train = df_X\ny_train = df_y\nx_valid = df_X\ny_valid = df_y","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:37:51.304504Z","iopub.execute_input":"2021-07-23T16:37:51.304934Z","iopub.status.idle":"2021-07-23T16:37:54.871723Z","shell.execute_reply.started":"2021-07-23T16:37:51.304884Z","shell.execute_reply":"2021-07-23T16:37:54.870099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('training data shape:' , x_train.shape, y_train.shape)\nprint('validation data shape:' , x_valid.shape, y_valid.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:37:58.088035Z","iopub.execute_input":"2021-07-23T16:37:58.088451Z","iopub.status.idle":"2021-07-23T16:37:58.095829Z","shell.execute_reply.started":"2021-07-23T16:37:58.088411Z","shell.execute_reply":"2021-07-23T16:37:58.094446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from catboost import CatBoostRegressor\nfrom catboost import Pool\n\ndef fit_cat(x_train, y_train, x_valid, y_valid, params: dict=None, verbose=100):\n\n    oof_pred = np.zeros(len(y_valid), dtype=np.float32)\n    model = CatBoostRegressor(\n                n_estimators=2000,\n                learning_rate=0.05,\n                loss_function='MAE',\n                eval_metric='MAE',\n                max_bin=50,\n                subsample=0.9,\n                colsample_bylevel=0.5,\n                verbose=100)\n    model.fit(x_train, y_train, use_best_model=True,\n                         eval_set=(x_valid, y_valid),\n                         early_stopping_rounds=25)\n    oof_pred = model.predict(x_valid)\n    score = mean_absolute_error(oof_pred, y_valid)\n    print('mae:', score)\n    return oof_pred, model, score\n\n# training xgbm\nparams = {\n'boosting_type': 'gbdt',\n'objective':'mae',\n'subsample': 0.5,\n'subsample_freq': 1,\n'learning_rate': 0.03,\n'num_leaves': 2**11-1,\n'min_data_in_leaf': 2**12-1,\n'feature_fraction': 0.5,\n'max_bin': 100,\n'n_estimators': 2500,\n'boost_from_average': False,\n\"random_seed\":42,\n    }\n\noof1, model1_cat, score1 = fit_cat(\n    x_train, y_train['target1'],\n    x_valid, y_valid['target1'],\n    params\n    )\noof2, model2_cat, score2 = fit_cat(\n    x_train, y_train['target2'],\n    x_valid, y_valid['target2'],\n    params\n    )\noof3, model3_cat, score3 = fit_cat(\n    x_train, y_train['target3'],\n    x_valid, y_valid['target3'],\n    params\n    )\noof4, model4_cat, score4 = fit_cat(\n    x_train, y_train['target4'],\n    x_valid, y_valid['target4'],\n    params\n    )\n\nscore = (score1+score2+score3+score4) / 4\nprint(f'score: {score}')\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-23T16:38:07.770045Z","iopub.execute_input":"2021-07-23T16:38:07.770460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle.dump(model1_cat, open('model1_cat_all.pkl', 'wb'))\npickle.dump(model2_cat, open('model2_cat_all.pkl', 'wb'))\npickle.dump(model3_cat, open('model3_cat_all.pkl', 'wb'))\npickle.dump(model4_cat, open('model4_cat_all.pkl', 'wb'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}