{"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":"## About Dataset","metadata":{"_uuid":"dedc4e57-daea-4f1f-9281-257352546646","_cell_guid":"7cda4356-b015-4992-877b-e40eb158b01e","trusted":true}},{"cell_type":"code","source":"modelsver=\"july-modelsver2\"\nversion = \"july_ver2\"","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:00:32.279798Z","iopub.execute_input":"2021-07-31T12:00:32.280198Z","iopub.status.idle":"2021-07-31T12:00:32.289218Z","shell.execute_reply.started":"2021-07-31T12:00:32.280115Z","shell.execute_reply":"2021-07-31T12:00:32.288125Z"},"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.float32)\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    end_mem = df.memory_usage().sum() / 1024**2\n    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))\n    return df","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:00:34.090098Z","iopub.execute_input":"2021-07-31T12:00:34.090433Z","iopub.status.idle":"2021-07-31T12:00:34.101907Z","shell.execute_reply.started":"2021-07-31T12:00:34.090401Z","shell.execute_reply":"2021-07-31T12:00:34.100681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unpack_json(df,var):\n    name = var\n    tmp = df[['date']+[name]].dropna(how='any').reset_index(drop=True)\n    temp=[]\n    for val in tmp[name]:\n        temp.append(json.loads(val))\n    tmp[name]=temp\n    \n    var_val_dict = dict(zip(tmp.iloc[0,1][0].keys(),[[] for val in tmp.iloc[0,1][0].keys()]))\n    date_list = []\n\n    for d, eng in zip(tmp['date'].values,tmp[name].values): #dfの1行ずつでループ\n        for val in eng: #1セルのリストの要素ごとにループ\n            date_list.append(d)\n            for key in val.keys(): #リストの1要素が辞書になっており、辞書内のキーでループ\n                var_val_dict[key].append(val[key])\n                #var_val_dict['date_tr'].append(id1)\n                #tqdm(zip(df['date'].values,df[name].values))\n\n    output_df = pd.DataFrame(var_val_dict)\n    output_df['date'] = date_list\n    gc.collect()\n    \n    return output_df","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:00:34.579264Z","iopub.execute_input":"2021-07-31T12:00:34.579576Z","iopub.status.idle":"2021-07-31T12:00:34.586768Z","shell.execute_reply.started":"2021-07-31T12:00:34.579546Z","shell.execute_reply":"2021-07-31T12:00:34.586130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training","metadata":{"_uuid":"ea4d85e1-21d7-4e7a-a8e7-36e5f53f4612","_cell_guid":"8d708767-e50d-4684-82a1-790feb5c0c1f","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"},"trusted":true}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom pathlib import Path\nfrom sklearn.metrics import mean_absolute_error\nfrom datetime import timedelta\nfrom functools import reduce\nfrom tqdm import tqdm\nimport lightgbm as lgbm\nfrom mlb.competition import make_env\nfrom datetime import timedelta\nimport os\nimport gc\nimport pickle\nimport json\n\npd.set_option('display.max_columns', 150)\npd.set_option('display.max_rows', 50)","metadata":{"_uuid":"2139878b-da24-41e3-bb59-76b60a1e16ef","_cell_guid":"3181642b-6cb0-424d-b5be-a0e6a5cb0457","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-07-31T12:00:34.746639Z","iopub.execute_input":"2021-07-31T12:00:34.747004Z","iopub.status.idle":"2021-07-31T12:00:36.873682Z","shell.execute_reply.started":"2021-07-31T12:00:34.746973Z","shell.execute_reply":"2021-07-31T12:00:36.872874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_DIR = Path('../input/mlb-player-digital-engagement-forecasting')\nTRAIN_DIR = Path('../input/train-update')","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:00:36.875276Z","iopub.execute_input":"2021-07-31T12:00:36.875630Z","iopub.status.idle":"2021-07-31T12:00:36.880522Z","shell.execute_reply.started":"2021-07-31T12:00:36.875595Z","shell.execute_reply":"2021-07-31T12:00:36.879389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tmp = pd.read_csv('../input/mlb-player-digital-engagement-forecasting/train_updated.csv')\ntargets = unpack_json(train_tmp,'nextDayPlayerEngagement')\nrosters = unpack_json(train_tmp,'rosters')\nscores_tmp = unpack_json(train_tmp,'playerBoxScores')\ngames = unpack_json(train_tmp,'games')\nteamBoxScores = unpack_json(train_tmp,'teamBoxScores')\nplayers = pd.read_csv(\"../input/playerscsv/NEWplayers.csv\")\nscores = scores_tmp.groupby(['playerId', 'date']).sum().reset_index()\nteams = pd.read_csv(BASE_DIR / 'teams.csv')\nseasons = pd.read_csv(BASE_DIR / 'seasons.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:00:36.882536Z","iopub.execute_input":"2021-07-31T12:00:36.882946Z","iopub.status.idle":"2021-07-31T12:02:24.295268Z","shell.execute_reply.started":"2021-07-31T12:00:36.882862Z","shell.execute_reply":"2021-07-31T12:02:24.294252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_tmp","metadata":{"_uuid":"3540a2ce-95e1-416f-9892-bcfa92cf6047","_cell_guid":"5f9dc680-5158-4bf2-856f-d43b6aa620de","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-07-31T12:02:24.297360Z","iopub.execute_input":"2021-07-31T12:02:24.297740Z","iopub.status.idle":"2021-07-31T12:02:24.408422Z","shell.execute_reply.started":"2021-07-31T12:02:24.297712Z","shell.execute_reply":"2021-07-31T12:02:24.407007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets_cols = ['playerId', 'target1', 'target2', 'target3', 'target4', 'date']\nplayers_cols = ['playerId', 'primaryPositionName', \"mlbDebutDate\",\"DOB\"]\nrosters_cols = ['playerId', 'teamId', 'status', 'date']\nscores_cols = [\"gamePk\", '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',\n 'label_primaryPositionName',\n 'label_teamId',\n 'label_status',\n 'battingOrder',\n 'gamesPlayedBatting',\n 'runsScored',\n 'homeRuns',\n 'baseOnBalls',\n 'hits',\n 'plateAppearances',\n 'totalBases',\n 'rbi',\n 'sacFlies',\n 'gamesPlayedPitching',\n 'gamesStartedPitching',\n 'winsPitching',\n 'lossesPitching',\n 'runsPitching',\n 'strikeOutsPitching',\n 'inningsPitched',\n 'saveOpportunities',\n 'earnedRuns',\n 'battersFaced',\n 'pitchesThrown',\n 'balls',\n 'strikes',\n 'rbiPitching',\n 'gamesFinishedPitching',\n 'saves',\n 'holds',\n 'assists',\n 'putOuts',\n 'chances',\n 'target1_mean',\n 'target1_median',\n 'target1_std',\n 'target1_min',\n 'target1_max',\n 'target1_prob',\n 'target2_mean',\n 'target2_median',\n 'target2_std',\n 'target2_min',\n 'target2_max',\n 'target2_prob',\n 'target3_mean',\n 'target3_median',\n 'target3_std',\n 'target3_min',\n 'target3_max',\n 'target3_prob',\n 'target4_mean',\n 'target4_median',\n 'target4_std',\n 'target4_min',\n 'target4_max',\n 'target4_prob',\n 'gamesStartedPitching_shift_one',\n 'gamesStartedPitching_shift_two',\n 'gamesStartedPitching_shift_three',\n 'gamesStartedPitching_shift_four',\n 'gamesStartedPitching_shift_five',\n 'gamesStartedPitching_shift_six',\n 'gamesStartedPitching_shift_seven',\n 'gamesStartedPitching_shift_eight',\n 'gamesStartedPitching_shift_nine',\n 'gamesStartedPitching_shift_ten',\n 'gamesStartedPitching_shift_ele',\n 'gamesStartedPitching_shift_twe',\n 'gamesStartedPitching_shift_thirteen',\n 'gamesStartedPitching_shift_fourteen',\n 'gamesStartedPitching_shift_fifteen',\n'target1_shift_three', 'target2_shift_three', 'target3_shift_three',\n'target4_shift_three', 'target1_shift_four', 'target2_shift_four',\n'target3_shift_four', 'target4_shift_four', 'target1_shift_five',\n'target2_shift_five', 'target3_shift_five', 'target4_shift_five',\n'target1_shift_six', 'target2_shift_six', 'target3_shift_six',\n'target4_shift_six', 'target1_shift_seven', 'target2_shift_seven',\n'target3_shift_seven', 'target4_shift_seven',\n                \n 'batter_contribution',\n 'batter_contribution_rolling_mean_t3',\n 'batter_contribution_rolling_mean_t5',\n 'batter_contribution_rolling_mean_t7',\n 'batter_contribution_rolling_mean_t10',\n 'batter_contribution_rolling_mean_t15',\n 'batter_contribution_rolling_std_t3',\n 'batter_contribution_rolling_std_t5',\n 'batter_contribution_rolling_std_t7',\n 'batter_contribution_rolling_std_t10',\n 'batter_contribution_rolling_std_t15',\n 'pitchingGameScore',\n 'pitchingGameScore_rolling_mean_t7',\n 'pitchingGameScore_rolling_mean_t14',\n 'pitchingGameScore_rolling_mean_t28',\n 'pitchingGameScore_rolling_std_t7',\n 'pitchingGameScore_rolling_std_t14',\n 'pitchingGameScore_rolling_std_t28',\n#  'numberOfFollowers',\n 'isgame_schedule_tom',\n 'isgame_schedule',\n 'starter_or_not',\n#  'player_age',\n#  'mlbDebutDate_passed',\n 'HappyBirthDay',\n 'stater_or_not_mean_t7',\n 'stater_or_not_mean_t14',\n 'stater_or_not_mean_t28'          \n]\n\nfeature_cols2=feature_cols+[\"target1\"]","metadata":{"_uuid":"046564c8-2d25-4540-9a94-9e629e263a22","_cell_guid":"39e17ba0-96e5-438c-94bd-6c184905c35f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-07-31T12:02:24.409821Z","iopub.execute_input":"2021-07-31T12:02:24.410088Z","iopub.status.idle":"2021-07-31T12:02:24.429969Z","shell.execute_reply.started":"2021-07-31T12:02:24.410063Z","shell.execute_reply":"2021-07-31T12:02:24.429037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_target_stats = pd.read_csv(\"../input/player-target-stats/player_target_stats.csv\")\ndata_names=player_target_stats.columns.values.tolist()\n# data_names","metadata":{"_uuid":"09b65c0a-4ac9-45cb-86ee-d81e871960a7","_cell_guid":"5ee50f59-b249-4698-ae86-e35289c6df01","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-07-31T12:02:24.431233Z","iopub.execute_input":"2021-07-31T12:02:24.431443Z","iopub.status.idle":"2021-07-31T12:02:24.485946Z","shell.execute_reply.started":"2021-07-31T12:02:24.431420Z","shell.execute_reply":"2021-07-31T12:02:24.485133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# trainのデータ作成","metadata":{}},{"cell_type":"code","source":"# creat dataset\ntrain = targets[targets_cols].merge(players[players_cols], on=['playerId'], how='left')\nprint(\"0\",train.shape)\ntrain = train.merge(rosters[rosters_cols], on=['playerId', 'date'], how='left')\ntrain = train.merge(scores[scores_cols], on=['playerId', 'date'], how='left')\ntrain = train.merge(player_target_stats, how='inner', left_on=[\"playerId\"],right_on=[\"playerId\"])\nprint(\"1\",train.shape)\n\ntrain = train.merge(teams, how='left', left_on=[\"teamId\"],right_on=[\"id\"])\ntrain[\"year_month\"] = pd.to_datetime(train['date'], format=\"%Y%m%d\").map(lambda x:str(x.year) + str(x.month).zfill(2)).astype(int)\nprint(\"2\",train.shape)\ntrain=train.rename(columns={\"date_x\": \"date\"})","metadata":{"_uuid":"6cb3c4b2-a69b-4ce5-9d70-6633354c11ea","_cell_guid":"69624101-e45d-442a-bbb4-fb5514840e21","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-07-31T12:02:24.487068Z","iopub.execute_input":"2021-07-31T12:02:24.487412Z","iopub.status.idle":"2021-07-31T12:02:47.561176Z","shell.execute_reply.started":"2021-07-31T12:02:24.487371Z","shell.execute_reply":"2021-07-31T12:02:47.560262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://blog.amedama.jp/entry/2017/10/10/135331\n#https://ohke.hateblo.jp/entry/2020/08/15/150000\ngames_schdule = pd.read_parquet('../input/mlb-schedule/schedule.parquet')\ngames_schdule[\"date\"]=pd.to_datetime(games_schdule['date'], format=\"%Y-%m-%d\").map(lambda x:str(x.year) + str(x.month).zfill(2)+ str(x.day).zfill(2)).astype(int)\ngames_schdule[\"before_date\"]=(pd.to_datetime(games_schdule['date'], format=\"%Y%m%d\") + timedelta(days=-1)).map(lambda x:str(x.year) + str(x.month).zfill(2)+ str(x.day).zfill(2)).astype(int)\ngames_schdule[\"isgame_schedule\"]=1\n\n\ntemas_dict={'CIN': 'CIN','BOS': 'BOS', 'TEX': 'TEX', 'SEA': 'SEA', 'CHC': 'CHC', 'ARI': 'ARI', 'STL': 'STL', 'PHI': 'PHI',\n 'COL': 'COL', 'TOR': 'TOR', 'CLE': 'CLE', 'LAA': 'LAA', 'SFG': 'SF', 'ATL': 'ATL', 'KCR': 'KC', 'BAL': 'BAL', 'MIN': 'MIN',\n 'HOU': 'HOU', 'SDP': 'SD', 'MIA': 'MIA', 'MIL': 'MIL', 'TBR': 'TB','CHW': 'CWS', 'WSN': 'WSH', 'NYM': 'NYM', 'NYY': 'NYY', \n 'LAD': 'LAD', 'OAK': 'OAK', 'DET': 'DET', 'PIT': 'PIT'}\n\ngames_schdule.Tm = games_schdule.Tm.map(temas_dict)\n\ngames_schdule.drop_duplicates([\"date\", \"Tm\"], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:02:47.562906Z","iopub.execute_input":"2021-07-31T12:02:47.563147Z","iopub.status.idle":"2021-07-31T12:02:47.890465Z","shell.execute_reply.started":"2021-07-31T12:02:47.563124Z","shell.execute_reply":"2021-07-31T12:02:47.889854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.merge(games_schdule[['Tm', \"before_date\", \"isgame_schedule\"]],left_on = ['date','abbreviation'],right_on = ['before_date','Tm'], how = 'left')\nprint(\"4\",train.shape)\ntrain=train.rename(columns={\"isgame_schedule\": \"isgame_schedule_tom\"})\ntrain = train.drop([\"Tm\", \"before_date\"], axis=1)\ntrain = train.merge(games_schdule[['Tm', \"date\", \"isgame_schedule\"]],left_on = ['date','abbreviation'],right_on = ['date','Tm'], how = 'left')\nprint(\"5\",train.shape)\n\ntrain[\"isgame_schedule\"]=train[\"isgame_schedule\"].fillna(0)\ntrain[\"isgame_schedule_tom\"]=train[\"isgame_schedule_tom\"].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:02:47.891729Z","iopub.execute_input":"2021-07-31T12:02:47.892097Z","iopub.status.idle":"2021-07-31T12:03:04.647997Z","shell.execute_reply.started":"2021-07-31T12:02:47.892061Z","shell.execute_reply":"2021-07-31T12:03:04.647168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"regualar_season_index=train[(pd.to_datetime(train['date'], format='%Y%m%d') > \"2017-04-02\") & (pd.to_datetime(train['date'], format='%Y%m%d') < \"2017-10-01\") |\n(pd.to_datetime(train['date'], format='%Y%m%d') > \"2018-03-29\") & (pd.to_datetime(train['date'], format='%Y%m%d') < \"2018-10-01\") |\n(pd.to_datetime(train['date'], format='%Y%m%d') > \"2019-03-29\") & (pd.to_datetime(train['date'], format='%Y%m%d') < \"2019-09-29\") |\n(pd.to_datetime(train['date'], format='%Y%m%d') > \"2020-07-23\") & (pd.to_datetime(train['date'], format='%Y%m%d') < \"2020-09-27\") |\n(pd.to_datetime(train['date'], format='%Y%m%d') > \"2021-04-01\") & (pd.to_datetime(train['date'], format='%Y%m%d') < \"2021-10-03\")][[\"isgame_schedule_tom\", \"isgame_schedule\"]].index","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:03:04.648994Z","iopub.execute_input":"2021-07-31T12:03:04.649217Z","iopub.status.idle":"2021-07-31T12:03:19.405876Z","shell.execute_reply.started":"2021-07-31T12:03:04.649192Z","shell.execute_reply":"2021-07-31T12:03:19.404540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['HappyBirthDay'] = train['date'].map(lambda x: str(x)[-4:]) == (pd.to_datetime(train[\"DOB\"], format='%Y-%m-%d')+ timedelta(days=-1)).map(lambda x: x.strftime('%m%d'))\ntrain[\"starter_or_not\"] = train[\"gamesPlayedPitching\"] + train[\"gamesStartedPitching\"]","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:03:19.407439Z","iopub.execute_input":"2021-07-31T12:03:19.407697Z","iopub.status.idle":"2021-07-31T12:03:35.958318Z","shell.execute_reply.started":"2021-07-31T12:03:19.407671Z","shell.execute_reply":"2021-07-31T12:03:35.957450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# country2num = {c: i for i, c in enumerate(train['birthCountry'].unique())}\nplayer2num = {c: i for i, c in enumerate(train['playerId'].unique())}\nposition2num = {c: i for i, c in enumerate(train['primaryPositionName'].unique())}\nteamid2num = {c: i for i, c in enumerate(train['teamId'].unique())}\nstatus2num = {c: i for i, c in enumerate(train['status'].unique())}\n# train['label_country_id'] = train['birthCountry'].map(country2num)\ntrain['label_playerId'] = train['playerId'].map(player2num)\ntrain['label_primaryPositionName'] = train['primaryPositionName'].map(position2num)\ntrain['label_teamId'] = train['teamId'].map(teamid2num)\ntrain['label_status'] = train['status'].map(status2num)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:03:35.959335Z","iopub.execute_input":"2021-07-31T12:03:35.959565Z","iopub.status.idle":"2021-07-31T12:03:37.820121Z","shell.execute_reply.started":"2021-07-31T12:03:35.959537Z","shell.execute_reply":"2021-07-31T12:03:37.819265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# trainの特徴量エンジニアリング","metadata":{}},{"cell_type":"code","source":"train[\"gamesStartedPitching_shift_one\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(1))\ntrain[\"gamesStartedPitching_shift_two\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(2))\ntrain[\"gamesStartedPitching_shift_three\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(3))\ntrain[\"gamesStartedPitching_shift_four\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(4))\ntrain[\"gamesStartedPitching_shift_five\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(5))\ntrain[\"gamesStartedPitching_shift_six\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(6))\ntrain[\"gamesStartedPitching_shift_seven\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(7))\ntrain[\"gamesStartedPitching_shift_eight\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(8))\ntrain[\"gamesStartedPitching_shift_nine\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(9))\ntrain[\"gamesStartedPitching_shift_ten\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(10))\ntrain[\"gamesStartedPitching_shift_ele\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(11))\ntrain[\"gamesStartedPitching_shift_twe\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(12))\ntrain[\"gamesStartedPitching_shift_thirteen\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(13))\ntrain[\"gamesStartedPitching_shift_fourteen\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(14))\ntrain[\"gamesStartedPitching_shift_fifteen\"]=train.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(15))","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:03:37.821292Z","iopub.execute_input":"2021-07-31T12:03:37.821518Z","iopub.status.idle":"2021-07-31T12:03:49.060881Z","shell.execute_reply.started":"2021-07-31T12:03:37.821495Z","shell.execute_reply":"2021-07-31T12:03:49.059983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['pitchingGameScore'] = (40\n    + 2 * train['outsPitching']\n    + 1 * train['strikeOutsPitching']\n    - 2 * train['baseOnBallsPitching']\n    - 2 * train['hitsPitching']\n    - 3 * train['runsPitching']\n    - 6 * train['homeRunsPitching']\n    )\n# https://maddog31.xyz/baseball-web/contribution_degree/contribution_degree2/\ntrain[\"batter_contribution\"]=train[\"totalBases\"]+train[\"baseOnBalls\"]+train[\"intentionalWalks\"]+train[\"hitByPitch\"]+train[\"stolenBases\"]+\\\ntrain[\"sacBunts\"]*0.5+train[\"sacFlies\"]*0.5+train[\"runsScored\"]*0.25+train[\"rbi\"]*0.25-\\\ntrain[\"groundIntoDoublePlay\"]-train[\"caughtStealing\"]\n\ntrain[\"pitchingGameScore_rolling_mean_t7\"]=train.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(7, min_periods=1).mean())\ntrain[\"pitchingGameScore_rolling_mean_t14\"]=train.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(14, min_periods=2).mean())\ntrain[\"pitchingGameScore_rolling_mean_t28\"]=train.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(28, min_periods=3).mean())\ntrain[\"pitchingGameScore_rolling_std_t7\"]=train.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(7, min_periods=1).std())\ntrain[\"pitchingGameScore_rolling_std_t14\"]=train.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(14, min_periods=2).std())\ntrain[\"pitchingGameScore_rolling_std_t28\"]=train.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(28, min_periods=3).std())\n\ntrain[\"batter_contribution_rolling_mean_t3\"]=train.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(3, min_periods=2).mean())\ntrain[\"batter_contribution_rolling_mean_t5\"]=train.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(5, min_periods=3).mean())\ntrain[\"batter_contribution_rolling_mean_t7\"]=train.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(7, min_periods=5).mean())\ntrain[\"batter_contribution_rolling_mean_t10\"]=train.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(10, min_periods=6).mean())\ntrain[\"batter_contribution_rolling_mean_t15\"]=train.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(15, min_periods=7).mean())\ntrain[\"batter_contribution_rolling_std_t3\"]=train.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(3, min_periods=2).std())\ntrain[\"batter_contribution_rolling_std_t5\"]=train.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(5, min_periods=3).std())\ntrain[\"batter_contribution_rolling_std_t7\"]=train.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(7, min_periods=5).std())\ntrain[\"batter_contribution_rolling_std_t10\"]=train.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(10, min_periods=6).std())\ntrain[\"batter_contribution_rolling_std_t15\"]=train.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(15, min_periods=7).std())","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:03:49.062056Z","iopub.execute_input":"2021-07-31T12:03:49.062384Z","iopub.status.idle":"2021-07-31T12:04:15.835781Z","shell.execute_reply.started":"2021-07-31T12:03:49.062347Z","shell.execute_reply":"2021-07-31T12:04:15.834864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"stater_or_not_mean_t7\"]=train.groupby([\"playerId\"])[\"starter_or_not\"].transform(lambda x: x.shift(1).rolling(7, min_periods=1).mean())\ntrain[\"stater_or_not_mean_t14\"]=train.groupby([\"playerId\"])[\"starter_or_not\"].transform(lambda x: x.shift(1).rolling(14, min_periods=2).mean())\ntrain[\"stater_or_not_mean_t28\"]=train.groupby([\"playerId\"])[\"starter_or_not\"].transform(lambda x: x.shift(1).rolling(28, min_periods=4).mean())","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:04:15.837221Z","iopub.execute_input":"2021-07-31T12:04:15.837539Z","iopub.status.idle":"2021-07-31T12:04:20.942037Z","shell.execute_reply.started":"2021-07-31T12:04:15.837511Z","shell.execute_reply":"2021-07-31T12:04:20.940672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:04:20.945826Z","iopub.execute_input":"2021-07-31T12:04:20.946127Z","iopub.status.idle":"2021-07-31T12:04:21.050533Z","shell.execute_reply.started":"2021-07-31T12:04:20.946102Z","shell.execute_reply":"2021-07-31T12:04:21.049475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"target1_shift_three\"]=train.groupby([\"playerId\"])[\"target1\"].transform(lambda x:x.shift(3))\ntrain[\"target2_shift_three\"]=train.groupby([\"playerId\"])[\"target2\"].transform(lambda x:x.shift(3))\ntrain[\"target3_shift_three\"]=train.groupby([\"playerId\"])[\"target3\"].transform(lambda x:x.shift(3))\ntrain[\"target4_shift_three\"]=train.groupby([\"playerId\"])[\"target4\"].transform(lambda x:x.shift(3))\n\ntrain[\"target1_shift_four\"]=train.groupby([\"playerId\"])[\"target1\"].transform(lambda x:x.shift(4))\ntrain[\"target2_shift_four\"]=train.groupby([\"playerId\"])[\"target2\"].transform(lambda x:x.shift(4))\ntrain[\"target3_shift_four\"]=train.groupby([\"playerId\"])[\"target3\"].transform(lambda x:x.shift(4))\ntrain[\"target4_shift_four\"]=train.groupby([\"playerId\"])[\"target4\"].transform(lambda x:x.shift(4))\n\ntrain[\"target1_shift_five\"]=train.groupby([\"playerId\"])[\"target1\"].transform(lambda x:x.shift(5))\ntrain[\"target2_shift_five\"]=train.groupby([\"playerId\"])[\"target2\"].transform(lambda x:x.shift(5))\ntrain[\"target3_shift_five\"]=train.groupby([\"playerId\"])[\"target3\"].transform(lambda x:x.shift(5))\ntrain[\"target4_shift_five\"]=train.groupby([\"playerId\"])[\"target4\"].transform(lambda x:x.shift(5))\n\ntrain[\"target1_shift_six\"]=train.groupby([\"playerId\"])[\"target1\"].transform(lambda x:x.shift(6))\ntrain[\"target2_shift_six\"]=train.groupby([\"playerId\"])[\"target2\"].transform(lambda x:x.shift(6))\ntrain[\"target3_shift_six\"]=train.groupby([\"playerId\"])[\"target3\"].transform(lambda x:x.shift(6))\ntrain[\"target4_shift_six\"]=train.groupby([\"playerId\"])[\"target4\"].transform(lambda x:x.shift(6))\n\ntrain[\"target1_shift_seven\"]=train.groupby([\"playerId\"])[\"target1\"].transform(lambda x:x.shift(7))\ntrain[\"target2_shift_seven\"]=train.groupby([\"playerId\"])[\"target2\"].transform(lambda x:x.shift(7))\ntrain[\"target3_shift_seven\"]=train.groupby([\"playerId\"])[\"target3\"].transform(lambda x:x.shift(7))\ntrain[\"target4_shift_seven\"]=train.groupby([\"playerId\"])[\"target4\"].transform(lambda x:x.shift(7))","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:04:21.052173Z","iopub.execute_input":"2021-07-31T12:04:21.052640Z","iopub.status.idle":"2021-07-31T12:04:43.738219Z","shell.execute_reply.started":"2021-07-31T12:04:21.052606Z","shell.execute_reply":"2021-07-31T12:04:43.737438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 推論時に消す(下2つ)","metadata":{}},{"cell_type":"code","source":"train=reduce_mem_usage(train)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:04:43.739573Z","iopub.execute_input":"2021-07-31T12:04:43.740118Z","iopub.status.idle":"2021-07-31T12:05:06.342853Z","shell.execute_reply.started":"2021-07-31T12:04:43.740076Z","shell.execute_reply":"2021-07-31T12:05:06.341903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.iloc[regualar_season_index].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:05:06.344080Z","iopub.execute_input":"2021-07-31T12:05:06.344373Z","iopub.status.idle":"2021-07-31T12:05:08.349308Z","shell.execute_reply.started":"2021-07-31T12:05:06.344341Z","shell.execute_reply":"2021-07-31T12:05:08.348313Z"},"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'] < 20210701)\nx_train1 = train_X.loc[_index].reset_index(drop=True)\ny_train1 = train_y.loc[_index].reset_index(drop=True)\nx_valid1 = train_X.loc[~_index].reset_index(drop=True)\ny_valid1 = train_y.loc[~_index].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:05:08.350500Z","iopub.execute_input":"2021-07-31T12:05:08.350802Z","iopub.status.idle":"2021-07-31T12:05:09.016112Z","shell.execute_reply.started":"2021-07-31T12:05:08.350773Z","shell.execute_reply":"2021-07-31T12:05:09.015182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = train[feature_cols2]\ntrain_y = train[['target1', 'target2', 'target3', 'target4']]\n\n_index = (train['date'] < 20210701)\nx_train2 = train_X.loc[_index].reset_index(drop=True)\ny_train2 = train_y.loc[_index].reset_index(drop=True)\nx_valid2 = train_X.loc[~_index].reset_index(drop=True)\ny_valid2 = train_y.loc[~_index].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:05:09.017193Z","iopub.execute_input":"2021-07-31T12:05:09.017435Z","iopub.status.idle":"2021-07-31T12:05:09.685626Z","shell.execute_reply.started":"2021-07-31T12:05:09.017407Z","shell.execute_reply":"2021-07-31T12:05:09.684708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#推論時にon\ntest_first = train.copy()\ntest_first=test_first[(test_first.date > 20210101)].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:05:09.686816Z","iopub.execute_input":"2021-07-31T12:05:09.687181Z","iopub.status.idle":"2021-07-31T12:05:10.164161Z","shell.execute_reply.started":"2021-07-31T12:05:09.687145Z","shell.execute_reply":"2021-07-31T12:05:10.163074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_X\ndel train_y\ndel train\n\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:05:10.167967Z","iopub.execute_input":"2021-07-31T12:05:10.168208Z","iopub.status.idle":"2021-07-31T12:05:10.333375Z","shell.execute_reply.started":"2021-07-31T12:05:10.168183Z","shell.execute_reply":"2021-07-31T12:05:10.332196Z"},"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\n\nparams1 = {'objective':'mae',\n           'reg_alpha': 0.14947461820098767, \n           'reg_lambda': 0.10185644384043743, \n           'n_estimators': 3633, \n           'learning_rate': 0.08046301304430488, \n           'num_leaves': 674, \n           'feature_fraction': 0.9101240539122566, \n           'bagging_fraction': 0.9884451442950513, \n           'bagging_freq': 8, \n           'min_child_samples': 51}\n\nparams2 = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 80,\n 'learning_rate': 0.1,\n 'random_state': 42,\n \"num_leaves\": 22\n}\n\nparams4 = {'objective':'mae',\n           'reg_alpha': 0.016468100279441976, \n           'reg_lambda': 0.09128335764019105, \n           'n_estimators': 9868, \n           'learning_rate': 0.10528150510326864, \n           'num_leaves': 157, \n           'feature_fraction': 0.5419185713426886, \n           'bagging_fraction': 0.2637405128936662, \n           'bagging_freq': 19, \n           'min_child_samples': 71}\n\n\nparams = {\n 'objective':'mae',\n 'reg_alpha': 0.1,\n 'reg_lambda': 0.1, \n 'n_estimators': 10000,\n 'learning_rate': 0.1,\n 'random_state': 42,\n \"num_leaves\": 100\n}\n\n\noof1, model1, score1 = fit_lgbm(\n    x_train1, y_train1['target1'],\n    x_valid1, y_valid1['target1'],\n    params1\n )\n\noof2, model2, score2 = fit_lgbm(\n    x_train2, y_train2['target2'],\n    x_valid2, y_valid2['target2'],\n    params2\n)\n\noof3, model3, score3 = fit_lgbm(\n    x_train2, y_train2['target3'],\n    x_valid2, y_valid2['target3'],\n   params\n)\n\noof4, model4, score4 = fit_lgbm(\n    x_train2, y_train2['target4'],\n    x_valid2, y_valid2['target4'],\n    params4\n)\n\nscore = (score1+score2+score3+score4) / 4\nprint(f'score: {score}')","metadata":{"_uuid":"c616b4b9-961c-4fcc-9643-391e8225a43c","_cell_guid":"50c7c4dd-9076-4e0a-98bc-bb2e56f0ecb4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-07-31T12:05:10.335250Z","iopub.execute_input":"2021-07-31T12:05:10.335551Z","iopub.status.idle":"2021-07-31T12:10:17.638064Z","shell.execute_reply.started":"2021-07-31T12:05:10.335521Z","shell.execute_reply":"2021-07-31T12:10:17.637225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# stage=\"stage1\"\n# with open(f'model_lgb_{stage}_{version}_1.pkl', 'wb') as handle:\n#     pickle.dump(model1, handle, protocol=pickle.HIGHEST_PROTOCOL)\n\n# with open(f'model_lgb_{stage}_{version}_2.pkl', 'wb') as handle:\n#     pickle.dump(model2, handle, protocol=pickle.HIGHEST_PROTOCOL)\n    \n# with open(f'model_lgb_{stage}_{version}_3.pkl', 'wb') as handle:\n#     pickle.dump(model3, handle, protocol=pickle.HIGHEST_PROTOCOL)\n    \n# with open(f'model_lgb_{stage}_{version}_4.pkl', 'wb') as handle:\n#     pickle.dump(model4, handle, protocol=pickle.HIGHEST_PROTOCOL)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:10:17.641785Z","iopub.execute_input":"2021-07-31T12:10:17.643135Z","iopub.status.idle":"2021-07-31T12:10:17.647938Z","shell.execute_reply.started":"2021-07-31T12:10:17.643019Z","shell.execute_reply":"2021-07-31T12:10:17.646669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 推論時に消す(下1つ)","metadata":{}},{"cell_type":"code","source":"del x_train2\ndel y_train2\ndel x_valid2\ndel y_valid2\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:10:17.649432Z","iopub.execute_input":"2021-07-31T12:10:17.649786Z","iopub.status.idle":"2021-07-31T12:10:17.798083Z","shell.execute_reply.started":"2021-07-31T12:10:17.649747Z","shell.execute_reply":"2021-07-31T12:10:17.796845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# stage=\"stage1\"\n# with open(f'../input/{modelsver}/model_lgb_{stage}_{version}_1.pkl', 'rb') as handle:\n#     model1=pickle.load(handle)\n# with open(f'../input/{modelsver}/model_lgb_{stage}_{version}_2.pkl', 'rb') as handle:\n#     model2=pickle.load(handle)\n# with open(f'../input/{modelsver}/model_lgb_{stage}_{version}_3.pkl', 'rb') as handle:\n#     model3=pickle.load(handle)    \n# with open(f'../input/{modelsver}/model_lgb_{stage}_{version}_4.pkl', 'rb') as handle:\n#     model4=pickle.load(handle)","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:10:17.799317Z","iopub.execute_input":"2021-07-31T12:10:17.799657Z","iopub.status.idle":"2021-07-31T12:10:17.811860Z","shell.execute_reply.started":"2021-07-31T12:10:17.799630Z","shell.execute_reply":"2021-07-31T12:10:17.810988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stage=\"stage2\"","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:10:17.812871Z","iopub.execute_input":"2021-07-31T12:10:17.813411Z","iopub.status.idle":"2021-07-31T12:10:17.833081Z","shell.execute_reply.started":"2021-07-31T12:10:17.813376Z","shell.execute_reply":"2021-07-31T12:10:17.832097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nfrom catboost import CatBoostRegressor\n\ndef fit_lgbm(x_train, y_train, x_valid, y_valid, target, params: dict=None, verbose=100):\n    oof_pred_lgb = np.zeros(len(y_valid), dtype=np.float32)\n    oof_pred_cat = np.zeros(len(y_valid), dtype=np.float32)\n    \n#     if os.path.isfile(f'../input/mlb-lgbm-and-catboost-models/model_lgb_{target}.pkl'):\n#         with open(f'../input/mlb-lgbm-and-catboost-models/model_lgb_{stage}_{version}_{target}.pkl', 'rb') as fin:\n#             model = pickle.load(fin)\n#     else:\n    \n    model = lgbm.LGBMRegressor(**params)\n    model.fit(x_train, y_train, \n        eval_set=[(x_valid, y_valid)],  \n        early_stopping_rounds=verbose, \n        verbose=verbose)\n\n    with open(f'model_lgb_{stage}_{version}_{target}.pkl', 'wb') as handle:\n        pickle.dump(model, handle, protocol=pickle.HIGHEST_PROTOCOL)\n    \n    oof_pred_lgb = model.predict(x_valid)\n    score_lgb = mean_absolute_error(oof_pred_lgb, y_valid)\n    print('mae:', score_lgb)\n    \n#     if os.path.isfile(f'../input/mlb-lgbm-and-catboost-models/model_cb_{target}.pkl'):\n#         with open(f'../input/mlb-lgbm-and-catboost-models/model_cb_{stage}_{version}_{target}.pkl', 'rb') as fin:\n#             model_cb = pickle.load(fin)\n#     else:\n    \n    model_cb = CatBoostRegressor(\n                n_estimators=2000,\n                learning_rate=0.05,\n                loss_function='MAE',\n                eval_metric='MAE',\n                max_bin=50,\n                subsample=0.9,\n                colsample_bylevel=0.5,\n                verbose=100)\n\n    model_cb.fit(x_train, y_train, use_best_model=True,\n                     eval_set=(x_valid, y_valid),\n                     early_stopping_rounds=25)\n\n    with open(f'model_cb_{stage}_{version}_{target}.pkl', 'wb') as handle:\n        pickle.dump(model_cb, handle, protocol=pickle.HIGHEST_PROTOCOL)\n    \n    oof_pred_cat = model_cb.predict(x_valid)\n    score_cat = mean_absolute_error(oof_pred_cat, y_valid)\n    print('mae:', score_cat)\n    \n    return oof_pred_lgb, model, oof_pred_cat, model_cb, score_lgb, score_cat\n\n\n# training lightgbm\nparams = {\n'boosting_type': 'gbdt',\n'objective':'mae',\n'subsample': 0.5,\n'subsample_freq': 1,\n'learning_rate': 0.03,\n'num_leaves': 2**11-1,\n'min_data_in_leaf': 2**12-1,\n'feature_fraction': 0.5,\n'max_bin': 100,\n'n_estimators': 2500,\n'boost_from_average': False,\n\"random_seed\":42,\n}\n\noof_pred_lgb2, model_lgb2, oof_pred_cat2, model_cb2, score_lgb2, score_cat2 = fit_lgbm(\n    x_train1, y_train1['target2'],\n    x_valid1, y_valid1['target2'],\n    2, params\n)\n\noof_pred_lgb1, model_lgb1, oof_pred_cat1, model_cb1, score_lgb1, score_cat1 = fit_lgbm(\n    x_train1, y_train1['target1'],\n    x_valid1, y_valid1['target1'],\n    1, params\n)\n\noof_pred_lgb3, model_lgb3, oof_pred_cat3, model_cb3, score_lgb3, score_cat3 = fit_lgbm(\n    x_train1, y_train1['target3'],\n    x_valid1, y_valid1['target3'],\n    3, params\n)\noof_pred_lgb4, model_lgb4, oof_pred_cat4, model_cb4, score_lgb4, score_cat4= fit_lgbm(\n    x_train1, y_train1['target4'],\n    x_valid1, y_valid1['target4'],\n    4, params\n)\n\nscore = (score_lgb1+score_lgb2+score_lgb3+score_lgb4) / 4\nprint(f'LightGBM score: {score}')\n\nscore = (score_cat1+score_cat2+score_cat3+score_cat4) / 4\nprint(f'Catboost score: {score}')","metadata":{"execution":{"iopub.status.busy":"2021-07-31T12:10:17.834144Z","iopub.execute_input":"2021-07-31T12:10:17.834468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# stage=\"stage2\"\n\n# with open(f'../input/{modelsver}/model_lgb_{stage}_{version}_1.pkl', 'rb') as handle:\n#     model_lgb1=pickle.load(handle)\n# with open(f'../input/{modelsver}/model_cb_{stage}_{version}_1.pkl', 'rb') as handle:\n#     model_cb1=pickle.load(handle)\n# with open(f'../input/{modelsver}/model_lgb_{stage}_{version}_2.pkl', 'rb') as handle:\n#     model_lgb2=pickle.load(handle)\n# with open(f'../input/{modelsver}/model_cb_{stage}_{version}_2.pkl', 'rb') as handle:\n#     model_cb2=pickle.load(handle)\n    \n# with open(f'../input/{modelsver}/model_lgb_{stage}_{version}_3.pkl', 'rb') as handle:\n#     model_lgb3=pickle.load(handle)\n# with open(f'../input/{modelsver}/model_cb_{stage}_{version}_3.pkl', 'rb') as handle:\n#     model_cb3=pickle.load(handle)\n    \n# with open(f'../input/{modelsver}/model_lgb_{stage}_{version}_4.pkl', 'rb') as handle:\n#     model_lgb4=pickle.load(handle)\n# with open(f'../input/{modelsver}/model_cb_{stage}_{version}_4.pkl', 'rb') as handle:\n#     model_cb4=pickle.load(handle)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference","metadata":{"_uuid":"372a2718-6f2d-4128-97cb-fefd653e1f40","_cell_guid":"25cf48a6-84c3-4d5d-a312-1a47c050722e","trusted":true}},{"cell_type":"code","source":"del x_train1\ndel y_train1\ndel x_valid1\ndel y_valid1\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players_cols = ['playerId', 'primaryPositionName', \"mlbDebutDate\",\"DOB\"]\nrosters_cols = ['playerId', 'teamId', 'status']\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']\n\nnull = np.nan\ntrue = True\nfalse = False","metadata":{"_uuid":"7828a113-8dc7-4691-b2a6-b232ca0525dc","_cell_guid":"08cf9a6e-1f2c-4acd-b714-703d641d996a","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 推論","metadata":{}},{"cell_type":"code","source":"# del train\n\ngc.collect()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import mlb","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import copy\n\nenv = mlb.make_env() # initialize the environment\niter_test = env.iter_test() # iterator which loops over each date in test set\ngc.collect()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (test_df, sample_prediction_df) in iter_test: # make predictions here\n    print(\"0\",test_first.shape)\n    \n    sub = copy.deepcopy(sample_prediction_df.reset_index())\n    sample_prediction_df = copy.deepcopy(sample_prediction_df.reset_index(drop=True))\n    \n    # LGBM summit\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    sample_prediction_df['date']=(pd.to_datetime(sample_prediction_df['date'], format='%Y%m%d') + timedelta(days=-1)).map(lambda x:str(x.year) + str(x.month).zfill(2)+ str(x.day).zfill(2)).astype(int)\n    print(\"日付:\", sample_prediction_df['date'].iloc[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', \"date\"]].copy()\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(player_target_stats, how='inner', left_on=[\"playerId\"],right_on=[\"playerId\"])\n    test = test.merge(teams, how='left', left_on=[\"teamId\"],right_on=[\"id\"])\n    test[\"year_month\"] = pd.to_datetime(test['date'], format=\"%Y%m%d\").map(lambda x:str(x.year) + str(x.month).zfill(2)).astype(int)\n#     test = test.merge(twitter[[\"date\", \"playerId\",\"numberOfFollowers\"]], how='left', left_on=[\"playerId\", \"year_month\"],right_on=[\"playerId\", \"date\"])\n    test=test.rename(columns={\"date_x\": \"date\"})\n    \n    test = test.merge(games_schdule[['Tm', \"before_date\", \"isgame_schedule\"]],left_on = ['date','abbreviation'],right_on = ['before_date','Tm'], how = 'left')\n    test=test.rename(columns={\"isgame_schedule\": \"isgame_schedule_tom\"})\n    test = test.drop([\"Tm\", \"before_date\"], axis=1)\n    test = test.merge(games_schdule[['Tm', \"date\", \"isgame_schedule\"]],left_on = ['date','abbreviation'],right_on = ['date','Tm'], how = 'left')\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_status'] = test['status'].map(status2num)\n\n    test['HappyBirthDay'] = test['date'].map(lambda x: str(x)[-4:]) == (pd.to_datetime(test[\"DOB\"], format='%Y-%m-%d')+ timedelta(days=-1)).map(lambda x: x.strftime('%m%d'))\n    test[\"starter_or_not\"] = test[\"gamesPlayedPitching\"] + test[\"gamesStartedPitching\"]\n    test=reduce_mem_usage(test)\n    \n    \n    \n    ## test_trainのラベル\n    test[\"test_train_label\"] = \"test\"\n    test_first[\"test_train_label\"]=\"train\"\n    test['index'] = test.reset_index().index\n    \n    print(\"1\",test_first.shape)\n#     test_first = test_first.drop([\"pitced_yeasterday\", \"pitched_yes\"], axis=1)\n    test_first=pd.concat([test, test_first], axis=0, ignore_index=True).sort_values(\"date\")\n    test_first=test_first.rename(columns={\"date_x\": \"date\"})\n    test_first['pitchingGameScore'] = (40 + 2 * test_first['outsPitching']+ 1 * test_first['strikeOutsPitching']- 2 * test_first['baseOnBallsPitching']- 2 * test_first['hitsPitching']- 3 * test_first['runsPitching']- 6 * test_first['homeRunsPitching'])\n\n    test_first[\"gamesStartedPitching_shift_one\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(1))\n    test_first[\"gamesStartedPitching_shift_two\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(2))\n    test_first[\"gamesStartedPitching_shift_three\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(3))\n    test_first[\"gamesStartedPitching_shift_four\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(4))\n    test_first[\"gamesStartedPitching_shift_five\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(5))\n    test_first[\"gamesStartedPitching_shift_six\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(6))\n    test_first[\"gamesStartedPitching_shift_seven\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(7))\n    test_first[\"gamesStartedPitching_shift_eight\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(8))\n    test_first[\"gamesStartedPitching_shift_nine\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(9))\n    test_first[\"gamesStartedPitching_shift_ten\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(10))\n    test_first[\"gamesStartedPitching_shift_ele\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(11))\n    test_first[\"gamesStartedPitching_shift_twe\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(12))\n    test_first[\"gamesStartedPitching_shift_thirteen\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(13))\n    test_first[\"gamesStartedPitching_shift_fourteen\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(14))\n    test_first[\"gamesStartedPitching_shift_fifteen\"]=test_first.groupby([\"playerId\"])[\"gamesStartedPitching\"].transform(lambda x:x.shift(15))\n    \n    test_first[\"target1_shift_three\"]=test_first.groupby([\"playerId\"])[\"target1\"].transform(lambda x:x.shift(3))\n    test_first[\"target2_shift_three\"]=test_first.groupby([\"playerId\"])[\"target2\"].transform(lambda x:x.shift(3))\n    test_first[\"target3_shift_three\"]=test_first.groupby([\"playerId\"])[\"target3\"].transform(lambda x:x.shift(3))\n    test_first[\"target4_shift_three\"]=test_first.groupby([\"playerId\"])[\"target4\"].transform(lambda x:x.shift(3))\n    test_first[\"target1_shift_four\"]=test_first.groupby([\"playerId\"])[\"target1\"].transform(lambda x:x.shift(4))\n    test_first[\"target2_shift_four\"]=test_first.groupby([\"playerId\"])[\"target2\"].transform(lambda x:x.shift(4))\n    test_first[\"target3_shift_four\"]=test_first.groupby([\"playerId\"])[\"target3\"].transform(lambda x:x.shift(4))\n    test_first[\"target4_shift_four\"]=test_first.groupby([\"playerId\"])[\"target4\"].transform(lambda x:x.shift(4))\n    test_first[\"target1_shift_five\"]=test_first.groupby([\"playerId\"])[\"target1\"].transform(lambda x:x.shift(5))\n    test_first[\"target2_shift_five\"]=test_first.groupby([\"playerId\"])[\"target2\"].transform(lambda x:x.shift(5))\n    test_first[\"target3_shift_five\"]=test_first.groupby([\"playerId\"])[\"target3\"].transform(lambda x:x.shift(5))\n    test_first[\"target4_shift_five\"]=test_first.groupby([\"playerId\"])[\"target4\"].transform(lambda x:x.shift(5))\n    test_first[\"target1_shift_six\"]=test_first.groupby([\"playerId\"])[\"target1\"].transform(lambda x:x.shift(6))\n    test_first[\"target2_shift_six\"]=test_first.groupby([\"playerId\"])[\"target2\"].transform(lambda x:x.shift(6))\n    test_first[\"target3_shift_six\"]=test_first.groupby([\"playerId\"])[\"target3\"].transform(lambda x:x.shift(6))\n    test_first[\"target4_shift_six\"]=test_first.groupby([\"playerId\"])[\"target4\"].transform(lambda x:x.shift(6))\n    test_first[\"target1_shift_seven\"]=test_first.groupby([\"playerId\"])[\"target1\"].transform(lambda x:x.shift(7))\n    test_first[\"target2_shift_seven\"]=test_first.groupby([\"playerId\"])[\"target2\"].transform(lambda x:x.shift(7))\n    test_first[\"target3_shift_seven\"]=test_first.groupby([\"playerId\"])[\"target3\"].transform(lambda x:x.shift(7))\n    test_first[\"target4_shift_seven\"]=test_first.groupby([\"playerId\"])[\"target4\"].transform(lambda x:x.shift(7))\n\n    test_first[\"batter_contribution\"]=test_first[\"totalBases\"]+test_first[\"baseOnBalls\"]+test_first[\"intentionalWalks\"]+test_first[\"hitByPitch\"]+test_first[\"stolenBases\"]+\\\n    test_first[\"sacBunts\"]*0.5+test_first[\"sacFlies\"]*0.5+test_first[\"runsScored\"]*0.25+test_first[\"rbi\"]*0.25-\\\n    test_first[\"groundIntoDoublePlay\"]-test_first[\"caughtStealing\"]\n                                       \n                                       \n    test_first[\"pitchingGameScore_rolling_mean_t7\"]=test_first.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(7, min_periods=1).mean())\n    test_first[\"pitchingGameScore_rolling_mean_t14\"]=test_first.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(14, min_periods=2).mean())\n    test_first[\"pitchingGameScore_rolling_mean_t28\"]=test_first.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(28, min_periods=3).mean())\n    test_first[\"pitchingGameScore_rolling_std_t7\"]=test_first.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(7, min_periods=1).std())\n    test_first[\"pitchingGameScore_rolling_std_t14\"]=test_first.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(14, min_periods=2).std())\n    test_first[\"pitchingGameScore_rolling_std_t28\"]=test_first.groupby([\"playerId\"])[\"pitchingGameScore\"].transform(lambda x: x.shift(1).rolling(28, min_periods=3).std())\n\n    test_first[\"batter_contribution_rolling_mean_t3\"]=test_first.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(3, min_periods=2).mean())\n    test_first[\"batter_contribution_rolling_mean_t5\"]=test_first.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(5, min_periods=3).mean())\n    test_first[\"batter_contribution_rolling_mean_t7\"]=test_first.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(7, min_periods=5).mean())\n    test_first[\"batter_contribution_rolling_mean_t10\"]=test_first.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(10, min_periods=6).mean())\n    test_first[\"batter_contribution_rolling_mean_t15\"]=test_first.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(15, min_periods=7).mean())\n\n    test_first[\"batter_contribution_rolling_std_t3\"]=test_first.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(3, min_periods=2).std())\n    test_first[\"batter_contribution_rolling_std_t5\"]=test_first.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(5, min_periods=3).std())\n    test_first[\"batter_contribution_rolling_std_t7\"]=test_first.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(7, min_periods=5).std())\n    test_first[\"batter_contribution_rolling_std_t10\"]=test_first.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(10, min_periods=6).std())\n    test_first[\"batter_contribution_rolling_std_t15\"]=test_first.groupby([\"playerId\"])[\"batter_contribution\"].transform(lambda x: x.shift(1).rolling(15, min_periods=7).std())##新規追加コード\n    \n    test_first[\"stater_or_not_mean_t7\"]=test_first.groupby([\"playerId\"])[\"starter_or_not\"].transform(lambda x: x.shift(1).rolling(7, min_periods=1).mean())\n    test_first[\"stater_or_not_mean_t14\"]=test_first.groupby([\"playerId\"])[\"starter_or_not\"].transform(lambda x: x.shift(1).rolling(14, min_periods=2).mean())\n    test_first[\"stater_or_not_mean_t28\"]=test_first.groupby([\"playerId\"])[\"starter_or_not\"].transform(lambda x: x.shift(1).rolling(28, min_periods=4).mean())\n\n    test = test_first[test_first.test_train_label ==\"test\"].sort_values(\"index\")\n    test_first = test_first[test_first.test_train_label ==\"train\"]\n    test_X = test[feature_cols]\n    # predict\n    pred1 = model1.predict(test_X)\n    \n    # predict\n    pred_lgd1 = model_lgb1.predict(test_X)\n    pred_lgd2 = model_lgb2.predict(test_X)\n    pred_lgd3 = model_lgb3.predict(test_X)\n    pred_lgd4 = model_lgb4.predict(test_X)\n    \n    pred_cat1 = model_cb1.predict(test_X)\n    pred_cat2 = model_cb2.predict(test_X)\n    pred_cat3 = model_cb3.predict(test_X)\n    pred_cat4 = model_cb4.predict(test_X)\n    \n    test['target1'] = np.clip(pred1,0,100)\n    test_X = test[feature_cols2]\n\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'] = 1.00*np.clip(pred1, 0, 100)+0.00*np.clip(pred_lgd1, 0, 100)+0.00*np.clip(pred_cat1, 0, 100)\n    sample_prediction_df['target2'] = 0.10*np.clip(pred2, 0, 100)+0.65*np.clip(pred_lgd2, 0, 100)+0.25*np.clip(pred_cat2, 0, 100)\n    sample_prediction_df['target3'] = 0.65*np.clip(pred3, 0, 100)+0.25*np.clip(pred_lgd3, 0, 100)+0.10*np.clip(pred_cat3, 0, 100)\n    sample_prediction_df['target4'] = 0.65*np.clip(pred4, 0, 100)+0.25*np.clip(pred_lgd4, 0, 100)+0.10*np.clip(pred_cat4, 0, 100)\n    sample_prediction_df = sample_prediction_df.fillna(0.)\n    \n    \n    del sample_prediction_df['playerId']\n    del sample_prediction_df['date']\n\n    # TF summit\n    # Features computation at Evaluation Date\n#     sub_fe, eval_dt = test_lag(sub)\n#     sub_fe = sub_fe.merge(LAST_MED_DF, on=\"playerId\", how=\"left\")\n#     sub_fe = sub_fe.fillna(0.)\n    \n#     _preds = 0.\n#     for reg in nets:\n#         _preds += reg.predict(sub_fe[FECOLS + MEDCOLS]) / NFOLDS\n#     sub_fe[TGTCOLS] = np.clip(_preds, 0, 100)\n#     sub.drop([\"date\"]+TGTCOLS, axis=1, inplace=True)\n#     sub = sub.merge(sub_fe[[\"playerId\"]+TGTCOLS], on=\"playerId\", how=\"left\")\n#     sub.drop(\"playerId\", axis=1, inplace=True)\n#     sub = sub.fillna(0.)\n#     # Blending\n    blend = pd.concat(\n        [sub[['date_playerId']],\n        (0*sub.drop(['date_playerId',\"date\"], axis=1) + 1*sample_prediction_df.drop('date_playerId', axis=1))],\n        axis=1\n    )\n    env.predict(blend)\n#     print(env.maes)\n#     print(env.get_pb_score())\n    \n    test['target1']=blend['target1']\n    test['target2']=blend['target2']\n    test['target3']=blend['target3']\n    test['target4']=blend['target4']\n    test_first=pd.concat([test, test_first], axis=0, ignore_index=True).sort_values(\"date\")\n    \n#     # Update Available information\n#     sub_fe[\"EvalDate\"] = eval_dt\n#     #sub_fe.drop(MEDCOLS, axis=1, inplace=True)\n#     LAST = LAST.append(sub_fe)\n#     LAST = LAST.drop_duplicates(subset=[\"EvalDate\",\"playerId\"], keep=\"last\")","metadata":{"_uuid":"bc76106c-5572-4d75-ae68-28381f7406b7","_cell_guid":"c3148c3f-68b2-46f6-b8d6-3284e68b507a","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_prediction_df","metadata":{"_uuid":"99a89e72-692e-4aff-874b-a9e1d44ddaee","_cell_guid":"e94e45d6-2620-412b-bd5c-cb773241c8fe","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}