{"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":"# Import","metadata":{"papermill":{"duration":0.005626,"end_time":"2022-10-10T19:20:32.884317","exception":false,"start_time":"2022-10-10T19:20:32.878691","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import random\nimport gc\n!pip install datatable\nimport datatable as dt\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport lightgbm as lgb\n#from lightgbm import LGBMClassifier\nfrom sklearn.preprocessing import StandardScaler\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)\n\nfrom sklearn.model_selection import KFold","metadata":{"papermill":{"duration":15.538508,"end_time":"2022-10-10T19:20:48.428772","exception":false,"start_time":"2022-10-10T19:20:32.890264","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-25T07:26:55.272301Z","iopub.execute_input":"2022-10-25T07:26:55.272902Z","iopub.status.idle":"2022-10-25T07:27:10.660243Z","shell.execute_reply.started":"2022-10-25T07:26:55.272760Z","shell.execute_reply":"2022-10-25T07:27:10.659121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Scaling = True\n#scaler = StandardScaler()\n\nEPOCHS = 5000\nEARLY_STOPPING = 30\nFILES = 9\n\nREFIT = False\nCONTINUOUS = False","metadata":{"papermill":{"duration":0.014419,"end_time":"2022-10-10T19:20:48.449630","exception":false,"start_time":"2022-10-10T19:20:48.435211","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-25T07:27:10.662581Z","iopub.execute_input":"2022-10-25T07:27:10.663202Z","iopub.status.idle":"2022-10-25T07:27:10.668349Z","shell.execute_reply.started":"2022-10-25T07:27:10.663153Z","shell.execute_reply":"2022-10-25T07:27:10.667508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{"papermill":{"duration":0.00586,"end_time":"2022-10-10T19:20:48.461573","exception":false,"start_time":"2022-10-10T19:20:48.455713","status":"completed"},"tags":[]}},{"cell_type":"code","source":"dtypes_dict = {\n    'game_num': 'int8', 'event_id': 'int8', 'event_time': 'float16',\n    'ball_pos_x': 'float16', 'ball_pos_y': 'float16', 'ball_pos_z': 'float16',\n    'ball_vel_x': 'float16', 'ball_vel_y': 'float16', 'ball_vel_z': 'float16',\n    'p0_pos_x': 'float16', 'p0_pos_y': 'float16', 'p0_pos_z': 'float16',\n    'p0_vel_x': 'float16', 'p0_vel_y': 'float16', 'p0_vel_z': 'float16',\n    'p0_boost': 'float16', 'p1_pos_x': 'float16', 'p1_pos_y': 'float16',\n    'p1_pos_z': 'float16', 'p1_vel_x': 'float16', 'p1_vel_y': 'float16',\n    'p1_vel_z': 'float16', 'p1_boost': 'float16', 'p2_pos_x': 'float16',\n    'p2_pos_y': 'float16', 'p2_pos_z': 'float16', 'p2_vel_x': 'float16',\n    'p2_vel_y': 'float16', 'p2_vel_z': 'float16', 'p2_boost': 'float16',\n    'p3_pos_x': 'float16', 'p3_pos_y': 'float16', 'p3_pos_z': 'float16',\n    'p3_vel_x': 'float16', 'p3_vel_y': 'float16', 'p3_vel_z': 'float16',\n    'p3_boost': 'float16', 'p4_pos_x': 'float16', 'p4_pos_y': 'float16',\n    'p4_pos_z': 'float16', 'p4_vel_x': 'float16', 'p4_vel_y': 'float16',\n    'p4_vel_z': 'float16', 'p4_boost': 'float16', 'p5_pos_x': 'float16',\n    'p5_pos_y': 'float16', 'p5_pos_z': 'float16', 'p5_vel_x': 'float16',\n    'p5_vel_y': 'float16', 'p5_vel_z': 'float16', 'p5_boost': 'float16',\n    'boost0_timer': 'float16', 'boost1_timer': 'float16', 'boost2_timer': 'float16',\n    'boost3_timer': 'float16', 'boost4_timer': 'float16', 'boost5_timer': 'float16',\n    'player_scoring_next': 'O', 'team_scoring_next': 'O', 'team_A_scoring_within_10sec': 'O',\n    'team_B_scoring_within_10sec': 'O'\n}\n\npath_to_data = '../input/fast-loading-high-compression-with-feather/feather_data'\ndf = pd.DataFrame({}, columns=dtypes_dict.keys())\nfor i in range(FILES):\n    dt_read = pd.read_feather(f'{path_to_data}/train_{i}_compressed.ftr').astype(dtypes_dict)\n    dt_read = dt_read.astype(dtypes_dict)\n    df = pd.concat([df, dt_read])\n    del dt_read\n    gc.collect()\n\ndf_test = pd.read_feather(\"../input/fast-loading-high-compression-with-feather/feather_data/test_compressed.ftr\")\ndf_test.drop(\"id\",axis =1, inplace = True)","metadata":{"papermill":{"duration":42.460568,"end_time":"2022-10-10T19:21:30.928195","exception":false,"start_time":"2022-10-10T19:20:48.467627","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-25T07:27:10.669412Z","iopub.execute_input":"2022-10-25T07:27:10.671019Z","iopub.status.idle":"2022-10-25T07:27:17.193277Z","shell.execute_reply.started":"2022-10-25T07:27:10.670975Z","shell.execute_reply":"2022-10-25T07:27:17.192173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Engineering","metadata":{"papermill":{"duration":0.006697,"end_time":"2022-10-10T19:21:30.943335","exception":false,"start_time":"2022-10-10T19:21:30.936638","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"#### **Assumption:**\nDistance of ball to the goal will have relationship to scoring. We therefore need to identify where the goals are. This can be seen in the [EDA notebook](https://www.kaggle.com/code/slythe/tps-oct-22-eda-of-rocket-league/)","metadata":{"papermill":{"duration":0.006738,"end_time":"2022-10-10T19:21:30.957085","exception":false,"start_time":"2022-10-10T19:21:30.950347","status":"completed"},"tags":[]}},{"cell_type":"code","source":"goal1_location = (0,100,0) #assumption that goal centre is at z= 0  (unknown how high the goal is)\ngoal2_location = (0,-100,0) ","metadata":{"papermill":{"duration":0.017393,"end_time":"2022-10-10T19:21:30.981754","exception":false,"start_time":"2022-10-10T19:21:30.964361","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-25T07:27:17.195653Z","iopub.execute_input":"2022-10-25T07:27:17.196741Z","iopub.status.idle":"2022-10-25T07:27:17.202124Z","shell.execute_reply.started":"2022-10-25T07:27:17.196699Z","shell.execute_reply":"2022-10-25T07:27:17.200771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = [\n    \"p0_pos_x\",\"p1_pos_x\",\"p2_pos_x\",\"p3_pos_x\",\"p4_pos_x\",\"p5_pos_x\",\n    \"p0_pos_y\",\"p1_pos_y\",\"p2_pos_y\",\"p3_pos_y\",\"p4_pos_y\",\"p5_pos_y\",\n    \"p0_pos_z\",\"p1_pos_z\",\"p2_pos_z\",\"p3_pos_z\",\"p4_pos_z\",\"p5_pos_z\",\n    \n    \"p0_vel_x\",\"p1_vel_x\",\"p2_vel_x\",\"p3_vel_x\",\"p4_vel_x\",\"p5_vel_x\",\n    \"p0_vel_y\",\"p1_vel_y\",\"p2_vel_y\",\"p3_vel_y\",\"p4_vel_y\",\"p5_vel_y\",\n    \"p0_vel_z\",\"p1_vel_z\",\"p2_vel_z\",\"p3_vel_z\",\"p4_vel_z\",\"p5_vel_z\",\n    \n    'p0_boost','p1_boost','p2_boost','p3_boost','p4_boost','p5_boost'\n       ]\n\n\ndef fillNan(cols, data):\n    random.seed()\n    num = random.random()\n    for i in cols:\n        data[i] = data[i].fillna(0)\n        data[i] = data[i].replace(0, (sum(data[i])/10676072)-num)\n        \ndef FeatureEngineering(data):\n    # distance of ball to goal1 (0,100,0) and goal2 (0,-100,0) \n    data[f\"goal1_distance\"] = ((data[\"ball_pos_x\"]-goal1_location[0])**2 + (data[\"ball_pos_y\"]-goal1_location[1])**2 + (data[\"ball_pos_z\"]-goal1_location[2])**2)**0.5\n    data[f\"goal2_distance\"] = ((data[\"ball_pos_x\"]-goal2_location[0])**2 + (data[\"ball_pos_y\"]-goal2_location[1])**2 + (data[\"ball_pos_z\"]-goal2_location[2])**2)**0.5\n    \n    #Ball direction \n    #data[\"ball_direction\"] = ((data[\"ball_vel_x\"])**2 + (data[\"ball_vel_y\"])**2 + (data[\"ball_vel_z\"])**2)**0.5\n    \n    #general direction of play of all players\n#     data[\"direction_of_play\"] = ((data[\"p0_vel_x\"]+data[\"p1_vel_x\"]+ data[\"p2_vel_x\"]+ data[\"p3_vel_x\"]+ data[\"p4_vel_x\"]+ data[\"p5_vel_x\"])**2 +\n#                                 (data[\"p0_vel_y\"]+data[\"p1_vel_y\"]+ data[\"p2_vel_y\"]+ data[\"p3_vel_y\"]+ data[\"p4_vel_y\"]+ data[\"p5_vel_y\"])**2 +\n#                                 (data[\"p0_vel_z\"]+data[\"p1_vel_z\"]+ data[\"p2_vel_z\"]+ data[\"p3_vel_z\"]+ data[\"p4_vel_z\"]+ data[\"p5_vel_z\"])**2)**0.5\n    \n    for i in range(6):\n        ## ball distance to player\n        data[f\"p{i}_ball_distance\"] = ((data[\"ball_pos_x\"]-data[f\"p{i}_pos_x\"])**2 + (data[\"ball_pos_y\"]-data[f\"p{i}_pos_y\"])**2 + (data[\"ball_pos_z\"]-data[f\"p{i}_pos_z\"])**2)**0.5\n        ## boost timer\n        #data[f\"p{i}_boost_timer\"] = data[f\"p{i}_boost\"]*data[f\"boost{i}_timer\"]\n        ##Player speed  \n        #data[f\"p{i}_speed\"] = ((data[f\"p{i}_vel_x\"])**2 + (data[f\"p{i}_vel_y\"])**2 + (data[f\"p{i}_vel_z\"])**2)**0.5\n        \n        #Player direction  \n        #data[f\"p{i}_direction\"] = ((data[f\"p{i}_vel_x\"])**2 + (data[f\"p{i}_vel_y\"])**2 + (data[f\"p{i}_vel_z\"])**2)**0.5\n        \n        #if the player hit the ball what would its direction be\n        #multiplied by the inverse of the ball distance (assumption is that the further the player is from the ball the less likely they would collide with ball)\n        #data[f\"p{i}_ball_hit_direction\"] = (1/data[f\"p{i}_ball_distance\"]) * ((data[\"ball_vel_x\"] + data[f\"p{i}_vel_x\"])**2 + (data[\"ball_vel_y\"] + data[f\"p{i}_vel_y\"])**2 + (data[\"ball_vel_z\"] + data[f\"p{i}_vel_z\"])**2)**0.5\n        ","metadata":{"papermill":{"duration":0.027472,"end_time":"2022-10-10T19:21:31.015718","exception":false,"start_time":"2022-10-10T19:21:30.988246","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-25T07:27:17.203647Z","iopub.execute_input":"2022-10-25T07:27:17.204029Z","iopub.status.idle":"2022-10-25T07:27:17.218932Z","shell.execute_reply.started":"2022-10-25T07:27:17.203989Z","shell.execute_reply":"2022-10-25T07:27:17.217799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fillNan(cols, df)\nfillNan(cols, df_test)\n\nFeatureEngineering(df)\nFeatureEngineering(df_test)\n\ndel goal1_location,goal2_location, FeatureEngineering, fillNan","metadata":{"_kg_hide-output":true,"papermill":{"duration":92.567875,"end_time":"2022-10-10T19:23:03.590032","exception":false,"start_time":"2022-10-10T19:21:31.022157","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-25T07:27:17.220442Z","iopub.execute_input":"2022-10-25T07:27:17.220776Z","iopub.status.idle":"2022-10-25T07:27:32.697404Z","shell.execute_reply.started":"2022-10-25T07:27:17.220746Z","shell.execute_reply":"2022-10-25T07:27:32.696351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mirror ","metadata":{}},{"cell_type":"code","source":"def mirrow_dataset(data):\n    \n    X_train_temp = data.copy()\n    \n    X_train_temp['ball_pos_x'] = - data['ball_pos_x']\n    X_train_temp['ball_pos_y'] = - data['ball_pos_y']\n\n    ball=[k for k in data.columns.to_list() if k.startswith(\"ball\")]\n\n    listP0=[k for k in data.columns.to_list() if k.startswith(\"p0\") and not k.endswith(\"boost\")]\n    listP1=[k for k in data.columns.to_list() if k.startswith(\"p1\") and not k.endswith(\"boost\")]\n    listP2=[k for k in data.columns.to_list() if k.startswith(\"p2\") and not k.endswith(\"boost\")]\n    listP3=[k for k in data.columns.to_list() if k.startswith(\"p3\") and not k.endswith(\"boost\")]\n    listP4=[k for k in data.columns.to_list() if k.startswith(\"p4\") and not k.endswith(\"boost\")]\n    listP5=[k for k in data.columns.to_list() if k.startswith(\"p5\") and not k.endswith(\"boost\")]\n\n    vel_list = [k for k in data.columns.to_list() if 'vel' in k]\n    z_list=[k for k in data.columns.to_list() if k.endswith(\"_z\") and k not in vel_list]\n\n    B0=[k for k in data.columns.to_list() if k.startswith(\"p0\") and k.endswith(\"boost\")]\n    B1=[k for k in data.columns.to_list() if k.startswith(\"p1\") and k.endswith(\"boost\")]\n    B2=[k for k in data.columns.to_list() if k.startswith(\"p2\") and k.endswith(\"boost\")]\n    B3=[k for k in data.columns.to_list() if k.startswith(\"p3\") and k.endswith(\"boost\")]\n    B4=[k for k in data.columns.to_list() if k.startswith(\"p4\") and k.endswith(\"boost\")]\n    B5=[k for k in data.columns.to_list() if k.startswith(\"p5\") and k.endswith(\"boost\")]\n\n    Orb0=[k for k in data.columns.to_list() if k.startswith(\"boost0\")]\n    Orb1=[k for k in data.columns.to_list() if k.startswith(\"boost1\")]\n    Orb2=[k for k in data.columns.to_list() if k.startswith(\"boost2\")]\n    Orb3=[k for k in data.columns.to_list() if k.startswith(\"boost3\")]\n    Orb4=[k for k in data.columns.to_list() if k.startswith(\"boost4\")]\n    Orb5=[k for k in data.columns.to_list() if k.startswith(\"boost5\")]\n\n    X_train_temp[ball] = - data[ball]\n\n    X_train_temp[listP0+listP1+listP2] = - data[listP3+listP4+listP5]\n    X_train_temp[listP3+listP4+listP5] = - data[listP0+listP1+listP2]\n\n    X_train_temp[z_list] = np.abs(X_train_temp[z_list])\n\n    X_train_temp[B0+B1+B2] = data[B3+B4+B5]\n    X_train_temp[B3+B4+B5] = data[B0+B1+B2]\n\n    X_train_temp[Orb0]=data[Orb5]\n    X_train_temp[Orb1]=data[Orb4]\n    X_train_temp[Orb2]=data[Orb3]\n    X_train_temp[Orb3]=data[Orb2]\n    X_train_temp[Orb4]=data[Orb1]\n    X_train_temp[Orb5]=data[Orb0]\n\n    X_train_temp['team_A_scoring_within_10sec'] = data['team_B_scoring_within_10sec']\n    X_train_temp['team_B_scoring_within_10sec'] = data['team_A_scoring_within_10sec']\n    \n    data = pd.concat([data,X_train_temp],axis=0)\n    return data","metadata":{"execution":{"iopub.status.busy":"2022-10-25T07:27:32.698601Z","iopub.execute_input":"2022-10-25T07:27:32.699542Z","iopub.status.idle":"2022-10-25T07:27:32.721402Z","shell.execute_reply.started":"2022-10-25T07:27:32.699502Z","shell.execute_reply":"2022-10-25T07:27:32.719684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def shuffling_players (data):\n    \n    train_temp = data.copy()\n    \n    list_columns = data.columns.to_list()\n\n    listP0=[k for k in list_columns if k.startswith(\"p0\")]\n    listP1=[k for k in list_columns if k.startswith(\"p1\")]\n    listP2=[k for k in list_columns if k.startswith(\"p2\")]\n    listP3=[k for k in list_columns if k.startswith(\"p3\")]\n    listP4=[k for k in list_columns if k.startswith(\"p4\")]\n    listP5=[k for k in list_columns if k.startswith(\"p5\")]\n\n    teamA=[listP0,listP1,listP2]\n    teamB=[listP3,listP4,listP5]\n\n    team_A_new = np.ndarray.tolist((np.random.permutation(teamA)))\n    team_B_new = np.ndarray.tolist((np.random.permutation(teamB)))\n\n    train_temp[listP0]=data[team_A_new[0]]\n    train_temp[listP1]=data[team_A_new[1]]\n    train_temp[listP2]=data[team_A_new[2]]\n    train_temp[listP3]=data[team_B_new[0]]\n    train_temp[listP4]=data[team_B_new[1]]\n    train_temp[listP5]=data[team_B_new[2]]\n    \n    data = pd.concat([data,train_temp])\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2022-10-25T07:27:32.723036Z","iopub.execute_input":"2022-10-25T07:27:32.723498Z","iopub.status.idle":"2022-10-25T07:27:32.743490Z","shell.execute_reply.started":"2022-10-25T07:27:32.723452Z","shell.execute_reply":"2022-10-25T07:27:32.742483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = mirrow_dataset(df)\n#df = shuffling_players(df)","metadata":{"execution":{"iopub.status.busy":"2022-10-25T07:27:32.745276Z","iopub.execute_input":"2022-10-25T07:27:32.746830Z","iopub.status.idle":"2022-10-25T07:27:44.640665Z","shell.execute_reply.started":"2022-10-25T07:27:32.746775Z","shell.execute_reply":"2022-10-25T07:27:44.639172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare to split","metadata":{"papermill":{"duration":0.005849,"end_time":"2022-10-10T19:23:03.602390","exception":false,"start_time":"2022-10-10T19:23:03.596541","status":"completed"},"tags":[]}},{"cell_type":"code","source":"trn_drop_cols = [\"game_num\",\"event_id\",\"event_time\",\"player_scoring_next\",\"team_scoring_next\"]\ndf.drop(columns=trn_drop_cols,inplace = True)","metadata":{"papermill":{"duration":13.602538,"end_time":"2022-10-10T19:23:17.211016","exception":false,"start_time":"2022-10-10T19:23:03.608478","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-25T07:27:44.644115Z","iopub.execute_input":"2022-10-25T07:27:44.644483Z","iopub.status.idle":"2022-10-25T07:27:49.657645Z","shell.execute_reply.started":"2022-10-25T07:27:44.644447Z","shell.execute_reply":"2022-10-25T07:27:49.656184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yA = df[\"team_A_scoring_within_10sec\"].astype('int8')\nyB = df[\"team_B_scoring_within_10sec\"].astype('int8')\nX = df.copy()\n\ndel X[\"team_A_scoring_within_10sec\"], X[\"team_B_scoring_within_10sec\"], \ndel df\ngc.collect()","metadata":{"papermill":{"duration":3.323702,"end_time":"2022-10-10T19:23:20.541139","exception":false,"start_time":"2022-10-10T19:23:17.217437","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-25T07:27:49.659323Z","iopub.execute_input":"2022-10-25T07:27:49.659799Z","iopub.status.idle":"2022-10-25T07:27:51.294887Z","shell.execute_reply.started":"2022-10-25T07:27:49.659752Z","shell.execute_reply":"2022-10-25T07:27:51.293797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {\n    'objective': 'binary',\n    'seed': 42,\n    'num_leaves': 128,\n    'n_estimators': EPOCHS,\n    'max_depth': 10,\n    'learning_rate': 0.1,\n    #'feature_fraction': 0.75,\n    'subsample': 0.7,\n    'subsample_freq': 8,\n    'n_jobs': -1,\n    'reg_alpha': 1,\n    'reg_lambda': 2,\n    'min_child_samples': 100,\n}","metadata":{"papermill":{"duration":0.019195,"end_time":"2022-10-10T19:23:20.566979","exception":false,"start_time":"2022-10-10T19:23:20.547784","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-25T07:27:51.296879Z","iopub.execute_input":"2022-10-25T07:27:51.297338Z","iopub.status.idle":"2022-10-25T07:27:51.304117Z","shell.execute_reply.started":"2022-10-25T07:27:51.297294Z","shell.execute_reply":"2022-10-25T07:27:51.302898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cross Val setup","metadata":{"papermill":{"duration":0.005847,"end_time":"2022-10-10T19:23:20.579104","exception":false,"start_time":"2022-10-10T19:23:20.573257","status":"completed"},"tags":[]}},{"cell_type":"code","source":"cv = KFold(n_splits=5,  #shuffle=True , random_state=42\n          )","metadata":{"papermill":{"duration":0.015125,"end_time":"2022-10-10T19:23:20.600360","exception":false,"start_time":"2022-10-10T19:23:20.585235","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-25T07:27:51.305462Z","iopub.execute_input":"2022-10-25T07:27:51.305812Z","iopub.status.idle":"2022-10-25T07:27:51.320194Z","shell.execute_reply.started":"2022-10-25T07:27:51.305782Z","shell.execute_reply":"2022-10-25T07:27:51.318910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model (X, y, refit = None, continuous = None):\n    score = []\n    preds = []\n\n    for fold, (train_idx, val_idx) in enumerate(cv.split(X,y)):\n\n        print(f\"###### Fold {fold} ######\")\n\n        X_train, X_val = X.iloc[train_idx], X.iloc[val_idx]\n        y_train, y_val = y.iloc[train_idx], y.iloc[val_idx]\n\n        #scaling\n        if Scaling:\n            print(\"Scaling\")\n            scaler = StandardScaler()\n            X_train = scaler.fit_transform(X_train)\n            X_val= scaler.transform(X_val)\n\n            test_s = df_test.copy(deep = True)\n            test_s = scaler.transform(test_s)\n        else:\n            test_s = df_test.copy(deep = True)\n            \n        trn_data = lgb.Dataset(X_train,label = y_train)\n        val_data = lgb.Dataset(X_val,label = y_val)\n\n        del X_train,X_val,y_train, y_val\n        gc.collect()\n        \n        if REFIT:\n            print(\"Refit\")\n            if fold ==0:\n                model = lgb.train(params, trn_data, valid_sets=[val_data],\n                                      callbacks= [lgb.early_stopping(EARLY_STOPPING)])\n            else:\n                model.refit(params, trn_data, valid_sets=[val_data],\n                      callbacks= [lgb.early_stopping(EARLY_STOPPING)])\n        \n        if CONTINUOUS:\n            print(\"Continuous\")\n            if fold ==0:\n                model = lgb.train(params, trn_data, valid_sets=[val_data],\n                      callbacks= [lgb.early_stopping(EARLY_STOPPING)])\n            else:\n                model = lgb.train(params, trn_data, valid_sets=[val_data],callbacks= [lgb.early_stopping(EARLY_STOPPING)],\n                                  init_model = model)             \n\n        if not REFIT and not CONTINUOUS:\n            print(\"standard model\")\n            model = lgb.train(params, trn_data, valid_sets=[val_data],\n                              callbacks= [lgb.early_stopping(EARLY_STOPPING)])\n\n        del trn_data, val_data\n        gc.collect()\n        \n        #predict\n        print(\"Predicting\")\n        preds.append(model.predict(test_s)) \n        score.append(model.best_score[\"valid_0\"][\"binary_logloss\"])\n\n        del test_s , model\n        gc.collect()\n\n    print(f\"\\n Mean logloss: {np.mean(score)}\")\n    \n    return score, preds \n\n# model A\nscoreA, A_preds = train_model(X, yA)","metadata":{"execution":{"iopub.status.busy":"2022-10-25T07:27:51.321987Z","iopub.execute_input":"2022-10-25T07:27:51.322420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model B\nscoreB, B_preds = train_model(X, yB)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submit","metadata":{"papermill":{"duration":0.009119,"end_time":"2022-10-10T21:52:48.715477","exception":false,"start_time":"2022-10-10T21:52:48.706358","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sub = pd.read_csv(\"../input/tabular-playground-series-oct-2022/sample_submission.csv\")\n\nsub[\"team_A_scoring_within_10sec\"] = np.mean(A_preds,axis =0)\nsub[\"team_B_scoring_within_10sec\"] = np.mean(B_preds,axis =0)\n\nsub.to_csv(\"submission.csv\",index=False)","metadata":{"papermill":{"duration":2.947088,"end_time":"2022-10-10T21:52:51.670889","exception":false,"start_time":"2022-10-10T21:52:48.723801","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub[[\"team_A_scoring_within_10sec\",\"team_B_scoring_within_10sec\"]].hist(figsize = (20,5))\nplt.show()","metadata":{"papermill":{"duration":0.506092,"end_time":"2022-10-10T21:52:52.185382","exception":false,"start_time":"2022-10-10T21:52:51.679290","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.008206,"end_time":"2022-10-10T21:52:52.202390","exception":false,"start_time":"2022-10-10T21:52:52.194184","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.008109,"end_time":"2022-10-10T21:52:52.218951","exception":false,"start_time":"2022-10-10T21:52:52.210842","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}