{"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","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:47:08.959507Z","iopub.execute_input":"2021-07-27T11:47:08.960173Z","iopub.status.idle":"2021-07-27T11:47:13.021638Z","shell.execute_reply.started":"2021-07-27T11:47:08.960057Z","shell.execute_reply":"2021-07-27T11:47:13.020104Z"},"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-27T11:47:13.024589Z","iopub.execute_input":"2021-07-27T11:47:13.025260Z","iopub.status.idle":"2021-07-27T11:47:35.239277Z","shell.execute_reply.started":"2021-07-27T11:47:13.025215Z","shell.execute_reply":"2021-07-27T11:47:35.238009Z"},"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-27T11:47:35.241584Z","iopub.execute_input":"2021-07-27T11:47:35.241925Z","iopub.status.idle":"2021-07-27T11:47:35.261854Z","shell.execute_reply.started":"2021-07-27T11:47:35.241891Z","shell.execute_reply":"2021-07-27T11:47:35.260491Z"},"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-27T11:47:35.263930Z","iopub.execute_input":"2021-07-27T11:47:35.264264Z","iopub.status.idle":"2021-07-27T11:47:35.281830Z","shell.execute_reply.started":"2021-07-27T11:47:35.264231Z","shell.execute_reply":"2021-07-27T11:47:35.280573Z"},"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-27T11:47:35.283638Z","iopub.execute_input":"2021-07-27T11:47:35.284270Z","iopub.status.idle":"2021-07-27T11:47:35.300705Z","shell.execute_reply.started":"2021-07-27T11:47:35.284221Z","shell.execute_reply":"2021-07-27T11:47:35.299377Z"},"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-27T11:47:35.302861Z","iopub.execute_input":"2021-07-27T11:47:35.303336Z","iopub.status.idle":"2021-07-27T11:47:35.313779Z","shell.execute_reply.started":"2021-07-27T11:47:35.303297Z","shell.execute_reply":"2021-07-27T11:47:35.312361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lastmonth_player_target_stats","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:47:35.316019Z","iopub.execute_input":"2021-07-27T11:47:35.316517Z","iopub.status.idle":"2021-07-27T11:47:35.394760Z","shell.execute_reply.started":"2021-07-27T11:47:35.316466Z","shell.execute_reply":"2021-07-27T11:47:35.393527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"team_scores","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:47:35.397486Z","iopub.execute_input":"2021-07-27T11:47:35.397865Z","iopub.status.idle":"2021-07-27T11:47:35.436846Z","shell.execute_reply.started":"2021-07-27T11:47:35.397834Z","shell.execute_reply":"2021-07-27T11:47:35.435591Z"},"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-27T11:47:35.439079Z","iopub.execute_input":"2021-07-27T11:47:35.439379Z","iopub.status.idle":"2021-07-27T11:47:36.308337Z","shell.execute_reply.started":"2021-07-27T11:47:35.439349Z","shell.execute_reply":"2021-07-27T11:47:36.307201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transactions['datetime_date'] = pd.to_datetime(transactions['date'], format=\"%Y%m%d\")\n# transactions['transaction_time'] = 1\n\n# tmp_df = transactions.copy()\n# tmp_df['datetime_date'] = transactions['datetime_date'] + pd.DateOffset(1)\n# tmp_df['transaction_time'] = 0\n# tmp_df['date'] = tmp_df['datetime_date'].dt.year * 10000 + tmp_df['datetime_date'].dt.month * 100 + tmp_df['datetime_date'].dt.day\n# transactions = pd.concat([transactions, tmp_df], axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:47:36.309851Z","iopub.execute_input":"2021-07-27T11:47:36.310178Z","iopub.status.idle":"2021-07-27T11:47:36.314890Z","shell.execute_reply.started":"2021-07-27T11:47:36.310148Z","shell.execute_reply":"2021-07-27T11:47:36.313774Z"},"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-27T11:47:36.316602Z","iopub.execute_input":"2021-07-27T11:47:36.316924Z","iopub.status.idle":"2021-07-27T11:47:37.135850Z","shell.execute_reply.started":"2021-07-27T11:47:36.316893Z","shell.execute_reply":"2021-07-27T11:47:37.134508Z"},"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']\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-27T11:47:37.137538Z","iopub.execute_input":"2021-07-27T11:47:37.137856Z","iopub.status.idle":"2021-07-27T11:47:37.149951Z","shell.execute_reply.started":"2021-07-27T11:47:37.137826Z","shell.execute_reply":"2021-07-27T11:47:37.148595Z"},"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', \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-27T11:47:37.151634Z","iopub.execute_input":"2021-07-27T11:47:37.151999Z","iopub.status.idle":"2021-07-27T11:47:37.170557Z","shell.execute_reply.started":"2021-07-27T11:47:37.151968Z","shell.execute_reply":"2021-07-27T11:47:37.169284Z"},"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-27T11:47:37.172140Z","iopub.execute_input":"2021-07-27T11:47:37.172551Z","iopub.status.idle":"2021-07-27T11:49:31.678258Z","shell.execute_reply.started":"2021-07-27T11:47:37.172520Z","shell.execute_reply":"2021-07-27T11:49:31.677080Z"},"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-27T11:49:31.679773Z","iopub.execute_input":"2021-07-27T11:49:31.680079Z","iopub.status.idle":"2021-07-27T11:49:35.167243Z","shell.execute_reply.started":"2021-07-27T11:49:31.680049Z","shell.execute_reply":"2021-07-27T11:49:35.166102Z"},"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-27T11:49:35.168624Z","iopub.execute_input":"2021-07-27T11:49:35.168969Z","iopub.status.idle":"2021-07-27T11:49:42.984202Z","shell.execute_reply.started":"2021-07-27T11:49:35.168936Z","shell.execute_reply":"2021-07-27T11:49:42.983105Z"},"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-27T11:49:42.985646Z","iopub.execute_input":"2021-07-27T11:49:42.985977Z","iopub.status.idle":"2021-07-27T11:50:43.548031Z","shell.execute_reply.started":"2021-07-27T11:49:42.985943Z","shell.execute_reply":"2021-07-27T11:50:43.546945Z"},"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-27T11:50:43.549726Z","iopub.execute_input":"2021-07-27T11:50:43.550033Z","iopub.status.idle":"2021-07-27T11:50:46.748133Z","shell.execute_reply.started":"2021-07-27T11:50:43.550004Z","shell.execute_reply":"2021-07-27T11:50:46.746833Z"},"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-27T11:50:46.750827Z","iopub.execute_input":"2021-07-27T11:50:46.751321Z","iopub.status.idle":"2021-07-27T11:50:46.759144Z","shell.execute_reply.started":"2021-07-27T11:50:46.751267Z","shell.execute_reply":"2021-07-27T11:50:46.757463Z"},"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': 2000,\n 'learning_rate': 0.1,\n 'random_state': 208,\n \"num_leaves\": 250\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        params\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        params\n    )\n\n    score = (score1+score2+score3+score4) / 4\n    print(f'score: {score}')\n    cv += (score / NFOLDS)\n    model1_list.append(model1)\n    model2_list.append(model2)\n    model3_list.append(model3)\n    model4_list.append(model4)\n\nprint(\"{} Folds Average CV: {}\".format(NFOLDS, cv))","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:50:46.760994Z","iopub.execute_input":"2021-07-27T11:50:46.761383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Importance","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)\n\n# sorted(zip(clf.feature_importances_, X.columns), reverse=True)\nfeature_imp = pd.DataFrame(sorted(zip(model1.feature_importances_,train_X.columns)), columns=['Value','Feature'])\n\nplt.figure(figsize=(20, 10))\nsns.barplot(x=\"Value\", y=\"Feature\", data=feature_imp.sort_values(by=\"Value\", ascending=False))\nplt.title('LightGBM Features with target 1')\nplt.tight_layout()\nplt.show()\nplt.savefig('lgbm_importances-01.png')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_imp = pd.DataFrame(sorted(zip(model2.feature_importances_,train_X.columns)), columns=['Value','Feature'])\n\nplt.figure(figsize=(20, 10))\nsns.barplot(x=\"Value\", y=\"Feature\", data=feature_imp.sort_values(by=\"Value\", ascending=False))\nplt.title('LightGBM Features with target 2')\nplt.tight_layout()\nplt.show()\nplt.savefig('lgbm_importances-02.png')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_imp = pd.DataFrame(sorted(zip(model3.feature_importances_,train_X.columns)), columns=['Value','Feature'])\n\nplt.figure(figsize=(20, 10))\nsns.barplot(x=\"Value\", y=\"Feature\", data=feature_imp.sort_values(by=\"Value\", ascending=False))\nplt.title('LightGBM Features with target 3')\nplt.tight_layout()\nplt.show()\nplt.savefig('lgbm_importances-03.png')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_imp = pd.DataFrame(sorted(zip(model4.feature_importances_,train_X.columns)), columns=['Value','Feature'])\n\nplt.figure(figsize=(20, 10))\nsns.barplot(x=\"Value\", y=\"Feature\", data=feature_imp.sort_values(by=\"Value\", ascending=False))\nplt.title('LightGBM Features with target 4')\nplt.tight_layout()\nplt.show()\nplt.savefig('lgbm_importances-04.png')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Calculate Cumulative data","metadata":{}},{"cell_type":"code","source":"cumulative_hits = {}\ncumulative_atBats = {}\ncumulative_earnedRuns = {}\ncumulative_inningsPitched = {}\ncumulative_totalBases = {}\ncumulative_baseOnBalls = {}\ncumulative_hitByPitch = {}\ncumulative_sacFlies = {}\ncumulative_baseOnBallsPitching = {}\ncumulative_hitByPitchPitching = {}\ncumulative_hitsPitching = {}\ncumulative_hr = {}\ncumulative_rbi = {}\ncumulative_win = {}\ncumulative_loss = {}\ncumulative_k = {}\ncumulative_save = {}\ncumulative_bs = {}\ncumulative_h_streak = {}\n# cumulative_hr_streak = {}\n# cumulative_base_streak = {}\n\nfor idx, row in tqdm_notebook(cumulative_data.iterrows()):\n    if (idx) % 100000 == 0:\n        print(idx)\n    date = str(row['date'])\n    playerId = row['playerId']\n    if date[:4] == \"2021\":\n        cumulative_hits[playerId] = row['cumulative_hits']\n        cumulative_atBats[playerId] = row['cumulative_atBats']\n        cumulative_earnedRuns[playerId] = row['cumulative_earnedRuns']\n        cumulative_inningsPitched[playerId] = row['cumulative_inningsPitched']\n        cumulative_totalBases[playerId] = row['cumulative_totalBases']\n        cumulative_baseOnBalls[playerId] = row['cumulative_baseOnBalls']\n        cumulative_hitByPitch[playerId] = row['cumulative_hitByPitch']\n        cumulative_sacFlies[playerId] = row['cumulative_sacFlies']\n        cumulative_baseOnBallsPitching[playerId] = row['cumulative_baseOnBallsPitching']\n        cumulative_hitByPitchPitching[playerId] = row['cumulative_hitByPitchPitching']\n        cumulative_hitsPitching[playerId] = row['cumulative_hitsPitching']\n        cumulative_hr[playerId] = row['cumulative_hr']\n        cumulative_rbi[playerId] = row['cumulative_rbi']\n        cumulative_win[playerId] = row['cumulative_win']\n        cumulative_loss[playerId] = row['cumulative_loss']\n        cumulative_k[playerId] = row['cumulative_k']\n        cumulative_save[playerId] = row['cumulative_save']\n        cumulative_bs[playerId] = row['cumulative_bs']\n        cumulative_h_streak[playerId] = row['cumulative_h_streak']\n#         cumulative_hr_streak[playerId] = row['cumulative_hr_streak']\n#         cumulative_base_streak[playerId] = row['cumulative_base_streak']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cumulative_h_streak","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players_cols = ['playerId', 'primaryPositionName']\nrosters_cols = ['playerId', 'teamId', 'status']\nscores_cols = ['playerId', 'flyOuts', 'gamePk',\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', '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']\ntrans_cols = ['playerId', 'typeDesc']\nawards_cols = ['playerId', 'awardId']\ngames_cols = ['gamePk', 'homeId', 'awayId', 'dayNight', 'gameType']\nstandings_cols = ['streakCode', 'pct', 'leagueRank', 'teamId']\nteam_scores_cols = ['teamId', 'gamePk', 'team_runsScored', 'team_runsPitching']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null = np.nan\ntrue = True\nfalse = False\n# player_target_stats = pd.read_csv(\"../input/player-target-stats/player_target_stats.csv\")\nenv = mlb.make_env() # initialize the environment\niter_test = env.iter_test() # iterator which loops over each date in test set","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nplayer_cumulative_stats_dict = torch.load('../input/recently-player-stats/player_stats_last7.pkl')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, (test_df, sample_prediction_df) in enumerate(iter_test): # make predictions here\n    \n    sample_prediction_df = sample_prediction_df.reset_index(drop=True)\n    \n    # creat dataset\n    sample_prediction_df['playerId'] = sample_prediction_df['date_playerId']\\\n                                        .map(lambda x: int(x.split('_')[1]))\n    sample_prediction_df['date'] = pd.to_datetime(sample_prediction_df['date_playerId']\\\n                                        .map(lambda x: int(x.split('_')[0])), format=\"%Y%m%d\") - pd.DateOffset(1)\n        \n    # Dealing with missing values\n    if test_df['rosters'].iloc[0] == test_df['rosters'].iloc[0]:\n        test_rosters = pd.DataFrame(eval(test_df['rosters'].iloc[0]))\n    else:\n        test_rosters = pd.DataFrame({'playerId': sample_prediction_df['playerId']})\n        for col in rosters.columns:\n            if col == 'playerId': continue\n            test_rosters[col] = np.nan\n            \n    if test_df['playerBoxScores'].iloc[0] == test_df['playerBoxScores'].iloc[0]:\n        test_scores = pd.DataFrame(eval(test_df['playerBoxScores'].iloc[0]))\n    else:\n        test_scores = pd.DataFrame({'playerId': sample_prediction_df['playerId']})\n        for col in scores.columns:\n            if col == 'playerId': continue\n            test_scores[col] = np.nan\n            \n#     if test_df['teamBoxScores'].iloc[0] == test_df['teamBoxScores'].iloc[0]:\n#         test_team_scores = pd.DataFrame(eval(test_df['teamBoxScores'].iloc[0]))\n#     else:\n#         test_team_scores = pd.DataFrame({'playerId': sample_prediction_df['playerId']})\n#         for col in team_scores.columns:\n#             if col == 'playerId': continue\n#             test_team_scores[col] = np.nan\n    \n    if test_df['transactions'].iloc[0] == test_df['transactions'].iloc[0]:\n        test_trans = pd.DataFrame(eval(test_df['transactions'].iloc[0]))\n        test_trans['transaction_time'] = 1\n    else:\n        test_trans = pd.DataFrame({'playerId': sample_prediction_df['playerId']})\n        for col in transactions.columns:\n            if col == 'playerId': continue\n            test_trans[col] = np.nan\n    \n    test_trans = test_trans.drop_duplicates(subset=['playerId'])\n#     pre_trans_old = test_trans.copy()\n#     if i != 0:\n#         test_trans = pd.concat([test_trans, pre_trans], axis=0)\n#     test_trans = test_trans.drop_duplicates(subset=['playerId'])\n#     if pre_trans_old.loc[0, 'transaction_time'] == 1:\n#         pre_trans_old['transaction_time'] = 0\n#     pre_trans = pre_trans_old.copy()\n    \n    if test_df['awards'].iloc[0] == test_df['awards'].iloc[0]:\n        test_awards = pd.DataFrame(eval(test_df['awards'].iloc[0]))\n        test_awards['awards_time'] = 1\n    else:\n        test_awards = pd.DataFrame({'playerId': sample_prediction_df['playerId']})\n        for col in awards.columns:\n            if col == 'playerId': continue\n            test_awards[col] = np.nan\n    test_awards = test_awards.drop_duplicates(subset=['playerId'])\n    \n    if test_df['games'].iloc[0] == test_df['games'].iloc[0]:\n        test_games = pd.DataFrame(eval(test_df['games'].iloc[0]))\n    else:\n        test_games = pd.DataFrame({'playerId': sample_prediction_df['playerId']})\n        for col in games.columns:\n            if col == 'playerId': continue\n            test_games[col] = np.nan\n\n    if test_df['standings'].iloc[0] == test_df['standings'].iloc[0]:\n        test_standings = pd.DataFrame(eval(test_df['standings'].iloc[0]))\n    else:\n        test_standings = pd.DataFrame({'playerId': sample_prediction_df['playerId']})\n        for col in standings.columns:\n            if col == 'playerId': continue\n            test_standings[col] = np.nan\n            \n    test_scores = test_scores.groupby('playerId').sum().reset_index()\n    test = sample_prediction_df[['playerId', 'date']].copy()\n\n    test = test.merge(players[players_cols], on='playerId', how='left')\n    test = test.merge(test_rosters[rosters_cols], on='playerId', how='left')\n    test = test.merge(test_scores[scores_cols], on='playerId', how='left')\n#     test = test.merge(test_team_scores[team_scores_cols], on=['teamId', 'gamePk'], how='left')\n    test = test.merge(test_games[games_cols], on='gamePk', how='left')\n    test = test.merge(test_standings[standings_cols], on='teamId', how='left')\n    \n    test['year'] = test['date'].dt.year\n    test['month'] = test['date'].dt.month\n    test['days'] = test['date'].dt.day\n    test = test.merge(lastmonth_player_target_stats[last_month_stats_cols], how='inner', left_on=[\"playerId\", 'year', 'month'],right_on=[\"playerId\", 'year', 'month'])\n    test = test.merge(inseason_player_target_stats[stats_cols], how='inner', left_on=[\"playerId\"],right_on=[\"playerId\"])\n\n    test = test.merge(test_trans[trans_cols], on='playerId', how='left')\n    test = test.merge(test_awards[awards_cols], on='playerId', how='left')\n    test = test.merge(seasons, on=['year'], how='left')\n    test.loc[test['gamePk'] > 700000, 'gameType'] = 'R'\n#     test.loc[test['gamePk'] > 700000, 'dayNight'] = 'day_night'\n    test['DaysAfterRegularSeason'] = (test['date'] - test['regularSeasonStartDate']).dt.days\n\n    avg_list = []\n    era_list = []\n    whip_list = []\n    ops_list = []\n    obp_list = []\n    slg_list = []\n    hit_list = []\n    hr_list = []\n    rbi_list = []\n    atBats_list = []\n    inningsPitched_list = []\n    win_list = []\n    loss_list = []\n    save_list = []\n    bs_list = []\n    h_streak_list = []\n\n    last7_avg_list = []\n    last5_era_list = []\n    last5_whip_list = []\n    last7_ops_list = []\n#     last7_obp_list = []\n#     last7_slg_list = []\n    last7_hit_list = []\n    last7_hr_list = []\n    last7_rbi_list = []\n    last7_atBats_list = []\n#     inningsPitched_list = []\n    last5_win_list = []\n    last5_loss_list = []\n#     save_list = []\n#     bs_list = []\n    \n    for idx, row in (test.iterrows()):\n        playerId = row['playerId']\n        if not np.isnan(row['hits']) and row['gameType'] != 'A':\n            cumulative_hits[playerId] += row['hits']\n            cumulative_atBats[playerId] += row['atBats']\n            cumulative_totalBases[playerId] += row['totalBases']\n            cumulative_baseOnBalls[playerId] += row['baseOnBalls']\n            cumulative_hitByPitch[playerId] += row['hitByPitch']\n            cumulative_sacFlies[playerId] += row['sacFlies']\n            cumulative_hr[playerId] += row['homeRuns']\n            cumulative_rbi[playerId] += row['rbi']\n            \n            player_cumulative_stats_dict[playerId]['cumulative_hits'].append(row['hits'])\n            player_cumulative_stats_dict[playerId]['cumulative_atBats'].append(row['atBats'])\n            player_cumulative_stats_dict[playerId]['cumulative_totalBases'].append(row['totalBases'])\n            player_cumulative_stats_dict[playerId]['cumulative_hitByPitch'].append(row['hitByPitch'])\n            player_cumulative_stats_dict[playerId]['cumulative_baseOnBalls'].append(row['baseOnBalls'])\n            player_cumulative_stats_dict[playerId]['cumulative_sacFlies'].append(row['sacFlies'])\n            player_cumulative_stats_dict[playerId]['cumulative_hr'].append(row['homeRuns'])\n            player_cumulative_stats_dict[playerId]['cumulative_rbi'].append(row['rbi'])\n            \n            if len(player_cumulative_stats_dict[playerId]['cumulative_hits']) > 7:\n                player_cumulative_stats_dict[playerId]['cumulative_hits'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_atBats'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_totalBases'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_hitByPitch'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_baseOnBalls'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_sacFlies'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_hr'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_rbi'].pop(0)\n\n            if row['hits'] != 0:\n                cumulative_h_streak[playerId] += 1\n            else:\n                if not np.isnan(cumulative_h_streak[playerId]):\n                    cumulative_h_streak[playerId] = 0\n            \n#             if row['homeRuns'] != 0:\n#                 cumulative_hr_streak[playerId] += 1\n#             else:\n#                 cumulative_hr_streak[playerId] = 0\n            \n#             if row['hits'] != 0 or row['baseOnBalls'] != 0 or row['hitByPitch'] != 0:\n#                 cumulative_base_streak[playerId] += 1\n#             else:\n#                 cumulative_base_streak[playerId] = 0\n            \n        if not np.isnan(row['earnedRuns']):\n            inningsPitched = row['inningsPitched']\n            if (str(inningsPitched)[-1]) == '1':\n                inningsPitched = int(str(inningsPitched).split('.')[0]) + 1/3\n            elif (str(inningsPitched)[-1]) == '2':\n                inningsPitched = int(str(inningsPitched).split('.')[0]) + 2/3\n            \n            cumulative_earnedRuns[playerId] += row['earnedRuns']\n            cumulative_inningsPitched[playerId] += inningsPitched\n            cumulative_hitByPitchPitching[playerId] += row['hitByPitchPitching']\n            cumulative_hitsPitching[playerId] += row['hitsPitching']\n            cumulative_baseOnBallsPitching[playerId] += row['baseOnBallsPitching']\n            cumulative_win[playerId] += row['winsPitching']\n            cumulative_loss[playerId] += row['lossesPitching']\n            cumulative_k[playerId] += row['strikeOutsPitching']\n            cumulative_bs[playerId] += row['blownSaves']\n            cumulative_save[playerId] += row['saves']\n            \n            if playerId != 660271 or inningsPitched > 0:\n                player_cumulative_stats_dict[playerId]['cumulative_earnedRuns'].append(row['earnedRuns'])\n                player_cumulative_stats_dict[playerId]['cumulative_inningsPitched'].append(inningsPitched)\n                player_cumulative_stats_dict[playerId]['cumulative_baseOnBallsPitching'].append(row['baseOnBallsPitching'])\n                player_cumulative_stats_dict[playerId]['cumulative_hitByPitchPitching'].append(row['hitByPitchPitching'])\n                player_cumulative_stats_dict[playerId]['cumulative_hitsPitching'].append(row['hitsPitching'])\n                player_cumulative_stats_dict[playerId]['cumulative_saves'].append(row['saves'])\n                player_cumulative_stats_dict[playerId]['cumulative_blownSaves'].append(row['blownSaves'])\n                player_cumulative_stats_dict[playerId]['cumulative_win'].append(row['winsPitching'])\n                player_cumulative_stats_dict[playerId]['cumulative_losses'].append(row['lossesPitching'])\n            \n            if len(player_cumulative_stats_dict[playerId]['cumulative_earnedRuns']) > 5:\n                player_cumulative_stats_dict[playerId]['cumulative_earnedRuns'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_inningsPitched'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_baseOnBallsPitching'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_hitByPitchPitching'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_hitsPitching'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_saves'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_blownSaves'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_win'].pop(0)\n                player_cumulative_stats_dict[playerId]['cumulative_losses'].pop(0)\n\n            \n        avg_list.append(cumulative_hits[playerId] / cumulative_atBats[playerId])\n        era_list.append(cumulative_earnedRuns[playerId] / cumulative_inningsPitched[playerId] * 9)\n        slg = cumulative_totalBases[playerId] / cumulative_atBats[playerId]\n        obp = (cumulative_hits[playerId] + cumulative_baseOnBalls[playerId] + cumulative_hitByPitch[playerId]) / (cumulative_atBats[playerId] + cumulative_baseOnBalls[playerId] +  cumulative_hitByPitch[playerId] + cumulative_sacFlies[playerId])\n        ops_list.append(slg + obp)\n        whip = (cumulative_baseOnBallsPitching[playerId] + cumulative_hitByPitchPitching[playerId] + cumulative_hitsPitching[playerId]) / cumulative_inningsPitched[playerId]\n        whip_list.append(whip)\n        hr_list.append(cumulative_hr[playerId])\n        hit_list.append(cumulative_hits[playerId])\n        rbi_list.append(cumulative_rbi[playerId])\n        inningsPitched_list.append(cumulative_inningsPitched[playerId])\n        atBats_list.append(cumulative_atBats[playerId])\n        win_list.append(cumulative_win[playerId])\n        loss_list.append(cumulative_loss[playerId])\n        save_list.append(cumulative_save[playerId])\n        bs_list.append(cumulative_bs[playerId])\n        h_streak_list.append(cumulative_h_streak[playerId])\n        \n        if playerId == 660271:\n            last7_avg_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_hits']) / sum(player_cumulative_stats_dict[playerId]['cumulative_atBats']))\n    #         era_list.append(cumulative_earnedRuns[playerId] / cumulative_inningsPitched[playerId] * 9)\n            slg = sum(player_cumulative_stats_dict[playerId]['cumulative_totalBases']) / sum(player_cumulative_stats_dict[playerId]['cumulative_atBats'])\n            obp = (sum(player_cumulative_stats_dict[playerId]['cumulative_hits']) + sum(player_cumulative_stats_dict[playerId]['cumulative_baseOnBalls']) + \n                    sum(player_cumulative_stats_dict[playerId]['cumulative_hitByPitch'])) / (sum(player_cumulative_stats_dict[playerId]['cumulative_atBats']) + \n                    sum(player_cumulative_stats_dict[playerId]['cumulative_baseOnBalls']) + sum(player_cumulative_stats_dict[playerId]['cumulative_hitByPitch']) +\n                    sum(player_cumulative_stats_dict[playerId]['cumulative_sacFlies']))\n            last7_ops_list.append(slg + obp)\n    #         whip = (cumulative_baseOnBallsPitching[playerId] + cumulative_hitByPitchPitching[playerId] + cumulative_hitsPitching[playerId]) / cumulative_inningsPitched[playerId]\n    #         whip_list.append(whip)\n            last7_hr_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_hr']))\n            last7_hit_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_hits']))\n            last7_rbi_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_rbi']))\n    #         inningsPitched_list.append(cumulative_inningsPitched[playerId])\n            last7_atBats_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_atBats']))\n        \n            last5_era_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_earnedRuns']) / sum(player_cumulative_stats_dict[playerId]['cumulative_inningsPitched']) * 9)\n            last5_whip_list.append((sum(player_cumulative_stats_dict[playerId]['cumulative_baseOnBallsPitching']) + sum(player_cumulative_stats_dict[playerId]['cumulative_hitsPitching']) + \n                    sum(player_cumulative_stats_dict[playerId]['cumulative_hitByPitchPitching'])) / sum(player_cumulative_stats_dict[playerId]['cumulative_inningsPitched']))\n            last5_win_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_win']))\n            last5_loss_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_losses']))\n            \n        elif row['primaryPositionName'] == 'Pitcher':\n            last7_avg_list.append(float('nan'))\n            last7_ops_list.append(float('nan'))\n            last7_hr_list.append(float('nan'))\n            last7_hit_list.append(float('nan'))\n            last7_rbi_list.append(float('nan'))\n            last7_atBats_list.append(float('nan'))\n            \n            if sum(player_cumulative_stats_dict[playerId]['cumulative_inningsPitched']) == 0:\n                last5_era_list.append(float('nan'))\n                last5_whip_list.append(float('nan'))\n                last5_win_list.append(float('nan'))\n                last5_loss_list.append(float('nan'))\n            else:\n                last5_era_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_earnedRuns']) / sum(player_cumulative_stats_dict[playerId]['cumulative_inningsPitched']) * 9)\n                last5_whip_list.append((sum(player_cumulative_stats_dict[playerId]['cumulative_baseOnBallsPitching']) + sum(player_cumulative_stats_dict[playerId]['cumulative_hitsPitching']) + \n                        sum(player_cumulative_stats_dict[playerId]['cumulative_hitByPitchPitching'])) / sum(player_cumulative_stats_dict[playerId]['cumulative_inningsPitched']))\n                last5_win_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_win']))\n                last5_loss_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_losses']))\n        else:\n            last5_era_list.append(float('nan'))\n            last5_whip_list.append(float('nan'))\n            last5_win_list.append(float('nan'))\n            last5_loss_list.append(float('nan'))\n            \n            if sum(player_cumulative_stats_dict[playerId]['cumulative_atBats']) == 0:\n                last7_avg_list.append(float('nan'))\n                last7_ops_list.append(float('nan'))\n                last7_hr_list.append(float('nan'))\n                last7_hit_list.append(float('nan'))\n                last7_rbi_list.append(float('nan'))\n                last7_atBats_list.append(float('nan'))\n                \n            else:\n                last7_avg_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_hits']) / sum(player_cumulative_stats_dict[playerId]['cumulative_atBats']))\n    #         era_list.append(cumulative_earnedRuns[playerId] / cumulative_inningsPitched[playerId] * 9)\n                slg = sum(player_cumulative_stats_dict[playerId]['cumulative_totalBases']) / sum(player_cumulative_stats_dict[playerId]['cumulative_atBats'])\n                obp = (sum(player_cumulative_stats_dict[playerId]['cumulative_hits']) + sum(player_cumulative_stats_dict[playerId]['cumulative_baseOnBalls']) + \n                        sum(player_cumulative_stats_dict[playerId]['cumulative_hitByPitch'])) / (sum(player_cumulative_stats_dict[playerId]['cumulative_atBats']) + \n                        sum(player_cumulative_stats_dict[playerId]['cumulative_baseOnBalls']) + sum(player_cumulative_stats_dict[playerId]['cumulative_hitByPitch']) +\n                        sum(player_cumulative_stats_dict[playerId]['cumulative_sacFlies']))\n                last7_ops_list.append(slg + obp)\n        #         whip = (cumulative_baseOnBallsPitching[playerId] + cumulative_hitByPitchPitching[playerId] + cumulative_hitsPitching[playerId]) / cumulative_inningsPitched[playerId]\n        #         whip_list.append(whip)\n                last7_hr_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_hr']))\n                last7_hit_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_hits']))\n                last7_rbi_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_rbi']))\n        #         inningsPitched_list.append(cumulative_inningsPitched[playerId])\n                last7_atBats_list.append(sum(player_cumulative_stats_dict[playerId]['cumulative_atBats']))\n\n        \n    test['avg'] = avg_list\n    test['era'] = era_list\n    test['whip'] = whip_list\n    test['ops'] = ops_list\n    test['cumulative_hits'] = hit_list\n    test['cumulative_hr'] = hr_list\n    test['cumulative_rbi'] = rbi_list\n    \n    test['cumulative_win'] = win_list\n    test['cumulative_loss'] = loss_list\n    test['pitch_win_pct'] = test['cumulative_win'] / (test['cumulative_win'] + test['cumulative_loss'])\n    test['cumulative_save'] = save_list\n    test['cumulative_bs'] = bs_list\n    \n    test['cumulative_h_streak'] = h_streak_list\n    \n    test['last7_avg'] = last7_avg_list\n    test['last7_ops'] = last7_ops_list\n    test['last7_cumulative_hits'] = last7_hit_list\n    test['last7_cumulative_hr'] = last7_hr_list\n    test['last7_cumulative_rbi'] = last7_rbi_list\n    \n    test['last5_era'] = last5_era_list\n    test['last5_whip'] = last5_whip_list\n    test['last5_cumulative_win'] = last5_win_list\n    test['last5_cumulative_loss'] = last5_loss_list\n\n#     test['cumulative_hr_streak'] = hr_streak_list\n#     test['cumulative_base_streak'] = base_streak_list\n    \n    test['label_playerId'] = test['playerId'].map(player2num)\n    test['label_primaryPositionName'] = test['primaryPositionName'].map(position2num)\n    test['label_teamId'] = test['teamId'].map(teamid2num)\n    test['label_homeId'] = test['homeId'].map(teamid2num)\n    test['label_awayId'] = test['awayId'].map(teamid2num)\n    test['label_status'] = test['status'].map(status2num)\n    test['label_typeDesc'] = test['typeDesc'].map(transdesc2num)\n    test['label_awardId'] = test['awardId'].map(awardid2num)\n    test['label_gameType'] = test['gameType'].map(awardid2num)\n    test['label_dayNight'] = test['dayNight'].map({'day': 0, 'night': 1})\n    test = test.drop_duplicates('playerId')\n    test['streakCode'] = test['streakCode'].fillna('W0')\n    test['WinLose'] = test['streakCode'].str[0]\n    test['WinLose'] = test['WinLose'].map({'L': 0, 'W': 1})\n    test['Streak'] = test['streakCode'].str[1].astype(int)\n    test['pct'] = test['pct'].astype(float)\n    test['Split'] = 0\n#     test['playoff_avg'] = np.nan\n#     test['playoff_ops'] = np.nan\n#     test['cumulative_playoff_hits'] = np.nan\n#     test['cumulative_playoff_hr'] = np.nan\n#     test['cumulative_playoff_rbi'] = np.nan\n\n    \n    test_X_1 = test[feature_cols].drop(columns=['Split']).values\n\n    # predict\n    for i in range(NFOLDS):\n        if i == 0:\n            pred1 = model1_list[i].predict(test_X_1) / NFOLDS\n            pred2 = model2_list[i].predict(test_X_1) / NFOLDS\n            pred3 = model3_list[i].predict(test_X_1) / NFOLDS\n            pred4 = model4_list[i].predict(test_X_1) / NFOLDS\n        else:\n            pred1 += model1_list[i].predict(test_X_1) / NFOLDS\n            pred2 += model2_list[i].predict(test_X_1) / NFOLDS\n            pred3 += model3_list[i].predict(test_X_1) / NFOLDS\n            pred4 += model4_list[i].predict(test_X_1) / NFOLDS\n    \n    # merge submission\n    sample_prediction_df['target1'] = np.clip(pred1, 0, 100)\n    sample_prediction_df['target2'] = np.clip(pred2, 0, 100)\n    sample_prediction_df['target3'] = np.clip(pred3, 0, 100)\n    sample_prediction_df['target4'] = np.clip(pred4, 0, 100)\n    sample_prediction_df = sample_prediction_df.fillna(0.)\n    del sample_prediction_df['playerId']\n    del sample_prediction_df['date']\n    \n    env.predict(sample_prediction_df)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_prediction_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}