{"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":"markdown","source":"# MLB Engagement Predetion using LightGBM","metadata":{}},{"cell_type":"markdown","source":"This is the first competition I spent a lot of time conducting research, do feature engineering, design appropriate cv methods. I'm a big baseball fan and very glad to have the opportunity to participating in this competition. Below is the method and pipeline about my work. ","metadata":{}},{"cell_type":"markdown","source":"## Import Library","metadata":{}},{"cell_type":"code","source":"import torch\nimport 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_notebook\nimport lightgbm as lgbm\nimport mlb\nimport joblib","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:23.098271Z","iopub.execute_input":"2021-07-28T15:17:23.098669Z","iopub.status.idle":"2021-07-28T15:17:23.10377Z","shell.execute_reply.started":"2021-07-28T15:17:23.09863Z","shell.execute_reply":"2021-07-28T15:17:23.102915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Dataset","metadata":{}},{"cell_type":"markdown","source":"Shout out to @colum2131 and @Ken Miller. Due to their preprocess on raw data, I saved a lot of time to deal with this part.","metadata":{}},{"cell_type":"code","source":"BASE_DIR = Path('../input/mlb-player-digital-engagement-forecasting')\nTRAIN_DIR = Path('../input/mlb-pdef-train-dataset')\n\nplayers = pd.read_csv(BASE_DIR / 'players.csv')\nseasons = pd.read_csv(BASE_DIR / 'seasons.csv')\nrosters = pd.read_pickle(TRAIN_DIR / 'rosters_train.pkl')\ntargets = pd.read_pickle(TRAIN_DIR / 'nextDayPlayerEngagement_train.pkl')\ngames = pd.read_pickle(TRAIN_DIR / 'games_train.pkl')\nscores = pd.read_pickle(TRAIN_DIR / 'playerBoxScores_train.pkl')\nteam_scores = pd.read_pickle(TRAIN_DIR / 'teamBoxScores_train.pkl')\ntransactions = pd.read_pickle(TRAIN_DIR / 'transactions_train.pkl')\nawards = pd.read_pickle(TRAIN_DIR / 'awards_train.pkl')\nstandings = pd.read_pickle(TRAIN_DIR / 'standings_train.pkl')\ninseason_player_target_stats = pd.read_csv(\"../input/inseason-target-stats/inseason_target_stats.csv\")\nlastmonth_player_target_stats = pd.read_csv(\"../input/month-target-stats/last_month_target_stats.csv\")\ncumulative_data = pd.read_csv(\"../input/cumulated-revised-data/train_cumulated.csv\")\nplayoff_cumulative_data = pd.read_csv('../input/playoff-cumulated-data/train_cumulated_playoff.csv')\nlast7_cumulative_data = pd.read_csv('../input/recently-player-stats/train_cumulated_last7.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:23.108744Z","iopub.execute_input":"2021-07-28T15:17:23.109389Z","iopub.status.idle":"2021-07-28T15:17:35.554952Z","shell.execute_reply.started":"2021-07-28T15:17:23.109347Z","shell.execute_reply":"2021-07-28T15:17:35.553924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocess data","metadata":{}},{"cell_type":"markdown","source":"### Date","metadata":{}},{"cell_type":"markdown","source":"#### Revise wrong date in season.csv","metadata":{}},{"cell_type":"code","source":"seasons['regularSeasonStartDate'][2] = '2019-03-28'\nseasons['seasonStartDate'][2] = '2019-03-28'\nseasons['seasonStartDate'][4] = '2021-04-01'","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:35.556725Z","iopub.execute_input":"2021-07-28T15:17:35.557198Z","iopub.status.idle":"2021-07-28T15:17:35.569197Z","shell.execute_reply.started":"2021-07-28T15:17:35.557157Z","shell.execute_reply":"2021-07-28T15:17:35.568022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Convert \"date\" feature to datetime type","metadata":{}},{"cell_type":"code","source":"seasons['regularSeasonStartDate'] = pd.to_datetime(seasons['regularSeasonStartDate'])\nseasons['postSeasonEndDate'] = pd.to_datetime(seasons['postSeasonEndDate'])\nseasons['regularSeasonEndDate'] = pd.to_datetime(seasons['regularSeasonEndDate'])\nseasons['allStarDate'][3] = np.nan\nseasons['allStarDate'] = pd.to_datetime(seasons['allStarDate'])\nseasons.rename(columns = {'seasonId':'year'}, inplace = True)\n\nteam_col_dict = {}\nfor i, col in enumerate(team_scores.columns):\n    if i > 4 and i < len(team_scores.columns)-2:\n        team_col_dict[col] = 'team_'+col\nteam_scores.rename(columns = team_col_dict, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:35.571621Z","iopub.execute_input":"2021-07-28T15:17:35.572114Z","iopub.status.idle":"2021-07-28T15:17:35.59118Z","shell.execute_reply.started":"2021-07-28T15:17:35.572059Z","shell.execute_reply":"2021-07-28T15:17:35.590176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lastmonth_player_target_stats.rename(columns = \n                                     {'target1_median':'target1_last_month_median', 'target2_median':'target2_last_month_median', \n                                      'target3_median':'target3_last_month_median', 'target4_median':'target4_last_month_median',\n                                      'target1_std':'target1_last_month_std', 'target2_std':'target2_last_month_std',\n                                      'target3_std':'target3_last_month_std', 'target4_std':'target4_last_month_std',\n                                      'target1_mean':'target1_last_month_mean', 'target2_mean':'target2_last_month_mean', \n                                      'target3_mean':'target3_last_month_mean', 'target4_mean':'target4_last_month_mean',}, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:35.593059Z","iopub.execute_input":"2021-07-28T15:17:35.593459Z","iopub.status.idle":"2021-07-28T15:17:35.603701Z","shell.execute_reply.started":"2021-07-28T15:17:35.593418Z","shell.execute_reply":"2021-07-28T15:17:35.602704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last7_cumulative_data.rename(columns = \n                             {\"cumulative_hits\": \"last7_cumulative_hits\", \"cumulative_atBats\": \"last7_cumulative_atBats\", \n                              \"cumulative_earnedRuns\": \"last5_cumulative_earnedRuns\", \"cumulative_inningsPitched\": \"last5_cumulative_inningsPitched\", \n                              \"cumulative_totalBases\": \"last7_cumulative_totalBases\", \"cumulative_baseOnBalls\": \"last7_cumulative_baseOnBalls\", \n                              \"cumulative_hitByPitch\": \"last7_cumulative_hitByPitch\",\"cumulative_sacFlies\": \"last7_cumulative_sacFlies\", \n                              \"cumulative_baseOnBallsPitching\": \"last5_cumulative_baseOnBallsPitching\", \n                              \"cumulative_hitByPitchPitching\": \"last5_cumulative_hitByPitchPitching\", \n                              \"cumulative_hitsPitching\": \"last5_cumulative_hitsPitching\", \"cumulative_hr\": \"last7_cumulative_hr\", \n                              'cumulative_rbi': \"last7_cumulative_rbi\", \"avg\": \"last7_avg\", \"era\": \"last5_era\", \"slg\":\"last7_slg\", \n                              \"obp\": \"last7_obp\", \"ops\": \"last7_ops\", \"whip\": \"last5_whip\",\n                              \"cumulative_win\": \"last5_cumulative_win\", \"cumulative_hold\": \"last5_cumulative_hold\", \n                              \"cumulative_save\": \"last5_cumulative_save\", 'cumulative_loss': \"last5_cumulative_loss\", \n                              'cumulative_bs': \"last5_cumulative_bs\", 'cumulative_k': 'last5_cumulative_k'}, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:35.605011Z","iopub.execute_input":"2021-07-28T15:17:35.605329Z","iopub.status.idle":"2021-07-28T15:17:35.622943Z","shell.execute_reply.started":"2021-07-28T15:17:35.605296Z","shell.execute_reply":"2021-07-28T15:17:35.621718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lastmonth_player_target_stats","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:35.624485Z","iopub.execute_input":"2021-07-28T15:17:35.624885Z","iopub.status.idle":"2021-07-28T15:17:35.688506Z","shell.execute_reply.started":"2021-07-28T15:17:35.624837Z","shell.execute_reply":"2021-07-28T15:17:35.687366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"team_scores","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:35.69001Z","iopub.execute_input":"2021-07-28T15:17:35.690628Z","iopub.status.idle":"2021-07-28T15:17:35.724849Z","shell.execute_reply.started":"2021-07-28T15:17:35.690575Z","shell.execute_reply":"2021-07-28T15:17:35.723709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Create \"year\", \"month\", \"days\" columns","metadata":{}},{"cell_type":"code","source":"standings['year'] = pd.to_datetime(standings['gameDate']).dt.year\nstandings['month'] = pd.to_datetime(standings['gameDate']).dt.month\nstandings['days'] = pd.to_datetime(standings['gameDate']).dt.day\nstandings['date'] = standings['year'] * 10000 + standings['month'] * 100 + standings['days']\ntargets['year'] = pd.to_datetime(targets['date'], format=\"%Y%m%d\").dt.year\ntargets['month'] = pd.to_datetime(targets['date'], format=\"%Y%m%d\").dt.month\ntargets['days'] = pd.to_datetime(targets['date'], format=\"%Y%m%d\").dt.day","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:35.727651Z","iopub.execute_input":"2021-07-28T15:17:35.728323Z","iopub.status.idle":"2021-07-28T15:17:36.571689Z","shell.execute_reply.started":"2021-07-28T15:17:35.728264Z","shell.execute_reply":"2021-07-28T15:17:36.57087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['datetime_date'] = pd.to_datetime(transactions['date'], format=\"%Y%m%d\")\ntransactions['transaction_time'] = 1\n\ntmp_df = transactions.copy()\ntmp_df['datetime_date'] = transactions['datetime_date'] + pd.DateOffset(1)\ntmp_df['transaction_time'] = 0\ntmp_df['date'] = tmp_df['datetime_date'].dt.year * 10000 + tmp_df['datetime_date'].dt.month * 100 + tmp_df['datetime_date'].dt.day\ntransactions = pd.concat([transactions, tmp_df], axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:36.573438Z","iopub.execute_input":"2021-07-28T15:17:36.57411Z","iopub.status.idle":"2021-07-28T15:17:36.578957Z","shell.execute_reply.started":"2021-07-28T15:17:36.574053Z","shell.execute_reply":"2021-07-28T15:17:36.577891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Box Score","metadata":{}},{"cell_type":"markdown","source":"#### Combine two games in one day","metadata":{}},{"cell_type":"code","source":"scores['game_count'] = 1\nscores = scores.groupby(['playerId', 'date']).sum().reset_index()\nscores","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:36.580026Z","iopub.execute_input":"2021-07-28T15:17:36.580303Z","iopub.status.idle":"2021-07-28T15:17:37.394834Z","shell.execute_reply.started":"2021-07-28T15:17:36.580275Z","shell.execute_reply":"2021-07-28T15:17:37.393721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Columns Name","metadata":{}},{"cell_type":"code","source":"targets_cols = ['playerId', 'target1', 'target2', 'target3', 'target4', \n                'date', 'year', 'month']\nplayers_cols = ['playerId', 'primaryPositionName']\nrosters_cols = ['playerId', 'teamId', 'status', 'date']\nscores_cols = ['playerId', 'flyOuts', 'groundOuts', 'runsScored', 'gamePk',\n               'doubles', 'triples', 'homeRuns', 'strikeOuts', \n               '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', 'runsPitching', 'doublesPitching',\n               'triplesPitching', 'homeRunsPitching', 'strikeOutsPitching',\n               'baseOnBallsPitching', 'intentionalWalksPitching', 'hitsPitching',\n               'hitByPitchPitching', 'atBatsPitching', 'caughtStealingPitching',\n               'stolenBasesPitching', 'inningsPitched', 'saveOpportunities',\n               'earnedRuns', 'battersFaced', 'outsPitching', 'pitchesThrown',\n               'balls', 'strikes', 'hitBatsmen', 'balks', 'wildPitches', \n               'pickoffsPitching', 'rbiPitching', 'gamesFinishedPitching', \n               'inheritedRunners', 'inheritedRunnersScored', \n               'catchersInterferencePitching', 'sacBuntsPitching', \n               'sacFliesPitching', 'saves', 'holds', 'blownSaves',\n               'assists', 'putOuts', 'errors', 'chances', 'date']\n\nteam_scores_cols = ['teamId', 'gamePk', 'team_runsScored', 'team_runsPitching']\n\ntrans_cols = ['playerId', 'date', 'typeDesc', 'transaction_time']\nawards_cols = ['playerId', 'date', 'awardId']\ngames_cols = ['gamePk', 'homeId', 'awayId', 'dayNight', 'gameType']\nstandings_cols = ['streakCode', 'pct', 'teamId', 'date']\nstats_cols = ['playerId', 'target1_median','target1_std', 'target2_median', 'target2_std', 'target3_median', 'target3_std','target4_median', 'target4_std',\n             'target1_mean', 'target2_mean', 'target3_mean', 'target4_mean']\nlast_month_stats_cols = ['playerId', 'year', 'month', 'target1_last_month_median','target1_last_month_std', 'target2_last_month_median', 'target2_last_month_std', \n                         'target3_last_month_median', 'target3_last_month_std','target4_last_month_median', 'target4_last_month_std',\n                         'target1_last_month_mean', 'target2_last_month_mean', 'target3_last_month_mean', 'target4_last_month_mean']","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:37.396391Z","iopub.execute_input":"2021-07-28T15:17:37.397047Z","iopub.status.idle":"2021-07-28T15:17:37.407641Z","shell.execute_reply.started":"2021-07-28T15:17:37.396991Z","shell.execute_reply":"2021-07-28T15:17:37.406752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_cols = ['label_playerId', 'label_primaryPositionName', 'label_teamId',\n                'label_status', 'DaysAfterRegularSeason', 'label_typeDesc', \n                'label_awardId', 'flyOuts', 'groundOuts', 'runsScored',\n                'label_homeId', 'label_awayId', 'Split', 'label_gameType',\n                'WinLose', 'Streak', 'pct', 'label_dayNight', 'transaction_time',\n                'avg', 'ops', 'cumulative_hits', 'cumulative_hr', 'cumulative_rbi',\n                'era', 'whip', 'cumulative_win', 'cumulative_loss', 'pitch_win_pct',\n                'cumulative_save', 'cumulative_bs', 'cumulative_h_streak', \n                'last7_avg', 'last7_ops', 'last7_cumulative_hits', 'last7_cumulative_hr', 'last7_cumulative_rbi',\n                'last5_era', 'last5_whip', 'last5_cumulative_win', 'last5_cumulative_loss',\n                'doubles', 'triples', 'homeRuns', 'strikeOuts', 'baseOnBalls', \n                'intentionalWalks', 'hits', 'hitByPitch', 'atBats', \n                'caughtStealing', 'stolenBases', 'totalBases', 'rbi',\n                'leftOnBase', 'catchersInterference', 'pickoffs', \n                'gamesPlayedPitching', 'gamesStartedPitching',\n                'completeGamesPitching', 'shutoutsPitching', 'winsPitching',\n                'lossesPitching',  'runsPitching', 'doublesPitching',\n                'triplesPitching', 'homeRunsPitching', 'strikeOutsPitching',\n                'baseOnBallsPitching', 'intentionalWalksPitching', 'hitsPitching',\n                'hitByPitchPitching', 'atBatsPitching', 'caughtStealingPitching',\n                'stolenBasesPitching', 'inningsPitched', 'saveOpportunities',\n                'earnedRuns',  'outsPitching', 'pitchesThrown', 'balls',\n                'strikes', 'hitBatsmen', 'balks', 'wildPitches', 'pickoffsPitching',\n                'rbiPitching', 'gamesFinishedPitching', 'inheritedRunners',\n                'inheritedRunnersScored', 'catchersInterferencePitching',\n                'saves', 'holds', 'blownSaves', 'assists', 'putOuts', 'errors', 'chances',\n                'target1_median','target1_std',  'target1_mean', 'target1_last_month_median','target1_last_month_std', 'target1_last_month_mean',\n                'target2_median', 'target2_std',  'target2_mean', 'target2_last_month_median','target2_last_month_std', 'target2_last_month_mean',\n                'target3_median', 'target3_std',  'target3_mean','target3_last_month_median','target3_last_month_std',  'target3_last_month_mean',\n                'target4_median', 'target4_std',  'target4_mean','target4_last_month_median','target4_last_month_std' ,'target4_last_month_mean']","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:37.40909Z","iopub.execute_input":"2021-07-28T15:17:37.409583Z","iopub.status.idle":"2021-07-28T15:17:37.424918Z","shell.execute_reply.started":"2021-07-28T15:17:37.409546Z","shell.execute_reply":"2021-07-28T15:17:37.424026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Merge data","metadata":{}},{"cell_type":"code","source":"train = 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(games[games_cols], on=['gamePk'], how='left')\nfor i, row in tqdm_notebook(games[(games['gameType'] == 'E') | (games['gameType'] == 'S')].iterrows()):\n    train.loc[(train['date'] == row['date']) & ((train['teamId'] == row['homeId']) | (train['teamId'] == row['awayId'])), 'gameType'] = row['gameType']\ntrain.loc[train['gamePk'] > 700000, 'gameType'] = 'R'\ntrain = train.merge(standings[standings_cols], on=['teamId', 'date'], how='left')\n# train = train.merge(team_scores[team_scores_cols], on=['gamePk', 'teamId'], how='left')\ntrain = train.merge(lastmonth_player_target_stats[last_month_stats_cols], how='inner', left_on=[\"playerId\", 'year', 'month'],right_on=[\"playerId\", 'year', 'month'])\ntrain = train.merge(inseason_player_target_stats[stats_cols], how='inner', left_on=[\"playerId\"],right_on=[\"playerId\"])\ntrain = train.merge(seasons, on=['year'], how='left')\ntransactions = transactions[trans_cols].drop_duplicates(subset=['playerId', 'date'])\ntrain = train.merge(transactions, on=['playerId', 'date'], how='left')\nawards = awards[awards_cols].drop_duplicates(subset=['playerId', 'date'])\ntrain = train.merge(awards, on=['playerId', 'date'], how='left')\n\ntrain = train.drop_duplicates(subset=['playerId', 'date'])\ntrain = train.merge(cumulative_data, on=['playerId', 'date'], how='left')\ntrain = train.merge(last7_cumulative_data, on=['playerId', 'date'], how='left')\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:17:37.426034Z","iopub.execute_input":"2021-07-28T15:17:37.426536Z","iopub.status.idle":"2021-07-28T15:19:29.480581Z","shell.execute_reply.started":"2021-07-28T15:17:37.426501Z","shell.execute_reply":"2021-07-28T15:19:29.479198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Label Encoding","metadata":{}},{"cell_type":"code","source":"player2num = {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())}\ntransdesc2num = {c: i for i, c in enumerate(train['typeDesc'].unique())}\nawardid2num = {c: i for i, c in enumerate(train['awardId'].unique())}\ngametype2num = {c: i for i, c in enumerate(train['gameType'].unique())}\n# print(gametype2num)\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)\ntrain['label_typeDesc'] = train['typeDesc'].map(transdesc2num)\ntrain['label_awardId'] = train['awardId'].map(awardid2num)\ntrain['label_homeId'] = train['homeId'].map(teamid2num)\ntrain['label_awayId'] = train['awayId'].map(teamid2num)\ntrain['label_dayNight'] = train['dayNight'].map({'day': 0, 'night': 1})\ntrain['label_gameType'] = train['gameType'].map(gametype2num)","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:19:29.482703Z","iopub.execute_input":"2021-07-28T15:19:29.483219Z","iopub.status.idle":"2021-07-28T15:19:31.229314Z","shell.execute_reply.started":"2021-07-28T15:19:29.483162Z","shell.execute_reply":"2021-07-28T15:19:31.228448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Engineering","metadata":{}},{"cell_type":"code","source":"train['datetime_date'] = pd.to_datetime(train['date'], format=\"%Y%m%d\")\ntrain['streakCode'] = train['streakCode'].fillna('W0')\ntrain['WinLose'] = train['streakCode'].str[0]\ntrain['WinLose'] = train['WinLose'].map({'L': 0, 'W': 1})\ntrain['Streak'] = train['streakCode'].str[1].astype(int)\ntrain['pct'] = train['pct'].astype(float)\ntrain['hr_ab'] = train['cumulative_hr'] / train['cumulative_atBats']\ntrain['pitch_win_pct'] = train['cumulative_win'] / (train['cumulative_win'] + train['cumulative_loss'])","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:19:31.230427Z","iopub.execute_input":"2021-07-28T15:19:31.230928Z","iopub.status.idle":"2021-07-28T15:19:35.772097Z","shell.execute_reply.started":"2021-07-28T15:19:31.23088Z","shell.execute_reply":"2021-07-28T15:19:35.771209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### CV Split","metadata":{}},{"cell_type":"code","source":"train['DaysAfterRegularSeason'] = (train['datetime_date'] - train['regularSeasonStartDate']).dt.days\ntrain['DaysAfterAllStar'] = (train['datetime_date'] - train['allStarDate']).dt.days\ntrain['DaysAfterpostSeasonEnd'] = (train['datetime_date'] - train['postSeasonEndDate']).dt.days\ntrain['DaysAfterRegularSeasonEnd'] = (train['datetime_date'] - train['regularSeasonEndDate']).dt.days\n# train['DaysAfterLastSeasonEnd'] = (train['datetime_date'] - train['LastSeasonEndDate']).dt.days\n\ndays_df = train[['year', 'month', 'DaysAfterRegularSeason', 'DaysAfterAllStar', 'DaysAfterRegularSeasonEnd']]\ndef f(x):\n    if x[2] < 0:\n        return 0\n    elif x[0] == 2018:\n        if x[1] == 5 or x[1] == 6:\n            return 1\n        elif x[1] == 8 or x[1] == 9:\n            return 2\n        else:\n            return 0\n    elif x[0] == 2019:\n        if x[1] == 2 or x[1] == 3 or x[1] == 4 or x[1] == 5 or x[1] == 6:\n            return 3\n        elif x[1] == 7 or x[1] == 8 or x[1] == 9 or x[1] == 10 or x[1] == 11:\n            return 4\n        else:\n            return 0\n        \n    elif x[0] == 2020:\n        return 5\n#     elif x[0] == 2019:\n#         if x[1] == 5 or x[1] == 6:\n#             return 3\n#         elif x[1] == 8 or x[1] == 9:\n#             return 4\n#         else:\n#             return 0\n        \n    else:\n        return 0\n        \ntrain['Split'] = days_df.apply(f, axis=1)\nactive_players = players.loc[players['playerForTestSetAndFuturePreds'] == True, 'playerId']\nactive_players = active_players.apply(lambda x: player2num[x])","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:19:35.773265Z","iopub.execute_input":"2021-07-28T15:19:35.773793Z","iopub.status.idle":"2021-07-28T15:20:11.018925Z","shell.execute_reply.started":"2021-07-28T15:19:35.773736Z","shell.execute_reply":"2021-07-28T15:20:11.017946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = train[feature_cols].reset_index(drop=True)\nNFOLDS = 5\ntrain_y = train[['target1', 'target2', 'target3', 'target4']].reset_index(drop=True)\nx_train = train_X\ntrain_X","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:20:11.020481Z","iopub.execute_input":"2021-07-28T15:20:11.020925Z","iopub.status.idle":"2021-07-28T15:20:13.588151Z","shell.execute_reply.started":"2021-07-28T15:20:11.020874Z","shell.execute_reply":"2021-07-28T15:20:13.586933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 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","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:20:13.590674Z","iopub.execute_input":"2021-07-28T15:20:13.591073Z","iopub.status.idle":"2021-07-28T15:20:13.599336Z","shell.execute_reply.started":"2021-07-28T15:20:13.591019Z","shell.execute_reply":"2021-07-28T15:20:13.597867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv = 0\nparams = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 12000,\n 'learning_rate': 0.01,\n 'random_state': 208,\n \"num_leaves\": 250,\n \"max_depth\": 8,\n\n}\nparams2 = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 12000,\n 'learning_rate': 0.01,\n 'random_state': 208,\n \"num_leaves\": 250, \n\n}\n\n\nmodel1_list = []\nmodel2_list = []\nmodel3_list = []\nmodel4_list = []\n\nfor idx in range(NFOLDS):\n    print(\"FOLD:\", idx)\n#     tr_idx, val_idx = folds[idx]\n    x_tr = x_train[x_train['Split'] != idx+1].drop(columns=['Split'])\n    x_val = x_train[x_train['Split'] == idx+1][x_train[x_train['Split'] == idx+1]['label_playerId'].isin(active_players.tolist())].drop(columns=['Split'])\n    y_tr, y_val = train_y[x_train['Split'] != idx+1], train_y[x_train['Split'] == idx+1][x_train[x_train['Split'] == idx+1]['label_playerId'].isin(active_players.tolist())]\n    \n    oof1, model1, score1 = fit_lgbm(\n        x_tr, y_tr['target1'],\n        x_val, y_val['target1'],\n        params\n    )\n    oof2, model2, score2 = fit_lgbm(\n        x_tr, y_tr['target2'],\n        x_val, y_val['target2'],\n        params2\n    )\n    oof3, model3, score3 = fit_lgbm(\n        x_tr, y_tr['target3'],\n        x_val, y_val['target3'],\n        params\n    )\n    oof4, model4, score4 = fit_lgbm(\n        x_tr, y_tr['target4'],\n        x_val, y_val['target4'],\n        params2\n    )\n\n    score = (score1+score2+score3+score4) / 4\n    print(f'score: {score}')\n    cv += (score / NFOLDS)\n    joblib.dump(model1, 'lgb_{}_1.pkl'.format(idx+1))\n    joblib.dump(model2, 'lgb_{}_2.pkl'.format(idx+1))\n    joblib.dump(model3, 'lgb_{}_3.pkl'.format(idx+1))\n    joblib.dump(model4, 'lgb_{}_4.pkl'.format(idx+1))\n    \n\nprint(\"{} Folds Average CV: {}\".format(NFOLDS, cv))","metadata":{"execution":{"iopub.status.busy":"2021-07-28T15:20:13.600982Z","iopub.execute_input":"2021-07-28T15:20:13.601363Z","iopub.status.idle":"2021-07-28T15:22:50.770638Z","shell.execute_reply.started":"2021-07-28T15:20:13.601312Z","shell.execute_reply":"2021-07-28T15:22:50.767145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}