{"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":"#### Credit to @columbia2131 - I started with his notebook and then added an external data set with descriptive statistics of the targets for each player and also added unique params for each target model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true}},{"cell_type":"markdown","source":"## About Dataset","metadata":{}},{"cell_type":"markdown","source":"## Training","metadata":{"execution":{"iopub.status.busy":"2021-06-16T09:14:33.869464Z","iopub.execute_input":"2021-06-16T09:14:33.869905Z","iopub.status.idle":"2021-06-16T09:14:33.874766Z","shell.execute_reply.started":"2021-06-16T09:14:33.869879Z","shell.execute_reply":"2021-06-16T09:14:33.873097Z"}}},{"cell_type":"code","source":"import warnings \nwarnings.filterwarnings('ignore')\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\nimport lightgbm as lgbm\nimport mlb\nimport gc","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:15.107491Z","iopub.execute_input":"2021-07-27T15:31:15.107900Z","iopub.status.idle":"2021-07-27T15:31:15.113911Z","shell.execute_reply.started":"2021-07-27T15:31:15.107858Z","shell.execute_reply":"2021-07-27T15:31:15.113227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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')\n\nrosters = pd.read_pickle(TRAIN_DIR / 'rosters_train.pkl')\ntargets = pd.read_pickle(TRAIN_DIR / 'nextDayPlayerEngagement_train.pkl')\nfollowers = pd.read_pickle(TRAIN_DIR / 'playerTwitterFollowers_train.pkl')\nteam_followers = pd.read_pickle(TRAIN_DIR / 'teamTwitterFollowers_train.pkl')\nteam_followers = team_followers.rename(columns={'numberOfFollowers': 'teamFollowers'})\n\nplayers_id_map=dict(zip(players.playerId.values,players.playerName.values))","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:15.115573Z","iopub.execute_input":"2021-07-27T15:31:15.115945Z","iopub.status.idle":"2021-07-27T15:31:15.738582Z","shell.execute_reply.started":"2021-07-27T15:31:15.115917Z","shell.execute_reply":"2021-07-27T15:31:15.737554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_cols = ['playerId', 'target1', 'target2', 'target3', 'target4', 'date']\nplayers_cols = ['playerId', 'primaryPositionName']\nroster_cols = ['playerId', 'teamId', 'status', 'date']\nfollowers_cols = ['playerId', 'numberOfFollowers', 'date']\nteamfollowers_cols = ['teamId', 'teamFollowers', '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','target2_mean',\n 'target2_median',\n 'target2_std',\n 'target2_min',\n 'target2_max',\n 'target2_prob','target3_mean',\n 'target3_median',\n 'target3_std',\n 'target3_min',\n 'target3_max',\n 'target3_prob','target4_mean',\n 'target4_median',\n 'target4_std',\n 'target4_min',\n 'target4_max',\n 'target4_prob']#'numberOfFollowers','teamFollowers'","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2021-07-27T15:31:15.739878Z","iopub.execute_input":"2021-07-27T15:31:15.740408Z","iopub.status.idle":"2021-07-27T15:31:15.753876Z","shell.execute_reply.started":"2021-07-27T15:31:15.740375Z","shell.execute_reply":"2021-07-27T15:31:15.752625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_target_stats = pd.read_csv(\"../input/player-target-stats/player_target_stats.csv\")\nwOBA_weights=pd.read_csv('../input/mlb-img/FanGraphs Leaderboard.csv')\n\ndata_names=player_target_stats.columns.values.tolist()\n#data_names","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:15.755256Z","iopub.execute_input":"2021-07-27T15:31:15.755572Z","iopub.status.idle":"2021-07-27T15:31:15.793445Z","shell.execute_reply.started":"2021-07-27T15:31:15.755542Z","shell.execute_reply":"2021-07-27T15:31:15.792387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boxscores_df=pd.read_csv('../input/mlb-img/boxscores_df.csv')\nboxscores_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:15.794896Z","iopub.execute_input":"2021-07-27T15:31:15.795223Z","iopub.status.idle":"2021-07-27T15:31:18.009573Z","shell.execute_reply.started":"2021-07-27T15:31:15.795192Z","shell.execute_reply":"2021-07-27T15:31:18.008588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Sabermetrics:\n    def __init__(self,df,method,wOBA_weights,grpby_elem,season,mode=None):\n        \n        self.df=df\n        self.method=method\n        self.wOBA_weights=wOBA_weights\n        self.grpby_elem=grpby_elem\n        self.season=season\n        self.mode=mode\n\n    def __get_battingstats__(self,season):        \n        epsilon=1e-10\n        sabermetrics_bat=pd.DataFrame()\n        sabermetrics_bat['playerId']=self.df.playerId.unique()\n        temp_scores=self.df[self.df.year==season]\n        if self.mode=='all':\n            print(\"zzsdsdd\")\n            temp_scores=self.df\n            \n            \n        tmp_dict=dict(zip(self.df.playerId.values,self.df.playerName.values))\n        wOBA_2018=self.wOBA_weights[self.wOBA_weights.Season==season]\n        sabermetrics_bat['playerName']=sabermetrics_bat['playerId'].map(tmp_dict)\n        \n        sabermetrics_bat['games_played']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).gamesPlayedBatting.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['num_plate_appearances']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).plateAppearances.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['total_homeruns_scored']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).homeRuns.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['net_stolen_bases']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).stolenBases.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['net_caught_stealing']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).caughtStealing.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['walk_percent']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).baseOnBalls.sum())\n        sabermetrics_bat['walk_percent']=sabermetrics_bat['walk_percent'].values/(sabermetrics_bat['num_plate_appearances'].values+epsilon)\n        sabermetrics_bat['tot_groundouts']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).groundOuts.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['tot_flyouts']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).flyOuts.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['tot_strikeouts']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).strikeOuts.sum())\n        sabermetrics_bat['strikeout_percent']=sabermetrics_bat['tot_strikeouts'].values/(sabermetrics_bat['num_plate_appearances'].values+epsilon)\n        sabermetrics_bat['tot_singles']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).singles.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['tot_doubles']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).doubles.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['tot_triples']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).triples.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['tot_hits']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).hits.sum())\n        sabermetrics_bat['tot_hitsbypitch']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).hitByPitch.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['tot_attbat']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).atBats.sum())\n        sabermetrics_bat['tot_sacflies']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).sacFlies.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['tot_sacbunts']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).sacBunts.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['tot_baseonball']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).baseOnBalls.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['tot_intentionalwalks']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).intentionalWalks.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['tot_unintentionalwalks']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).unintentional_walks.agg({self.method}).to_dict()[self.method])\n        sabermetrics_bat['batting_average']=sabermetrics_bat['tot_hits'].values/(sabermetrics_bat['tot_attbat'].values+epsilon)\n        sabermetrics_bat['tot_GIDP']=sabermetrics_bat[self.grpby_elem].map(temp_scores.groupby(self.grpby_elem).groundIntoDoublePlay.agg({self.method}).to_dict()[self.method])\n\n        # BABIP (Batting Average on Balls in Play): The rate at which the batter gets a hit when he puts the ball in play, calculated as (H-HR)/(AB-K-HR+SF).\n        sabermetrics_bat['BABIP']=sabermetrics_bat['tot_hits'].values-sabermetrics_bat['total_homeruns_scored'].values\n        temp=sabermetrics_bat['tot_attbat'].values+sabermetrics_bat['tot_sacflies'].values-sabermetrics_bat['tot_strikeouts'].values-sabermetrics_bat['total_homeruns_scored'].values\n        sabermetrics_bat['BABIP']=sabermetrics_bat['BABIP'].values/(temp+epsilon)\n\n        #OBP (On Base Percentage): Rate at which the batter reaches base, calculated as (H+BB+HBP)/(AB+BB+HBP+SF).\n        sabermetrics_bat['OBP']=sabermetrics_bat['tot_hits'].values+sabermetrics_bat['tot_baseonball'].values+sabermetrics_bat['tot_hitsbypitch'].values\n        temp=sabermetrics_bat['tot_attbat'].values+sabermetrics_bat['tot_sacflies'].values+sabermetrics_bat['tot_baseonball'].values+sabermetrics_bat['tot_hitsbypitch'].values\n        sabermetrics_bat['OBP']=sabermetrics_bat['OBP'].values/(temp+epsilon)\n\n        #wOBA = (0.690×uBB + 0.722×HBP + 0.888×1B + 1.271×2B + 1.616×3B + 2.101×HR) / (AB + BB – IBB + SF + HBP). The coefficients are available for respective seasons\n        sabermetrics_bat['wOBA']=wOBA_2018['wBB'].values*sabermetrics_bat['tot_unintentionalwalks'].values+wOBA_2018['wHBP'].values*sabermetrics_bat['tot_hitsbypitch'].values+\\\n                                    wOBA_2018['w1B'].values*sabermetrics_bat['tot_singles'].values+wOBA_2018['w2B'].values*sabermetrics_bat['tot_doubles'].values+wOBA_2018['w3B'].values*sabermetrics_bat['tot_triples'].values+\\\n                                    wOBA_2018['wHR'].values*sabermetrics_bat['total_homeruns_scored'].values\n        temp=sabermetrics_bat['tot_attbat'].values+sabermetrics_bat['tot_sacflies'].values+sabermetrics_bat['tot_baseonball'].values-sabermetrics_bat['tot_intentionalwalks'].values+sabermetrics_bat['tot_hitsbypitch'].values\n        sabermetrics_bat['wOBA']=sabermetrics_bat['wOBA'].values/(temp+epsilon)\n        sabermetrics_bat['wOBA']=sabermetrics_bat['wOBA'].clip(0,1)\n        \n        #Total bases =  [1B + (2 × 2B) + (3 × 3B) + (4 × HR)]\n        sabermetrics_bat['net_TB']=sabermetrics_bat['tot_singles'].values+2*sabermetrics_bat['tot_doubles'].values+\\\n                    3*sabermetrics_bat['tot_triples'].values+sabermetrics_bat['total_homeruns_scored'].values\n\n        #SLG – Slugging average: total bases achieved on hits divided by at-bats (TB/AB)\n        sabermetrics_bat['SLG']=sabermetrics_bat['net_TB']/(sabermetrics_bat['tot_attbat']+epsilon)\n        sabermetrics_bat['SLG']=sabermetrics_bat['SLG'].clip(0,3)\n        \n        #A =  H + BB + HBP - HR - .5 * IBB    \n        #B = (1.4 * TB - .6 * H - 3 * HR + .1 * (BB + HBP - IBB) + .9 * (SB - CS - GIDP)) * 1.1\n        #C = AB - H + CS + GIDP\n\n        sabermetrics_bat['A_bsr']=sabermetrics_bat['tot_hits']+sabermetrics_bat['tot_baseonball']+sabermetrics_bat['tot_hitsbypitch']-\\\n                                sabermetrics_bat['total_homeruns_scored']-0.5*sabermetrics_bat['tot_intentionalwalks']\n        sabermetrics_bat['B_bsr']=1.1*(1.4*sabermetrics_bat['net_TB']-0.6*sabermetrics_bat['tot_hits']-3*sabermetrics_bat['total_homeruns_scored']+\\\n                                0.1*(sabermetrics_bat['tot_baseonball']+sabermetrics_bat['tot_hitsbypitch']-sabermetrics_bat['tot_intentionalwalks'])+\\\n                                0.9*(sabermetrics_bat['net_stolen_bases']-sabermetrics_bat['net_caught_stealing']-sabermetrics_bat['tot_GIDP']))                     \n\n        sabermetrics_bat['C_bsr']=sabermetrics_bat['tot_attbat']+sabermetrics_bat['net_caught_stealing']-sabermetrics_bat['tot_hits']+\\\n                                sabermetrics_bat['tot_GIDP']\n\n        sabermetrics_bat['bsr']=sabermetrics_bat['total_homeruns_scored']*(sabermetrics_bat['A_bsr']*sabermetrics_bat['B_bsr'])/(sabermetrics_bat['B_bsr']+sabermetrics_bat['C_bsr'])\n\n        #wRAA= {{wOBA-.320}/{1.25}}*(AB+BB+HBP+SF+SH)\n\n        sabermetrics_bat['wRAA']=(sabermetrics_bat['wOBA'].values-np.ones(len(sabermetrics_bat))*0.32)/1.25 * \\\n                            (sabermetrics_bat['tot_attbat']+sabermetrics_bat['tot_baseonball']+sabermetrics_bat['tot_hitsbypitch']+\\\n                             sabermetrics_bat['tot_sacflies']+sabermetrics_bat['tot_sacbunts'])\n        sabermetrics_bat['wRAA']=sabermetrics_bat['wRAA'].clip(-20,40)\n        \n        #ISO - calculated by subtracting batting average from slugging percentage\n        sabermetrics_bat['IS0']=sabermetrics_bat['SLG']-sabermetrics_bat['batting_average']\n\n        if self.method!='mean':\n\n            for cols in sabermetrics_bat.columns:\n                if 'tot' in cols or 'net' in cols:\n                    sabermetrics_bat[cols]=sabermetrics_bat[cols]/sabermetrics_bat['games_played']\n\n        return sabermetrics_bat\n    \n    def __get_pitchingstats__(self,season):\n        \n        print(self.season)\n        epsilon=1e-10\n        saber_pitch=pd.DataFrame()\n        saber_pitch['playerId']=self.df.playerId.unique()\n        box_scores=self.df[self.df.year==season]\n        if self.mode=='all':\n            box_scores=self.df.copy(deep=True)\n        \n        tmp_dict=dict(zip(self.df.playerId.values,self.df.playerName.values))\n        wOBA_2018=self.wOBA_weights[self.wOBA_weights.Season==season]\n        saber_pitch['playerName']=saber_pitch['playerId'].map(tmp_dict)\n\n        \n        \n        saber_pitch['games_played_pitch']=saber_pitch['playerId'].map(box_scores.groupby('playerId').gamesPlayedPitching.sum())\n        saber_pitch['games_started_pitch']=saber_pitch['playerId'].map(box_scores.groupby('playerId').gamesStartedPitching.sum())\n        saber_pitch['game_nonstarting_pitcher']=saber_pitch['games_played_pitch']-saber_pitch['games_started_pitch']\n        saber_pitch['tot_walks_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).baseOnBallsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_battersfaced']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).battersFaced.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_balks']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).balks.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_blownsaves']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).blownSaves.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_shutout_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).shutoutsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_wins_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).winsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_loss_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).lossesPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_flyout_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).flyOutsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_airout_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).airOutsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_groundout_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).groundOutsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_strikeout_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).strikeOutsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_runs_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).runsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_doubles_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).doublesPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_triples_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).triplesPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_homeruns_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).homeRunsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_intentionalwalks_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).intentionalWalksPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_unintentionalwalks_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).unintentional_walks_pitch.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_hits_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).hitsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_HBP_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).hitByPitchPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_atbats_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).atBatsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_caughtsteal_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).caughtStealingPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_stolenbases_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).stolenBasesPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_innings_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).inningsPitched.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_saveopportunities']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).saveOpportunities.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_earnedruns']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).earnedRuns.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_outs_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).outsPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_pitchesthrown']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).pitchesThrown.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_pitchesthrown_cnt']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).balls.count())\n        saber_pitch['tot_balls_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).earnedRuns.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_strikes_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).strikes.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_hitbatsman']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).hitBatsmen.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_wildpitches']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).wildPitches.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_rbi_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).rbiPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['games_finished_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).gamesFinishedPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_inherited_runner']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).inheritedRunners.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_inherited_runner_scored']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).inheritedRunnersScored.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_catcherinterference_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).catchersInterferencePitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_sacflies_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).sacFliesPitching.agg({self.method}).to_dict()[self.method])\n        saber_pitch['tot_sacbunts_pitch']=saber_pitch[self.grpby_elem].map(box_scores.groupby(self.grpby_elem).sacBuntsPitching.agg({self.method}).to_dict()[self.method])\n\n        saber_pitch['GO_AO_ratio']=saber_pitch['tot_groundout_pitch']/(saber_pitch['tot_airout_pitch']+epsilon)\n        saber_pitch['GO_AO_ratio']=saber_pitch['GO_AO_ratio'].clip(0,10)\n\n        saber_pitch['IP_GS_ratio']=saber_pitch['tot_innings_pitch']/(saber_pitch['games_started_pitch']+epsilon)\n        #saber_pitch[saber_pitch['IP_GS_ratio']>25]=25\n        saber_pitch['IP_GS_ratio']=saber_pitch['IP_GS_ratio'].clip(0,20)\n\n        saber_pitch['K_BB_ratio']=saber_pitch['tot_strikeout_pitch']/(saber_pitch['tot_walks_pitch']+epsilon)\n        #saber_pitch[saber_pitch['K_BB_ratio']>10]=10\n        saber_pitch['K_BB_ratio']=saber_pitch['K_BB_ratio'].clip(0,20)\n        \n\n        saber_pitch['PC_ST_ratio']=saber_pitch['tot_pitchesthrown']/(saber_pitch['tot_strikeout_pitch']+epsilon)\n        #saber_pitch[saber_pitch['PC_ST_ratio']>1.75]=1.75\n        saber_pitch['PC_ST_ratio']=saber_pitch['PC_ST_ratio'].clip(0,50)\n        \n\n        #ERA – Earned run average\n        saber_pitch['ERA']=9*saber_pitch['tot_earnedruns']/(saber_pitch['tot_innings_pitch']+epsilon)\n        saber_pitch['ERA']=saber_pitch['ERA'].clip(0,20)\n        #ERA+ – Adjusted ERA+  = 100*(2-{{\\mathit {ERA}} \\over {\\mathit {lgERA}}}\\cdot {1 \\over {\\mathit {PF}}})}\n\n\n        # PTB(pitcher's total bases)=0.89*(1.255*(H-HR)+4*HR)+0.56*(BB+HBP-IBB)\n        saber_pitch['PTB']=0.89*((saber_pitch['tot_hits_pitch']-saber_pitch['tot_homeruns_pitch'])*1.255 + 4*saber_pitch['tot_homeruns_pitch']) +\\\n        0.56*(saber_pitch['tot_walks_pitch']+saber_pitch['tot_HBP_pitch']-saber_pitch['tot_intentionalwalks_pitch'])\n        saber_pitch['PTB']=saber_pitch['PTB'].clip(0,20)\n        \n\n        # ERC(Component ERA)=9*{(H+BB+HBP)*PTB / BFP*IP}-0.56\n\n        saber_pitch['CERA']=9*(((saber_pitch['tot_hits_pitch']+saber_pitch['tot_walks_pitch']+saber_pitch['tot_hits_pitch'])*saber_pitch['PTB'])/ \\\n                        (saber_pitch['tot_battersfaced']*saber_pitch['tot_innings_pitch']+epsilon)) - 0.56\n        saber_pitch['CERA']=saber_pitch['CERA'].clip(0,8)\n        \n        #Defense-Independent Component ERA (DICE) = 3.00+{{13HR+3(BB+HBP)-2K}/{IP}}}\n\n        saber_pitch['DICE']=3+(13*saber_pitch['tot_homeruns_pitch']+3*(saber_pitch['tot_walks_pitch']+saber_pitch['tot_HBP_pitch'])-2*saber_pitch['tot_strikeout_pitch'])/\\\n                                (saber_pitch['tot_innings_pitch']+epsilon)\n        saber_pitch['DICE']=saber_pitch['DICE'].clip(0,8)\n\n        # FIP= {13HR+3BB-2K}/{IP}}+C\n        #saber_pitch['FIP']=((13*saber_pitch['tot_homeruns_pitch']+3*saber_pitch['tot_walks_pitch']-2*saber_pitch['tot_strikeout_pitch'])/(saber_pitch['tot_innings_pitch']+epsilon))+bias_weights['cFIP']\n\n\n\n        # BAA(Batting average against) = {H}/{BF-BB-HBP-SH-SF-CINT}}}\n        temp=saber_pitch['tot_battersfaced']-saber_pitch['tot_walks_pitch']-saber_pitch['tot_HBP_pitch']-saber_pitch['tot_sacflies_pitch']- \\\n                saber_pitch['tot_sacbunts_pitch']-saber_pitch['tot_catcherinterference_pitch']\n        saber_pitch['BAA']=saber_pitch['tot_hits_pitch']/(temp+epsilon)\n\n\n        #Power finesse ratio: The sum of strikeouts and walks divided by innings pitched\n\n        saber_pitch['power_finesse']=(saber_pitch['tot_strikeout_pitch']+saber_pitch['tot_walks_pitch'])/(saber_pitch['tot_innings_pitch']+epsilon)\n        saber_pitch['power_finesse']=saber_pitch['power_finesse'].clip(0,5)\n        \n        saber_pitch['WHIP']=(saber_pitch['tot_hits_pitch']+saber_pitch['tot_walks_pitch'])/(saber_pitch['tot_innings_pitch']+epsilon)\n        saber_pitch['WHIP']=saber_pitch['WHIP'].clip(1,2)\n        \n        if self.method!='mean':\n\n            for cols in saber_pitch.columns:\n                    if 'tot' in cols:\n                        saber_pitch[cols]=saber_pitch[cols]/saber_pitch['games_played_pitch']\n\n        return saber_pitch\n    \n    def __generate_batting_feats__(self):\n        obj=Sabermetrics(season=2018,df=self.df,method=self.method,wOBA_weights=wOBA_weights,grpby_elem=self.grpby_elem)\n        if self.mode=='all':\n            obj=Sabermetrics(season=2018,df=self.df,method=self.method,wOBA_weights=wOBA_weights,grpby_elem=self.grpby_elem,mode=self.mode)\n        \n        temp=obj.__get_battingstats__(season=2020)\n        sabermetrics_batting=pd.DataFrame(columns=temp.columns.tolist()+['season'])\n\n        for yr in [2018,2019,2020,2021]:\n            temp=obj.__get_battingstats__(season=yr)\n            temp['season']=yr\n            if self.mode=='all':\n                temp['season']=2021\n                sabermetrics_batting=pd.concat([sabermetrics_batting,temp])\n                break\n                \n            sabermetrics_batting=pd.concat([sabermetrics_batting,temp])\n                \n                \n                \n        for cols in sabermetrics_batting.columns:\n            sabermetrics_batting[cols]=sabermetrics_batting[cols].fillna(0)\n\n        sabermetrics_batting['merger']=[str(val1)+'_'+str(val2) for val1,val2 in zip(sabermetrics_batting['playerId'],sabermetrics_batting['season'])]\n\n        return sabermetrics_batting\n\n\n    def __generate_pitching_feats__(self):\n        obj=Sabermetrics(season=2018,df=self.df,method=self.method,wOBA_weights=wOBA_weights,grpby_elem=self.grpby_elem)\n        if self.mode=='all':\n            obj=Sabermetrics(season=2018,df=self.df,method=self.method,wOBA_weights=wOBA_weights,grpby_elem=self.grpby_elem,mode=self.mode)\n            \n        temp=obj.__get_pitchingstats__(season=2020)\n        sabermetrics_pitching=pd.DataFrame(columns=temp.columns.tolist()+['season'])\n\n        for yr in [2018,2019,2020,2021]:\n            temp=obj.__get_pitchingstats__(season=yr)\n            temp['season']=yr\n            if self.mode=='all':\n                temp['season']=2021\n                sabermetrics_pitching=pd.concat([sabermetrics_pitching,temp])\n                break\n            sabermetrics_pitching=pd.concat([sabermetrics_pitching,temp])\n            \n        for cols in sabermetrics_pitching.columns:\n            sabermetrics_pitching[cols]=sabermetrics_pitching[cols].fillna(0)\n\n        sabermetrics_pitching['merger']=[str(val1)+'_'+str(val2) for val1,val2 in zip(sabermetrics_pitching['playerId'],sabermetrics_pitching['season'])]\n\n        return sabermetrics_pitching\n\n","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2021-07-27T15:31:18.013161Z","iopub.execute_input":"2021-07-27T15:31:18.013499Z","iopub.status.idle":"2021-07-27T15:31:18.124354Z","shell.execute_reply.started":"2021-07-27T15:31:18.013468Z","shell.execute_reply":"2021-07-27T15:31:18.123417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"obj=Sabermetrics(season=2018,df=boxscores_df,method='sum',wOBA_weights=wOBA_weights,grpby_elem='playerId')\nsabermetrics_batting=obj.__generate_batting_feats__()\nsabermetrics_pitching=obj.__generate_pitching_feats__()\npd.set_option('display.max_columns', 500)\n\nsabermetrics=pd.merge(sabermetrics_batting,sabermetrics_pitching,on='merger',how='left')\ncols2_drop=[ 'playerId_x', 'playerId_y','playerName_x','season_x','season_y','playerName_y','tot_pitchesthrown_cnt']\nsabermetrics.drop(columns=cols2_drop,inplace=True)\nsabermetrics=sabermetrics.fillna(0)\n\nsabermetrics.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:18.125769Z","iopub.execute_input":"2021-07-27T15:31:18.126233Z","iopub.status.idle":"2021-07-27T15:31:21.305187Z","shell.execute_reply.started":"2021-07-27T15:31:18.126198Z","shell.execute_reply":"2021-07-27T15:31:21.304032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sabermetrics.to_csv('sabermetrics.csv',index=None)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:21.307068Z","iopub.execute_input":"2021-07-27T15:31:21.307503Z","iopub.status.idle":"2021-07-27T15:31:22.048164Z","shell.execute_reply.started":"2021-07-27T15:31:21.307460Z","shell.execute_reply":"2021-07-27T15:31:22.047397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"obj=Sabermetrics(season=2018,df=boxscores_df,method='sum',wOBA_weights=wOBA_weights,grpby_elem='playerId',mode='all')\nsabermetrics_batting=obj.__generate_batting_feats__()\nsabermetrics_pitching=obj.__generate_pitching_feats__()\n\nsabermetrics_all=pd.merge(sabermetrics_batting,sabermetrics_pitching,on='merger',how='left')\ncols2_drop=[ 'playerId_x', 'playerId_y','playerName_x','season_x','season_y','playerName_y','tot_pitchesthrown_cnt']\nsabermetrics_all.drop(columns=cols2_drop,inplace=True)\nsabermetrics_all=sabermetrics_all.fillna(0)\n\nsabermetrics_all","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:22.049853Z","iopub.execute_input":"2021-07-27T15:31:22.050468Z","iopub.status.idle":"2021-07-27T15:31:23.878234Z","shell.execute_reply.started":"2021-07-27T15:31:22.050420Z","shell.execute_reply":"2021-07-27T15:31:23.877403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = targets[targets_cols].merge(players[players_cols], on=['playerId'], how='left')\nposition2num = {c: i for i, c in enumerate(train['primaryPositionName'].unique())}\ntrain['primaryPositionName'] = train['primaryPositionName'].map(position2num)\ntrain['year']=[str(val)[:4] for val in train.date.values]\ntrain['merger']=[str(val1)+'_'+val2 for val1,val2 in zip(train.playerId.values,train.year.values)]\ntrain=train.merge(sabermetrics, on='merger',how='left')\ntrain = train.merge(player_target_stats, how='inner', left_on=[\"playerId\"],right_on=[\"playerId\"])\n\n\n#train = train.merge(rosters[roster_cols], on=['playerId', 'date'], how='left')\n#train = train.merge(scores[scores_cols], on=['playerId', 'date'], how='left')\n\n\n\nplayer2num = {c: i for i, c in enumerate(train['playerId'].unique())}\n#teamid2num = {c: i for i, c in enumerate(train['teamId'].unique())}\n#status2num = {c: i for i, c in enumerate(train['status'].unique())}\ntrain['playerId'] = train['playerId'].map(player2num)\n#train['teamId'] = train['teamId'].map(teamid2num)\n#train['status'] = train['status'].map(status2num)\n\n\ntrain.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:23.879652Z","iopub.execute_input":"2021-07-27T15:31:23.880118Z","iopub.status.idle":"2021-07-27T15:31:39.892267Z","shell.execute_reply.started":"2021-07-27T15:31:23.880072Z","shell.execute_reply":"2021-07-27T15:31:39.891593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_cols=train.drop(columns=['playerId', 'target1', 'target2', 'target3', 'target4', 'date','merger','year']).columns\nfor cols in feature_cols:\n    train[cols]=train[cols].fillna(0)\nfeature_cols","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:39.893405Z","iopub.execute_input":"2021-07-27T15:31:39.893853Z","iopub.status.idle":"2021-07-27T15:31:44.701501Z","shell.execute_reply.started":"2021-07-27T15:31:39.893807Z","shell.execute_reply":"2021-07-27T15:31:44.700787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem_usage(df, verbose=True):\n    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n    start_mem = df.memory_usage().sum() / 1024**2\n    for col in df.columns:\n        col_type = df[col].dtypes\n        if col_type in numerics:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)\n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)\n\n    end_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n\n    return df","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2021-07-27T15:31:44.703162Z","iopub.execute_input":"2021-07-27T15:31:44.703638Z","iopub.status.idle":"2021-07-27T15:31:44.716514Z","shell.execute_reply.started":"2021-07-27T15:31:44.703596Z","shell.execute_reply":"2021-07-27T15:31:44.715463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split,GroupKFold\n\n#X_train, X_valid, y_train, y_valid = train_test_split(train[feature_cols], train[['target1', 'target2', 'target3', 'target4']], test_size=0.25, random_state=421)\ntrain_X = train[feature_cols]\ntrain_y = train[['target1', 'target2', 'target3', 'target4']]\n\n_index = (train['date'] < 20210401)\nx_train = train_X.loc[_index].reset_index(drop=True)\ny_train = train_y.loc[_index].reset_index(drop=True)\nx_valid = train_X.loc[~_index].reset_index(drop=True)\ny_valid = train_y.loc[~_index].reset_index(drop=True)\nx_train=x_train.values.reshape(x_train.shape[0],1,x_train.shape[1])\nx_valid=x_valid.values.reshape(x_valid.shape[0],1,x_valid.shape[1])\ndel train_X,train_y\ngc.collect()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-07-27T15:31:44.718050Z","iopub.execute_input":"2021-07-27T15:31:44.718351Z","iopub.status.idle":"2021-07-27T15:31:48.737319Z","shell.execute_reply.started":"2021-07-27T15:31:44.718324Z","shell.execute_reply":"2021-07-27T15:31:48.736339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\n# training lightgbm\nparams1 = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 2000,\n 'learning_rate': 0.01,\n 'random_state': 42,\n \"num_leaves\": 64\n}\n\nparams2 = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 100,\n 'learning_rate': 0.01,\n 'random_state': 42,\n \"num_leaves\": 25\n}\n\nparams4 = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 2000,\n 'learning_rate': 0.01,\n 'random_state': 42,\n \"num_leaves\": 64\n}\n\n\nparams = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 2000,\n 'learning_rate': 0.01,\n 'random_state': 42,\n \"num_leaves\": 64\n}\n\n\noof1, model1, score1 = fit_lgbm(\n    x_train, y_train['target1'],\n    x_valid, y_valid['target1'],\n    params1\n)\n\noof2, model2, score2 = fit_lgbm(\n    x_train, y_train['target2'],\n    x_valid, y_valid['target2'],\n    params2\n)\n\noof3, model3, score3 = fit_lgbm(\n    x_train, y_train['target3'],\n    x_valid, y_valid['target3'],\n    params\n)\n\noof4, model4, score4 = fit_lgbm(\n    x_train, y_train['target4'],\n    x_valid, y_valid['target4'],\n    params4\n)\n\nscore = (score1+score2+score3+score4) / 4\nprint(f'score: {score}')\"\"\"","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2021-07-27T15:31:48.738840Z","iopub.execute_input":"2021-07-27T15:31:48.739252Z","iopub.status.idle":"2021-07-27T15:31:48.747553Z","shell.execute_reply.started":"2021-07-27T15:31:48.739210Z","shell.execute_reply":"2021-07-27T15:31:48.746520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import matplotlib.pyplot as plt\nimport seaborn as sns\n\nfeature_imp = pd.DataFrame(sorted(zip(model1.feature_importances_,x_train[feature_cols])), columns=['Value','Feature'])\n\nplt.figure(figsize=(15, 10))\nsns.barplot(x=\"Value\", y=\"Feature\", data=feature_imp.sort_values(by=\"Value\", ascending=False).head(40))\nplt.title('LightGBM Features (avg over folds)')\nplt.tight_layout()\nplt.show()\n#plt.savefig('lgbm_importances-01.png')\n\nprint (feature_imp['Feature'].tolist())\n\nfeature_imp.tail(20)\"\"\"","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2021-07-27T15:31:48.748731Z","iopub.execute_input":"2021-07-27T15:31:48.749020Z","iopub.status.idle":"2021-07-27T15:31:48.767628Z","shell.execute_reply.started":"2021-07-27T15:31:48.748992Z","shell.execute_reply":"2021-07-27T15:31:48.766413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.callbacks import Callback, ReduceLROnPlateau, ModelCheckpoint, EarlyStopping\n\nclass Attention(tf.keras.layers.Layer):\n    def __init__(self,**kwargs):\n        super(Attention,self).__init__(**kwargs)\n\n    def build(self,input_shape):\n        self.W=self.add_weight(name=\"att_weight\",shape=(input_shape[-1],1),initializer=\"normal\")\n        self.b=self.add_weight(name=\"att_bias\",shape=(input_shape[1],1),initializer=\"zeros\")        \n        super(Attention, self).build(input_shape)\n\n    def call(self,x):\n        et=tf.keras.backend.squeeze(tf.keras.backend.tanh(tf.keras.backend.dot(x,self.W)+self.b),axis=-1)\n        at=tf.keras.backend.softmax(et)\n        at=tf.keras.backend.expand_dims(at,axis=-1)\n        output=x*at\n        return tf.keras.backend.sum(output,axis=1)\n\n    def compute_output_shape(self,input_shape):\n        return (input_shape[0],input_shape[-1])\n\n    def get_config(self):\n        return super(Attention,self).get_config()","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:48.769132Z","iopub.execute_input":"2021-07-27T15:31:48.769572Z","iopub.status.idle":"2021-07-27T15:31:55.179005Z","shell.execute_reply.started":"2021-07-27T15:31:48.769522Z","shell.execute_reply":"2021-07-27T15:31:55.177917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nnum_columns=len(feature_cols); num_labels=y_train.shape[-1];\ndef create_mlp1(attention):\n    inp = tf.keras.layers.Input(shape = (x_train.shape[1], x_train.shape[2]))\n    #inp = tf.keras.layers.Input(shape = (num_columns))\n    x = tf.keras.layers.BatchNormalization()(inp)\n    x = tf.keras.layers.LSTM(128, input_shape=(x_train.shape[1], x_train.shape[2]),return_sequences=True)(x)\n    att_out=attention(x)\n\n    \"\"\"\n    x = tf.keras.layers.Reshape((1,1, x.shape[-1]), input_shape=(1,x.shape[-1]))(x)\n    x = tf.keras.layers.Conv1D(32,5,input_shape=(1,1,x.shape[-1]),padding='same')(x)    \n    x = tf.keras.layers.Conv1D(16,3,input_shape=(1,1,x.shape[-1]),padding='same')(x)    \n    \"\"\"#x = tf.keras.layers.Dropout(0.12)(x)\n    x = tf.keras.layers.Activation(tf.keras.activations.relu,name='inp_activation')(att_out)\n    x = tf.keras.layers.Flatten()(x)\n  \n    hidden_unit=[64,90,150,100];drop_dense=[0.1,0.1,0.1,0.15]\n    for i in range(1):\n        x = tf.keras.layers.Dense(hidden_unit[i],name=f\"Dense_layer_{i}\")(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = tf.keras.layers.Activation(tf.keras.activations.relu)(x)\n        x = tf.keras.layers.Dropout(drop_dense[i])(x)  \n    \n    x = tf.keras.layers.Dense(4)(x)\n    out = tf.keras.layers.Activation('linear')(x)\n    \n    model = tf.keras.models.Model(inputs = inp, outputs = out)\n    model.compile(optimizer = tf.keras.optimizers.SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True),\n                  loss = [tf.keras.losses.MeanAbsoluteError(),tf.keras.losses.Huber(delta=1.0)]) \n                  #metrics = tf.keras.metrics.AUC(name = 'AUC')) \n    #print(model.summary())\n    return model\nmodel=create_mlp1(attention=Attention())\nprint(model.summary())\n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:55.180485Z","iopub.execute_input":"2021-07-27T15:31:55.180799Z","iopub.status.idle":"2021-07-27T15:31:55.737109Z","shell.execute_reply.started":"2021-07-27T15:31:55.180768Z","shell.execute_reply":"2021-07-27T15:31:55.736204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#ckp_path = f'JSModel_{fold}.hdf5'\nmodel = create_mlp1(attention=Attention())\n#model = model_fn(hp)\nes = EarlyStopping(monitor='val_loss', mode='min', verbose=0, patience=5)\n\n\nrlr = ReduceLROnPlateau(monitor = 'val_MeanAbsoluteError', factor = 0.1, patience = 3, verbose = 0, \n                        min_delta = 1e-4, mode = 'min')\n\n#X_train = (X_train - np.mean(X_train, axis=0)) / np.std(X_train, axis=0)\n#X_val = (X_val - np.mean(X_val, axis=0)) / np.std(X_val, axis=0)\n\n#y_train = (y_train - np.mean(y_train, axis=0)) / np.std(y_train, axis=0)\n#y_val = (y_val - np.mean(y_val, axis=0)) / np.std(y_val, axis=0)\n\nmodel.fit(x_train, y_train, validation_data = (x_valid, y_valid), epochs = 100, \n          batch_size = 4096,callbacks=[es])#, verbose = 0)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:31:55.738520Z","iopub.execute_input":"2021-07-27T15:31:55.738828Z","iopub.status.idle":"2021-07-27T15:32:53.759321Z","shell.execute_reply.started":"2021-07-27T15:31:55.738798Z","shell.execute_reply":"2021-07-27T15:32:53.757450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=model.predict(x_valid)\npred.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:33:06.193429Z","iopub.execute_input":"2021-07-27T15:33:06.193822Z","iopub.status.idle":"2021-07-27T15:33:09.367533Z","shell.execute_reply.started":"2021-07-27T15:33:06.193785Z","shell.execute_reply":"2021-07-27T15:33:09.366395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference","metadata":{}},{"cell_type":"code","source":"players_cols = ['playerId', 'primaryPositionName']\nrosters_cols = ['playerId', 'teamId', 'status','date']\n\n\nnull = np.nan\ntrue = True\nfalse = False\n\nenv = mlb.make_env() # initialize the environment\niter_test = env.iter_test() # iterator which loops over each date in test set\n\nfor (test_df, sample_prediction_df) in 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'] = sample_prediction_df['date_playerId']\\\n                                        .map(lambda x: int(x.split('_')[0]))\n    \n    # Dealing with missing values\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    test_scores = test_scores.groupby('playerId').sum().reset_index()\n    test = sample_prediction_df[['playerId']].copy()\n    test['date']=sample_prediction_df['date']\n    test = test.merge(players[players_cols], on='playerId', how='left')\n    test['primaryPositionName'] = test['primaryPositionName'].map(position2num)\n    test['year']=[str(val)[:4] for val in test.date.values]\n    test['merger']=[str(val1)+'_'+val2 for val1,val2 in zip(test.playerId.values,test.year.values)]\n    test=test.merge(sabermetrics_all, on='merger',how='left')\n    test = test.merge(player_target_stats, how='inner', left_on=[\"playerId\"],right_on=[\"playerId\"])\n\n    \n    \n   # test = test.merge(rosters[roster_cols], on=['playerId', 'date'], how='left')\n  \n    test['playerId'] = test['playerId'].map(player2num)\n    #test['teamId'] = test['teamId'].map(teamid2num)\n    #test['status'] = test['status'].map(status2num)\n    test_X = test[feature_cols]\n    test_X=test_X.values.reshape(test_X.shape[0],1,test_X.shape[1])\n    # predict\n   \n    pred = model.predict(test_X)\n    \n    # merge submission\n    sample_prediction_df['target1'] = np.clip(pred[:,0], 0, 100)\n    sample_prediction_df['target2'] = np.clip(pred[:,1], 0, 100)\n    sample_prediction_df['target3'] = np.clip(pred[:,2], 0, 100)\n    sample_prediction_df['target4'] = np.clip(pred[:,3], 0, 100)\n    sample_prediction_df = sample_prediction_df.fillna(0.)\n    del sample_prediction_df['playerId'],sample_prediction_df['date']\n    \n    env.predict(sample_prediction_df)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T15:33:09.369048Z","iopub.execute_input":"2021-07-27T15:33:09.369367Z","iopub.status.idle":"2021-07-27T15:33:12.594288Z","shell.execute_reply.started":"2021-07-27T15:33:09.369336Z","shell.execute_reply":"2021-07-27T15:33:12.593161Z"},"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)\nx_train = train_X.loc[_index].reset_index(drop=True)\ny_train = train_y.loc[_index].reset_index(drop=True)\nx_valid = train_X.loc[~_index].reset_index(drop=True)\ny_valid = train_y.loc[~_index].reset_index(drop=True)\n\ndel train_X,train_y\ngc.collect()\"\"\"","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2021-07-27T15:30:27.509370Z","iopub.execute_input":"2021-07-27T15:30:27.509783Z","iopub.status.idle":"2021-07-27T15:30:27.515821Z","shell.execute_reply.started":"2021-07-27T15:30:27.509746Z","shell.execute_reply":"2021-07-27T15:30:27.514761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\n# training lightgbm\nparams1 = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 3000,\n 'learning_rate': 0.1,\n 'random_state': 42,\n \"num_leaves\": 100\n}\n\nparams2 = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 3000,\n 'learning_rate': 0.1,\n 'random_state': 42,\n \"num_leaves\": 22\n}\n\nparams4 = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 3000,\n 'learning_rate': 0.1,\n 'random_state': 42,\n \"num_leaves\": 100\n}\n\n\nparams = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 3000,\n 'learning_rate': 0.1,\n 'random_state': 42,\n \"num_leaves\": 100\n}\n\n\noof1, model1, score1 = fit_lgbm(\n    x_train, y_train['target1'],\n    x_valid, y_valid['target1'],\n    params1\n)\n\noof2, model2, score2 = fit_lgbm(\n    x_train, y_train['target2'],\n    x_valid, y_valid['target2'],\n    params2\n)\n\noof3, model3, score3 = fit_lgbm(\n    x_train, y_train['target3'],\n    x_valid, y_valid['target3'],\n    params\n)\n\noof4, model4, score4 = fit_lgbm(\n    x_train, y_train['target4'],\n    x_valid, y_valid['target4'],\n    params4\n)\n\nscore = (score1+score2+score3+score4) / 4\nprint(f'score: {score}')\"\"\"","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2021-07-27T15:30:27.773074Z","iopub.execute_input":"2021-07-27T15:30:27.773442Z","iopub.status.idle":"2021-07-27T15:30:27.781219Z","shell.execute_reply.started":"2021-07-27T15:30:27.773398Z","shell.execute_reply":"2021-07-27T15:30:27.780521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"players_cols = ['playerId', 'primaryPositionName']\nrosters_cols = ['playerId', 'teamId', 'status','date']\n\nnull = np.nan\ntrue = True\nfalse = False\n\nenv = mlb.make_env() # initialize the environment\niter_test = env.iter_test() # iterator which loops over each date in test set\n\nfor (test_df, sample_prediction_df) in 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'] = sample_prediction_df['date_playerId']\\\n                                        .map(lambda x: int(x.split('_')[0]))\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    test_scores = test_scores.groupby('playerId').sum().reset_index()\n    test = sample_prediction_df[['playerId']].copy()\n    test['date']=sample_prediction_df['date']\n    test = test.merge(players[players_cols], on='playerId', how='left')\n    test['primaryPositionName'] = test['primaryPositionName'].map(position2num)\n    test['year']=[str(val)[:4] for val in test.date.values]\n    test['merger']=[str(val1)+'_'+val2 for val1,val2 in zip(test.playerId.values,test.year.values)]\n    test=test.merge(sabermetrics_all, on='merger',how='left')\n    test = test.merge(player_target_stats, how='inner', left_on=[\"playerId\"],right_on=[\"playerId\"])\n\n    \n    \n    test = test.merge(rosters[roster_cols], on=['playerId', 'date'], how='left')\n  \n    test['playerId'] = test['playerId'].map(player2num)\n    test['teamId'] = test['teamId'].map(teamid2num)\n    test['status'] = test['status'].map(status2num)\n\n    test_X = test[feature_cols]\n    \n    # predict\n    pred1 = model1.predict(test_X)\n    pred2 = model2.predict(test_X)\n    pred3 = model3.predict(test_X)\n    pred4 = model4.predict(test_X)\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'],sample_prediction_df['date']\n    \n    env.predict(sample_prediction_df)\"\"\"","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2021-07-27T15:30:27.979980Z","iopub.execute_input":"2021-07-27T15:30:27.980514Z","iopub.status.idle":"2021-07-27T15:30:27.989327Z","shell.execute_reply.started":"2021-07-27T15:30:27.980465Z","shell.execute_reply":"2021-07-27T15:30:27.988387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}