{"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\nimport pandas as pd\nfrom pathlib import Path\nfrom sklearn.metrics import mean_absolute_error\nfrom datetime import timedelta\nfrom functools import reduce\nfrom tqdm import tqdm\nimport lightgbm as lgbm\nimport mlb\nimport os\nBASE_DIR = Path('../input/mlb-player-digital-engagement-forecasting')\nTRAIN_DIR = Path('../input/mlbpdeftrainupdate')","metadata":{"execution":{"iopub.status.busy":"2021-07-21T16:58:11.058823Z","iopub.execute_input":"2021-07-21T16:58:11.059237Z","iopub.status.idle":"2021-07-21T16:58:13.299105Z","shell.execute_reply.started":"2021-07-21T16:58:11.059152Z","shell.execute_reply":"2021-07-21T16:58:13.298076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players = pd.read_csv(BASE_DIR / 'players.csv')\n\nrosters = pd.read_pickle(TRAIN_DIR / 'rosters_train.pkl')\ntargets = pd.read_pickle(TRAIN_DIR / 'nextDayPlayerEngagement_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-21T16:58:13.301104Z","iopub.execute_input":"2021-07-21T16:58:13.30159Z","iopub.status.idle":"2021-07-21T16:58:17.745505Z","shell.execute_reply.started":"2021-07-21T16:58:13.301555Z","shell.execute_reply":"2021-07-21T16:58:17.744487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players ","metadata":{"execution":{"iopub.status.busy":"2021-07-21T16:58:17.74756Z","iopub.execute_input":"2021-07-21T16:58:17.747966Z","iopub.status.idle":"2021-07-21T16:58:17.783403Z","shell.execute_reply.started":"2021-07-21T16:58:17.747921Z","shell.execute_reply":"2021-07-21T16:58:17.782229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_cols = ['playerId', 'target1', 'target2', 'target3', 'target4', 'date']\nplayers_cols = ['playerId', 'primaryPositionName','heightInches','weight']\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\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','target1_mean',\n 'target1_median',\n 'target1_std',\n 'target1_min',\n 'target1_max',\n 'target1_prob',\n 'target2_mean',\n 'target2_median',\n 'target2_std',\n 'target2_min',\n 'target2_max',\n 'target2_prob',\n 'target3_mean',\n 'target3_median',\n 'target3_std',\n 'target3_min',\n 'target3_max',\n 'target3_prob',\n 'target4_mean',\n 'target4_median',\n 'target4_std',\n 'target4_min',\n 'target4_max',\n 'target4_prob']\nfeature_cols2 = ['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','target1_mean',\n 'target1_median',\n 'target1_std',\n 'target1_min',\n 'target1_max',\n 'target1_prob',\n 'target2_mean',\n 'target2_median',\n 'target2_std',\n 'target2_min',\n 'target2_max',\n 'target2_prob',\n 'target3_mean',\n 'target3_median',\n 'target3_std',\n 'target3_min',\n 'target3_max',\n 'target3_prob',\n 'target4_mean',\n 'target4_median',\n 'target4_std',\n 'target4_min',\n 'target4_max',\n 'target4_prob',\n    'target1']","metadata":{"execution":{"iopub.status.busy":"2021-07-21T16:58:17.785148Z","iopub.execute_input":"2021-07-21T16:58:17.78548Z","iopub.status.idle":"2021-07-21T16:58:17.805764Z","shell.execute_reply.started":"2021-07-21T16:58:17.785447Z","shell.execute_reply":"2021-07-21T16:58:17.804387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_target_stats = pd.read_csv(\"../input/my-player-target-stat/player_target_stats.csv\")\ndata_names=player_target_stats.columns.values.tolist()\ndata_names","metadata":{"execution":{"iopub.status.busy":"2021-07-21T16:58:17.80754Z","iopub.execute_input":"2021-07-21T16:58:17.807988Z","iopub.status.idle":"2021-07-21T16:58:17.856547Z","shell.execute_reply.started":"2021-07-21T16:58:17.807946Z","shell.execute_reply":"2021-07-21T16:58:17.855496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creat dataset\ntrain = targets[targets_cols].merge(players[players_cols], on=['playerId'], how='left')\ntrain = train.merge(rosters[rosters_cols], on=['playerId', 'date'], how='left')\ntrain = train.merge(scores[scores_cols], on=['playerId', 'date'], how='left')\ntrain = train.merge(player_target_stats, how='inner', left_on=[\"playerId\"],right_on=[\"playerId\"])\n\n\n# label encoding\nplayer2num = {c: i for i, c in enumerate(train['playerId'].unique())}\nposition2num = {c: i for i, c in enumerate(train['primaryPositionName'].unique())}\nteamid2num = {c: i for i, c in enumerate(train['teamId'].unique())}\nstatus2num = {c: i for i, c in enumerate(train['status'].unique())}\ntrain['label_playerId'] = train['playerId'].map(player2num)\ntrain['label_primaryPositionName'] = train['primaryPositionName'].map(position2num)\ntrain['label_teamId'] = train['teamId'].map(teamid2num)\ntrain['label_status'] = train['status'].map(status2num)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T16:58:17.858027Z","iopub.execute_input":"2021-07-21T16:58:17.858343Z","iopub.status.idle":"2021-07-21T16:58:27.273013Z","shell.execute_reply.started":"2021-07-21T16:58:17.858312Z","shell.execute_reply":"2021-07-21T16:58:27.271805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = train[feature_cols]\ntrain_y = train[['target1', 'target2', 'target3', 'target4']]\n\n#_index = (train['date'] < 20210401)\n_index = ((train['date'] > 20200529) & (train['date'] <= 20200801)) | ((train['date'] > 20190529) & (train['date'] <= 20190801)) | ((train['date'] > 20180529) & (train['date'] <= 20180801))\nx_train1 = train_X.loc[~_index].reset_index(drop=True)\ny_train1 = train_y.loc[~_index].reset_index(drop=True)\nx_valid1 = train_X.loc[_index].reset_index(drop=True)\ny_valid1 = train_y.loc[_index].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T16:58:27.274405Z","iopub.execute_input":"2021-07-21T16:58:27.274838Z","iopub.status.idle":"2021-07-21T16:58:31.650836Z","shell.execute_reply.started":"2021-07-21T16:58:27.274795Z","shell.execute_reply":"2021-07-21T16:58:31.649678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = train[feature_cols2]\ntrain_y = train[['target1', 'target2', 'target3', 'target4']]\n\n#_index = (train['date'] < 20210401)\nx_train2 = train_X.loc[~_index].reset_index(drop=True)\ny_train2 = train_y.loc[~_index].reset_index(drop=True)\nx_valid2 = train_X.loc[_index].reset_index(drop=True)\ny_valid2 = train_y.loc[_index].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T16:58:31.653448Z","iopub.execute_input":"2021-07-21T16:58:31.653772Z","iopub.status.idle":"2021-07-21T16:58:34.529042Z","shell.execute_reply.started":"2021-07-21T16:58:31.653742Z","shell.execute_reply":"2021-07-21T16:58:34.528045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LightGBM training","metadata":{}},{"cell_type":"code","source":"# def fit_lgbm(x_train, y_train, x_valid, y_valid, params: dict=None, verbose=100):\n#     oof_pred = np.zeros(len(y_valid), dtype=np.float32)\n#     model = lgbm.LGBMRegressor(**params)\n#     model.fit(x_train, y_train, \n#         eval_set=[(x_valid, y_valid)],  \n#         early_stopping_rounds=verbose, \n#         verbose=verbose)\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\nparams1 = {'objective':'mae',\n           'reg_alpha': 0.14547461820098767, \n           'reg_lambda': 0.10185644384043743, \n           'n_estimators': 3333, \n           'learning_rate': 0.1046301304430488, \n           'num_leaves': 674, \n           'feature_fraction': 0.8101240539122566, \n           'bagging_fraction': 0.8884451442950513, \n           'bagging_freq': 8, \n           'min_child_samples': 51}\n\nparams2 = {\n 'objective':'mae',\n           'reg_alpha': 0.14947461820098767, \n           'reg_lambda': 0.10185644384043743, \n           'n_estimators': 3633, \n           'learning_rate': 0.08046301304430488, \n           'num_leaves': 64, \n           'feature_fraction': 0.9101240539122566, \n           'bagging_fraction': 0.9884451442950513, \n           'bagging_freq': 3, \n           'min_child_samples': 15\n}\n\nparams4 = {'objective':'mae',\n           'reg_alpha': 0.016468100279441976, \n           'reg_lambda': 0.09128335764019105, \n           'n_estimators': 9868, \n           'learning_rate': 0.10528150510326864, \n           'num_leaves': 157, \n           'feature_fraction': 0.5419185713426886, \n           'bagging_fraction': 0.2637405128936662, \n           'bagging_freq': 19, \n           'min_child_samples': 71}\n\n\nparams = {\n 'objective':'mae',\n#  'reg_alpha': 0.1,\n#  'reg_lambda': 0.1, \n 'n_estimators': 10000,\n 'learning_rate': 0.1,\n 'random_state': 2021,\n \"num_leaves\": 127,\n 'feature_fraction': 0.5419185713426886, \n 'bagging_fraction': 0.5637405128936662, \n 'bagging_freq': 15, \n}\n\n\n\n# oof1, model1, score1 = fit_lgbm(\n#     x_train1, y_train1['target1'],\n#     x_valid1, y_valid1['target1'],\n#     params1\n#  )\n\n# oof2, model2, score2 = fit_lgbm(\n#     x_train2, y_train2['target2'],\n#     x_valid2, y_valid2['target2'],\n#     params2\n# )\n\n# oof3, model3, score3 = fit_lgbm(\n#     x_train2, y_train2['target3'],\n#     x_valid2, y_valid2['target3'],\n#    params\n# )\n\n# oof4, model4, score4 = fit_lgbm(\n#     x_train2, y_train2['target4'],\n#     x_valid2, y_valid2['target4'],\n#     params4\n# )\n\n# score = (score1+score2+score3+score4) / 4\n# print(f'score: {score}')","metadata":{"execution":{"iopub.status.busy":"2021-07-21T16:58:34.530574Z","iopub.execute_input":"2021-07-21T16:58:34.530883Z","iopub.status.idle":"2021-07-21T16:58:34.540855Z","shell.execute_reply.started":"2021-07-21T16:58:34.530852Z","shell.execute_reply":"2021-07-21T16:58:34.539853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CatBoost","metadata":{}},{"cell_type":"code","source":"import pickle\nfrom catboost import CatBoostRegressor\n\ndef fit_lgbm(x_train, y_train, x_valid, y_valid, target, params: dict=None, verbose=100):\n    oof_pred_lgb = np.zeros(len(y_valid), dtype=np.float32)\n    oof_pred_cat = np.zeros(len(y_valid), dtype=np.float32)\n    \n    if os.path.isfile(f'../input/mlb-lgbm-and-catboost-models/mymodel_lgb_{target}.pkl'):\n        with open(f'../input/mlb-lgbm-and-catboost-models/mymodel_lgb_{target}.pkl', 'rb') as fin:\n            model = pickle.load(fin)\n    else:\n    \n        model = lgbm.LGBMRegressor(**params)\n        model.fit(x_train, y_train, \n            eval_set=[(x_valid, y_valid)],  \n            early_stopping_rounds=verbose, \n            verbose=verbose)\n\n        with open(f'mymodel_lgb_{target}.pkl', 'wb') as handle:\n            pickle.dump(model, handle, protocol=pickle.HIGHEST_PROTOCOL)\n    \n    oof_pred_lgb = model.predict(x_valid)\n    score_lgb = mean_absolute_error(oof_pred_lgb, y_valid)\n    print('mae:', score_lgb)\n    \n#     if os.path.isfile(f'../input/mlb-lgbm-and-catboost-models/mymodel_cb_{target}.pkl'):\n#         with open(f'../input/mlb-lgbm-and-catboost-models/mymodel_cb_{target}.pkl', 'rb') as fin:\n#             model_cb = pickle.load(fin)\n#     else:\n    \n#         model_cb = CatBoostRegressor(\n#                     n_estimators=2500,\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.6,\n#                     verbose=100)\n\n#         model_cb.fit(x_train, y_train, use_best_model=True,\n#                          eval_set=(x_valid, y_valid),\n#                          early_stopping_rounds=25)\n\n#         with open(f'model_cb_{target}.pkl', 'wb') as handle:\n#             pickle.dump(model_cb, handle, protocol=pickle.HIGHEST_PROTOCOL)\n    \n#     oof_pred_cat = model_cb.predict(x_valid)\n#     score_cat = mean_absolute_error(oof_pred_cat, y_valid)\n#     print('mae:', score_cat)\n    \n    return oof_pred_lgb, model, score_lgb\n\n\n# training lightgbm\nparams = {\n'boosting_type': 'gbdt',\n'objective':'mae',\n'subsample': 0.6,\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.6,\n'max_bin': 100,\n'n_estimators': 3000,\n'boost_from_average': False,\n\"random_seed\":2021,\n}\n\noof_pred_lgb2, model_lgb2, score_lgb2 = fit_lgbm(\n    x_train1, y_train1['target2'],\n    x_valid1, y_valid1['target2'],\n    2, params\n)\n\noof_pred_lgb1, model_lgb1, score_lgb1 = fit_lgbm(\n    x_train1, y_train1['target1'],\n    x_valid1, y_valid1['target1'],\n    1, params\n)\n\noof_pred_lgb3, model_lgb3,score_lgb3 = fit_lgbm(\n    x_train1, y_train1['target3'],\n    x_valid1, y_valid1['target3'],\n    3, params\n)\noof_pred_lgb4, model_lgb4, score_lgb4= fit_lgbm(\n    x_train1, y_train1['target4'],\n    x_valid1, y_valid1['target4'],\n    4, params\n)\n\nscore = (score_lgb1+score_lgb2+score_lgb3+score_lgb4) / 4\nprint(f'LightGBM score: {score}')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-21T16:58:34.542123Z","iopub.execute_input":"2021-07-21T16:58:34.54252Z"},"trusted":true},"execution_count":null,"outputs":[]}]}