{"metadata":{"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":50160,"databundleVersionId":7921029,"sourceType":"competition"},{"sourceId":8171752,"sourceType":"datasetVersion","datasetId":4836407},{"sourceId":8524870,"sourceType":"datasetVersion","datasetId":4740110},{"sourceId":8542800,"sourceType":"datasetVersion","datasetId":4759747},{"sourceId":169280146,"sourceType":"kernelVersion"},{"sourceId":169863671,"sourceType":"kernelVersion"},{"sourceId":171055948,"sourceType":"kernelVersion"},{"sourceId":171159375,"sourceType":"kernelVersion"},{"sourceId":171401503,"sourceType":"kernelVersion"},{"sourceId":178480573,"sourceType":"kernelVersion"},{"sourceId":178580464,"sourceType":"kernelVersion"}],"dockerImageVersionId":30664,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.13"},"papermill":{"default_parameters":{},"duration":96.460801,"end_time":"2024-05-06T18:35:53.207326","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-05-06T18:34:16.746525","version":"2.5.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Dependencies","metadata":{"papermill":{"duration":0.026657,"end_time":"2024-05-06T18:34:20.088812","exception":false,"start_time":"2024-05-06T18:34:20.062155","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import os\nimport gc\nfrom glob import glob\nfrom pathlib import Path\nfrom datetime import datetime\nimport numpy as np\nimport pandas as pd\nimport polars as pl\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport joblib\n# import lightgbm as lgb\nfrom catboost import CatBoostClassifier, Pool\n\nfrom sklearn.model_selection import StratifiedGroupKFold\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.ensemble import VotingClassifier\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import confusion_matrix\n\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":3.287697,"end_time":"2024-05-06T18:34:23.465302","exception":false,"start_time":"2024-05-06T18:34:20.177605","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:07.097370Z","iopub.execute_input":"2024-05-29T06:15:07.097994Z","iopub.status.idle":"2024-05-29T06:15:09.969945Z","shell.execute_reply.started":"2024-05-29T06:15:07.097962Z","shell.execute_reply":"2024-05-29T06:15:09.969092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import fork_9_of_home_credit_baseline_data as data_nb","metadata":{"papermill":{"duration":0.477109,"end_time":"2024-05-06T18:34:24.063577","exception":false,"start_time":"2024-05-06T18:34:23.586468","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:09.971441Z","iopub.execute_input":"2024-05-29T06:15:09.971900Z","iopub.status.idle":"2024-05-29T06:15:10.484839Z","shell.execute_reply.started":"2024-05-29T06:15:09.971872Z","shell.execute_reply":"2024-05-29T06:15:10.483996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    load_model=True\n    load_folder=Path(\"/kaggle/input/cat-base-df-fork-of-home-credit-baseline-tr-ds\")\n    debug=False\n    infer_train=False\n    debug_subsample=0.01\n    credit_b_a_cols_num=0 # >0 - top feats, -1 - all feats, 0 - no df\n    shifted_cols=False\n    drop_cols_endwith=[]\n    drop_cols_exclude = [\n        \"last_last_conts_type_509L\", \n        \"first_last_conts_type_509L\",\n        \"first_first_credacc_cards_status_52L\",\n        \"last_first_credacc_cards_status_52L\",\n        \"cnt_requesttype_4525192L\",\n    ]\n    drop_cols_contains = [\n        \"conts_type_509L\", \n        \"_credacc_cards_status_52L\",\n        \"empls_employer_name_740M\",\n#         \"max_contractsum_5085717L\", ## added\n    ]\n    isnull_threshold=1\n    data_load_folder=Path(\"/kaggle/input/home-credit-diff-data-ds/\")\n    n_splits=5","metadata":{"papermill":{"duration":0.038548,"end_time":"2024-05-06T18:34:24.129314","exception":false,"start_time":"2024-05-06T18:34:24.090766","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.486118Z","iopub.execute_input":"2024-05-29T06:15:10.486575Z","iopub.status.idle":"2024-05-29T06:15:10.492821Z","shell.execute_reply.started":"2024-05-29T06:15:10.486546Z","shell.execute_reply":"2024-05-29T06:15:10.491970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if CFG.infer_train:\n    sample_df = pd.read_csv(data_nb.CFG.root_dir / \"sample_submission.csv\")\n    print(\"sample_df len: \", sample_df.shape[0])\n    if sample_df.shape[0] == 10:\n        CFG.debug = True","metadata":{"papermill":{"duration":0.048084,"end_time":"2024-05-06T18:34:24.204539","exception":false,"start_time":"2024-05-06T18:34:24.156455","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.495272Z","iopub.execute_input":"2024-05-29T06:15:10.495582Z","iopub.status.idle":"2024-05-29T06:15:10.504576Z","shell.execute_reply.started":"2024-05-29T06:15:10.495557Z","shell.execute_reply":"2024-05-29T06:15:10.503876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def is_gpu_enabled():\n    from tensorflow.python.client import device_lib\n    # Return whether GPU is enabled in the running Kaggle kernel\n    search_string = str(device_lib.list_local_devices())\n    return 'GPU' in search_string","metadata":{"papermill":{"duration":0.035801,"end_time":"2024-05-06T18:34:24.381436","exception":false,"start_time":"2024-05-06T18:34:24.345635","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.505555Z","iopub.execute_input":"2024-05-29T06:15:10.505826Z","iopub.status.idle":"2024-05-29T06:15:10.516005Z","shell.execute_reply.started":"2024-05-29T06:15:10.505803Z","shell.execute_reply":"2024-05-29T06:15:10.515243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data collection","metadata":{"papermill":{"duration":0.025835,"end_time":"2024-05-06T18:34:24.496122","exception":false,"start_time":"2024-05-06T18:34:24.470287","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Base feats","metadata":{"papermill":{"duration":0.025983,"end_time":"2024-05-06T18:34:24.548444","exception":false,"start_time":"2024-05-06T18:34:24.522461","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"Import utility script notebook with data collection functions. See notebook for details: [Home Credit: baseline - DATA](https://www.kaggle.com/code/andreynesterov/home-credit-baseline-data)","metadata":{"papermill":{"duration":0.026316,"end_time":"2024-05-06T18:34:24.601384","exception":false,"start_time":"2024-05-06T18:34:24.575068","status":"completed"},"tags":[]}},{"cell_type":"code","source":"base_files = [\n    \"_static_cb_0.parquet\",\n    \"_static_0_*.parquet\",\n    \"_applprev_1_*.parquet\", \n    \"_tax_registry_a_1.parquet\",\n    \"_tax_registry_b_1.parquet\",\n    \"_tax_registry_c_1.parquet\",\n#     \"_credit_bureau_a_1_*.parquet\",\n    \"_credit_bureau_b_1.parquet\",\n    \"_other_1.parquet\",\n    \"_person_1.parquet\",\n    \"_deposit_1.parquet\",\n    \"_debitcard_1.parquet\",\n    \"_credit_bureau_b_2.parquet\",\n#     \"_credit_bureau_a_2_*.parquet\",\n    \"_applprev_2.parquet\",\n    \"_person_2.parquet\",\n]","metadata":{"papermill":{"duration":0.035754,"end_time":"2024-05-06T18:34:24.663806","exception":false,"start_time":"2024-05-06T18:34:24.628052","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.517074Z","iopub.execute_input":"2024-05-29T06:15:10.517325Z","iopub.status.idle":"2024-05-29T06:15:10.528642Z","shell.execute_reply.started":"2024-05-29T06:15:10.517303Z","shell.execute_reply":"2024-05-29T06:15:10.527926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cred_b_a_files = [\n    \"_credit_bureau_a_1_*.parquet\",\n    \"_credit_bureau_a_2_*.parquet\",\n]","metadata":{"papermill":{"duration":0.035019,"end_time":"2024-05-06T18:34:24.788939","exception":false,"start_time":"2024-05-06T18:34:24.753920","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.529699Z","iopub.execute_input":"2024-05-29T06:15:10.529963Z","iopub.status.idle":"2024-05-29T06:15:10.546902Z","shell.execute_reply.started":"2024-05-29T06:15:10.529942Z","shell.execute_reply":"2024-05-29T06:15:10.546034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nif not CFG.load_model:\n#     train_base_df = pd.read_parquet(CFG.data_load_folder / \"train_base_full_df.parquet\")\n    train_base_df = pd.read_parquet(CFG.data_load_folder / \"train_base_df_f9_v13.parquet\")\n    base_cat_cols = list(train_base_df.select_dtypes(\"object\").columns)\n    train_base_df[base_cat_cols] = train_base_df[base_cat_cols].astype(\"category\")\n    base_cat_cols = list(train_base_df.select_dtypes(\"category\").columns)\n    display(train_base_df)","metadata":{"_kg_hide-output":true,"papermill":{"duration":10.726422,"end_time":"2024-05-06T18:34:35.667259","exception":false,"start_time":"2024-05-06T18:34:24.940837","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.548071Z","iopub.execute_input":"2024-05-29T06:15:10.548750Z","iopub.status.idle":"2024-05-29T06:15:10.561482Z","shell.execute_reply.started":"2024-05-29T06:15:10.548707Z","shell.execute_reply":"2024-05-29T06:15:10.560534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    display(len(base_cat_cols))\n    display(base_cat_cols)","metadata":{"_kg_hide-output":true,"papermill":{"duration":0.04979,"end_time":"2024-05-06T18:34:35.753210","exception":false,"start_time":"2024-05-06T18:34:35.703420","status":"completed"},"scrolled":true,"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.562538Z","iopub.execute_input":"2024-05-29T06:15:10.562798Z","iopub.status.idle":"2024-05-29T06:15:10.573834Z","shell.execute_reply.started":"2024-05-29T06:15:10.562767Z","shell.execute_reply":"2024-05-29T06:15:10.572930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    train_base_df = data_nb.reduce_mem_usage(train_base_df, float16_as32=False)","metadata":{"papermill":{"duration":7.076971,"end_time":"2024-05-06T18:34:42.866800","exception":false,"start_time":"2024-05-06T18:34:35.789829","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.577603Z","iopub.execute_input":"2024-05-29T06:15:10.577942Z","iopub.status.idle":"2024-05-29T06:15:10.584479Z","shell.execute_reply.started":"2024-05-29T06:15:10.577919Z","shell.execute_reply":"2024-05-29T06:15:10.583518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    train_base_cols = train_base_df.columns","metadata":{"papermill":{"duration":0.044547,"end_time":"2024-05-06T18:34:42.950156","exception":false,"start_time":"2024-05-06T18:34:42.905609","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.585672Z","iopub.execute_input":"2024-05-29T06:15:10.586033Z","iopub.status.idle":"2024-05-29T06:15:10.596188Z","shell.execute_reply.started":"2024-05-29T06:15:10.586000Z","shell.execute_reply":"2024-05-29T06:15:10.595207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## credit_bureau_a df","metadata":{"papermill":{"duration":0.037421,"end_time":"2024-05-06T18:34:43.605484","exception":false,"start_time":"2024-05-06T18:34:43.568063","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%%time\nif not CFG.load_model:\n#     train_credit_bureau_a_df = pd.read_parquet(CFG.data_load_folder / \"train_cred_bureau_a_df.parquet\").drop(columns=[\"WEEK_NUM\", \"target\"])\n    train_credit_bureau_a_df = pd.read_parquet(\n        CFG.data_load_folder / \"train_credit_bureau_a_df_f9_v13.parquet\"\n    ).drop(columns=[\"WEEK_NUM\", \"target\"])\n    cat_credit_bureau_a_cols = list(train_credit_bureau_a_df.select_dtypes(\"object\").columns)\n    train_credit_bureau_a_df[cat_credit_bureau_a_cols] = train_credit_bureau_a_df[\n        cat_credit_bureau_a_cols\n    ].astype(\"category\")\n    cat_credit_bureau_a_cols = list(train_credit_bureau_a_df.select_dtypes(\"category\").columns)\n    all_credit_bureau_a_cols = train_credit_bureau_a_df.columns\n    display(train_credit_bureau_a_df)","metadata":{"papermill":{"duration":4.963079,"end_time":"2024-05-06T18:34:48.767929","exception":false,"start_time":"2024-05-06T18:34:43.804850","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.597261Z","iopub.execute_input":"2024-05-29T06:15:10.597553Z","iopub.status.idle":"2024-05-29T06:15:10.609266Z","shell.execute_reply.started":"2024-05-29T06:15:10.597516Z","shell.execute_reply":"2024-05-29T06:15:10.608268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    train_credit_bureau_a_df = data_nb.reduce_mem_usage(train_credit_bureau_a_df, float16_as32=False)\n#     train_credit_bureau_a_df.drop(columns=[\"WEEK_NUM\", \"target\"], inplace=True)","metadata":{"papermill":{"duration":3.875419,"end_time":"2024-05-06T18:34:52.684330","exception":false,"start_time":"2024-05-06T18:34:48.808911","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.610396Z","iopub.execute_input":"2024-05-29T06:15:10.610747Z","iopub.status.idle":"2024-05-29T06:15:10.624029Z","shell.execute_reply.started":"2024-05-29T06:15:10.610723Z","shell.execute_reply":"2024-05-29T06:15:10.623304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Merged df","metadata":{"papermill":{"duration":0.039906,"end_time":"2024-05-06T18:34:52.941793","exception":false,"start_time":"2024-05-06T18:34:52.901887","status":"completed"},"tags":[]}},{"cell_type":"code","source":"if not CFG.load_model:\n    train_df = train_base_df.merge(train_credit_bureau_a_df, on=\"case_id\", how=\"left\")\n    display(train_df)","metadata":{"papermill":{"duration":3.702608,"end_time":"2024-05-06T18:34:56.684016","exception":false,"start_time":"2024-05-06T18:34:52.981408","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.625070Z","iopub.execute_input":"2024-05-29T06:15:10.625385Z","iopub.status.idle":"2024-05-29T06:15:10.633263Z","shell.execute_reply.started":"2024-05-29T06:15:10.625363Z","shell.execute_reply":"2024-05-29T06:15:10.632531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if not CFG.load_model:\n#     train_df = train_df.sample(frac=1, axis=1, random_state=42)","metadata":{"papermill":{"duration":0.059924,"end_time":"2024-05-06T18:34:56.796926","exception":false,"start_time":"2024-05-06T18:34:56.737002","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.634228Z","iopub.execute_input":"2024-05-29T06:15:10.634469Z","iopub.status.idle":"2024-05-29T06:15:10.643737Z","shell.execute_reply.started":"2024-05-29T06:15:10.634432Z","shell.execute_reply":"2024-05-29T06:15:10.642847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    del train_base_df, train_credit_bureau_a_df\ngc.collect()","metadata":{"papermill":{"duration":0.159723,"end_time":"2024-05-06T18:34:57.123785","exception":false,"start_time":"2024-05-06T18:34:56.964062","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.644872Z","iopub.execute_input":"2024-05-29T06:15:10.645387Z","iopub.status.idle":"2024-05-29T06:15:10.730606Z","shell.execute_reply.started":"2024-05-29T06:15:10.645349Z","shell.execute_reply":"2024-05-29T06:15:10.729587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Drop columns","metadata":{"papermill":{"duration":0.053328,"end_time":"2024-05-06T18:34:57.344132","exception":false,"start_time":"2024-05-06T18:34:57.290804","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def get_drop_cols(df, drop_cols_endwith=[], drop_cols_contains=[], drop_cols_exclude=[], isnull_threshold=1):\n    drop_cols = []\n    for name_prefix in drop_cols_endwith:\n        cols_names = df.columns[df.columns.str.endswith(name_prefix)]\n        drop_cols += cols_names.to_list()\n    for name_prefix in drop_cols_contains:\n        cols_names = df.columns[df.columns.str.contains(name_prefix)]\n        drop_cols += cols_names.to_list()\n    for col_name in drop_cols_exclude:\n        if col_name in drop_cols:\n            drop_cols.remove(col_name)\n        \n    if isnull_threshold < 1:\n        for col in df.columns:\n            if col not in [\"target\", \"case_id\", \"WEEK_NUM\"]:\n                isnull = df[col].isnull().mean()\n                if isnull > isnull_threshold:\n                    drop_cols.append(col)\n    return drop_cols","metadata":{"papermill":{"duration":0.065211,"end_time":"2024-05-06T18:34:57.462542","exception":false,"start_time":"2024-05-06T18:34:57.397331","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.731897Z","iopub.execute_input":"2024-05-29T06:15:10.732259Z","iopub.status.idle":"2024-05-29T06:15:10.744361Z","shell.execute_reply.started":"2024-05-29T06:15:10.732227Z","shell.execute_reply":"2024-05-29T06:15:10.743460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    drop_cols = get_drop_cols(\n        train_df, CFG.drop_cols_endwith, CFG.drop_cols_contains, CFG.drop_cols_exclude, CFG.isnull_threshold\n    )\nelse:\n    drop_cols = []\ndisplay(len(drop_cols))\ndisplay(drop_cols)","metadata":{"_kg_hide-output":true,"papermill":{"duration":0.068966,"end_time":"2024-05-06T18:34:57.586041","exception":false,"start_time":"2024-05-06T18:34:57.517075","status":"completed"},"scrolled":true,"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.745761Z","iopub.execute_input":"2024-05-29T06:15:10.746343Z","iopub.status.idle":"2024-05-29T06:15:10.766640Z","shell.execute_reply.started":"2024-05-29T06:15:10.746310Z","shell.execute_reply":"2024-05-29T06:15:10.765742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### from https://www.kaggle.com/code/batprem/home-credit-risk-mode-utility-scripts\n\ndef gini_stability(base, score_col=\"score\", w_fallingrate=88.0, w_resstd=-0.5):\n    gini_in_time = base.loc[:, [\"WEEK_NUM\", \"target\", score_col]]\\\n        .sort_values(\"WEEK_NUM\")\\\n        .groupby(\"WEEK_NUM\")[[\"target\", score_col]]\\\n        .apply(lambda x: 2*roc_auc_score(x[\"target\"], x[score_col])-1).tolist()\n    \n    x = np.arange(len(gini_in_time))\n    y = gini_in_time\n    a, b = np.polyfit(x, y, 1)\n    y_hat = a*x + b\n    residuals = y - y_hat\n    res_std = np.std(residuals)\n    avg_gini = np.mean(gini_in_time)\n    return avg_gini + w_fallingrate * min(0, a) + w_resstd * res_std","metadata":{"papermill":{"duration":0.064477,"end_time":"2024-05-06T18:34:57.821498","exception":false,"start_time":"2024-05-06T18:34:57.757021","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.767771Z","iopub.execute_input":"2024-05-29T06:15:10.768043Z","iopub.status.idle":"2024-05-29T06:15:10.777059Z","shell.execute_reply.started":"2024-05-29T06:15:10.768019Z","shell.execute_reply":"2024-05-29T06:15:10.776268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if CFG.debug and not CFG.load_model:\n    train_df = train_df[: int(CFG.debug_subsample * train_df.shape[0])]\n    print(train_df.shape)","metadata":{"papermill":{"duration":0.064365,"end_time":"2024-05-06T18:34:57.938940","exception":false,"start_time":"2024-05-06T18:34:57.874575","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.778221Z","iopub.execute_input":"2024-05-29T06:15:10.778471Z","iopub.status.idle":"2024-05-29T06:15:10.787738Z","shell.execute_reply.started":"2024-05-29T06:15:10.778444Z","shell.execute_reply":"2024-05-29T06:15:10.786937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    X = train_df.drop(columns=[\"target\", \"case_id\", \"WEEK_NUM\"] + drop_cols)\n    print(\"X shape: \", X.shape)\n    y = train_df[\"target\"]\n    weeks = train_df[\"WEEK_NUM\"]\n    cat_cols = list(X.select_dtypes(\"category\").columns)\n    X[cat_cols] = X[cat_cols].astype(str)","metadata":{"_kg_hide-output":false,"papermill":{"duration":0.191104,"end_time":"2024-05-06T18:34:58.184184","exception":false,"start_time":"2024-05-06T18:34:57.993080","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.788804Z","iopub.execute_input":"2024-05-29T06:15:10.789041Z","iopub.status.idle":"2024-05-29T06:15:10.798533Z","shell.execute_reply.started":"2024-05-29T06:15:10.789020Z","shell.execute_reply":"2024-05-29T06:15:10.797731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    oof_df = train_df[[\"WEEK_NUM\", \"target\"]].copy()\n    train_cols = train_df.columns\n    del train_df\n    gc.collect()","metadata":{"papermill":{"duration":0.159119,"end_time":"2024-05-06T18:34:58.396588","exception":false,"start_time":"2024-05-06T18:34:58.237469","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.799689Z","iopub.execute_input":"2024-05-29T06:15:10.800022Z","iopub.status.idle":"2024-05-29T06:15:10.814813Z","shell.execute_reply.started":"2024-05-29T06:15:10.799992Z","shell.execute_reply":"2024-05-29T06:15:10.813924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    device = 'gpu' if is_gpu_enabled() else 'cpu'\n    print(\"device:\", device)","metadata":{"papermill":{"duration":13.389001,"end_time":"2024-05-06T18:35:11.953552","exception":false,"start_time":"2024-05-06T18:34:58.564551","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.815927Z","iopub.execute_input":"2024-05-29T06:15:10.816212Z","iopub.status.idle":"2024-05-29T06:15:10.825615Z","shell.execute_reply.started":"2024-05-29T06:15:10.816188Z","shell.execute_reply":"2024-05-29T06:15:10.824825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{"papermill":{"duration":0.053771,"end_time":"2024-05-06T18:35:12.061109","exception":false,"start_time":"2024-05-06T18:35:12.007338","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%%time\nif not CFG.load_model:\n    params = {\n        'iterations': 10 if CFG.debug else 6000,\n#         'iterations': 10,\n        'loss_function': 'Logloss',\n        'eval_metric': 'AUC',\n        'task_type': 'GPU' if is_gpu_enabled() else 'CPU',\n        'learning_rate': 0.15,\n#         'learning_rate': 0.03,\n        'random_state': 42,\n        'devices': '0:1',\n        'reg_lambda': 10,\n#         'max_ctr_complexity': 3,\n#         'boosting_type': 'Ordered',\n#         'max_depth': 7,\n#         'reg_lambda': 10,\n#         'colsample_bylevel': 0.3,\n    }\n    \n    fitted_models = []\n    oof_pred = np.zeros(X.shape[0])\n    cv = StratifiedGroupKFold(n_splits=CFG.n_splits, shuffle=False)\n    \n    for i, (idx_train, idx_valid) in enumerate(cv.split(X, y, groups=weeks)):\n        print(\"Fold: \", i)\n        X_train, y_train = X.iloc[idx_train], y.iloc[idx_train]\n        X_valid, y_valid = X.iloc[idx_valid], y.iloc[idx_valid]\n        \n        train_pool = Pool(X_train, y_train, cat_features=cat_cols)\n        val_pool = Pool(X_valid, y_valid, cat_features=cat_cols)\n        \n#         params['random_state'] += i\n        model = CatBoostClassifier(**params)\n        model.fit(\n            train_pool, \n            eval_set=val_pool,\n            verbose=200,\n            early_stopping_rounds=100,\n            use_best_model=True,\n        )\n        \n        fitted_models.append(model)\n        val_pred = model.predict_proba(X_valid)[:, 1]\n        oof_pred[idx_valid] = val_pred\n        del X_train, X_valid\n        gc.collect()\n#         break\n        \n    oof_models_dict = [(str(i), model) for i, model in enumerate(fitted_models)]\n    model = VotingClassifier(\n        estimators=oof_models_dict,\n        voting='soft',\n    )\n    model.estimators_ = fitted_models\n    model.le_ = LabelEncoder().fit(y)\n    model.classes_ = model.le_.classes_","metadata":{"_kg_hide-output":true,"papermill":{"duration":25.497545,"end_time":"2024-05-06T18:35:37.612864","exception":false,"start_time":"2024-05-06T18:35:12.115319","status":"completed"},"scrolled":true,"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.826793Z","iopub.execute_input":"2024-05-29T06:15:10.827180Z","iopub.status.idle":"2024-05-29T06:15:10.842682Z","shell.execute_reply.started":"2024-05-29T06:15:10.827149Z","shell.execute_reply":"2024-05-29T06:15:10.841803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if CFG.load_model:\n    train_df = pd.read_parquet(\"/kaggle/input/home-credit-credit-risk-model-stability/parquet_files/train/train_base.parquet\")\n    y = train_df[\"target\"]\n    oof_df = train_df[[\"WEEK_NUM\", \"target\"]].copy()\n    model = joblib.load(CFG.load_folder / \"oof_model.pkl\")\n    oof_pred = joblib.load(CFG.load_folder / \"oof_pred.pkl\")\n    base_cat_cols, train_base_cols, cat_credit_bureau_a_cols, all_credit_bureau_a_cols = joblib.load(\n#     base_cat_cols, train_base_cols = joblib.load(\n        CFG.load_folder / \"train_base_columns.pkl\"\n    )","metadata":{"papermill":{"duration":0.065816,"end_time":"2024-05-06T18:35:37.854408","exception":false,"start_time":"2024-05-06T18:35:37.788592","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:10.843844Z","iopub.execute_input":"2024-05-29T06:15:10.844189Z","iopub.status.idle":"2024-05-29T06:15:15.464819Z","shell.execute_reply.started":"2024-05-29T06:15:10.844156Z","shell.execute_reply":"2024-05-29T06:15:15.463786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roc_auc_oof = roc_auc_score(y, oof_pred)\nprint(\"CV roc_auc_oof: \", roc_auc_oof)","metadata":{"papermill":{"duration":0.077087,"end_time":"2024-05-06T18:35:38.228316","exception":false,"start_time":"2024-05-06T18:35:38.151229","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:15.466341Z","iopub.execute_input":"2024-05-29T06:15:15.466930Z","iopub.status.idle":"2024-05-29T06:15:16.244299Z","shell.execute_reply.started":"2024-05-29T06:15:15.466893Z","shell.execute_reply":"2024-05-29T06:15:16.243346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.debug:\n#     oof_df = train_df[[\"WEEK_NUM\", \"target\"]].copy()\n    oof_df[\"pred_oof\"] = oof_pred\n    gini_score = gini_stability(oof_df, score_col=\"pred_oof\")\n    print(\"gini_score:\\t\", gini_score)","metadata":{"papermill":{"duration":0.064781,"end_time":"2024-05-06T18:35:38.352027","exception":false,"start_time":"2024-05-06T18:35:38.287246","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:16.245468Z","iopub.execute_input":"2024-05-29T06:15:16.245781Z","iopub.status.idle":"2024-05-29T06:15:17.161984Z","shell.execute_reply.started":"2024-05-29T06:15:16.245754Z","shell.execute_reply":"2024-05-29T06:15:17.160752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_feature_importance(models, feature_names):\n    feature_imp_all = []\n    for model in models:\n        feature_imp_df = pd.DataFrame({'Value': model.feature_importances_}, index=feature_names)\n        feature_imp_all.append(feature_imp_df)\n    feature_imp_all_df = pd.concat(feature_imp_all)\n    feature_imp_all_df = feature_imp_all_df.groupby(feature_imp_all_df.index).mean()\n    return feature_imp_all_df","metadata":{"papermill":{"duration":0.065288,"end_time":"2024-05-06T18:35:38.473118","exception":false,"start_time":"2024-05-06T18:35:38.407830","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:17.169813Z","iopub.execute_input":"2024-05-29T06:15:17.170195Z","iopub.status.idle":"2024-05-29T06:15:17.177331Z","shell.execute_reply.started":"2024-05-29T06:15:17.170165Z","shell.execute_reply":"2024-05-29T06:15:17.175818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    feature_imp_df = get_feature_importance(model.estimators_, model.estimators_[0].feature_names_)\n    with pd.option_context('display.max_rows', None, 'display.max_columns', None): \n        feature_imp_sorted_df = feature_imp_df.sort_values(\"Value\", ascending=False)\n        display(feature_imp_sorted_df)","metadata":{"_kg_hide-output":true,"papermill":{"duration":0.13198,"end_time":"2024-05-06T18:35:38.660871","exception":false,"start_time":"2024-05-06T18:35:38.528891","status":"completed"},"scrolled":true,"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:17.178660Z","iopub.execute_input":"2024-05-29T06:15:17.178994Z","iopub.status.idle":"2024-05-29T06:15:17.199290Z","shell.execute_reply.started":"2024-05-29T06:15:17.178963Z","shell.execute_reply":"2024-05-29T06:15:17.198324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    joblib.dump(feature_imp_sorted_df, \"feature_imp_df.pkl\")","metadata":{"papermill":{"duration":0.070715,"end_time":"2024-05-06T18:35:38.791271","exception":false,"start_time":"2024-05-06T18:35:38.720556","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:17.200723Z","iopub.execute_input":"2024-05-29T06:15:17.201077Z","iopub.status.idle":"2024-05-29T06:15:17.209709Z","shell.execute_reply.started":"2024-05-29T06:15:17.201045Z","shell.execute_reply":"2024-05-29T06:15:17.208837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    joblib.dump(model, \"oof_model.pkl\")","metadata":{"papermill":{"duration":0.218953,"end_time":"2024-05-06T18:35:39.069863","exception":false,"start_time":"2024-05-06T18:35:38.850910","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:17.211311Z","iopub.execute_input":"2024-05-29T06:15:17.211677Z","iopub.status.idle":"2024-05-29T06:15:17.218997Z","shell.execute_reply.started":"2024-05-29T06:15:17.211645Z","shell.execute_reply":"2024-05-29T06:15:17.218007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    joblib.dump((train_cols, cat_cols, drop_cols), \"train_cat_columns.pkl\")","metadata":{"papermill":{"duration":0.071283,"end_time":"2024-05-06T18:35:39.201997","exception":false,"start_time":"2024-05-06T18:35:39.130714","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:17.220234Z","iopub.execute_input":"2024-05-29T06:15:17.220609Z","iopub.status.idle":"2024-05-29T06:15:17.228074Z","shell.execute_reply.started":"2024-05-29T06:15:17.220577Z","shell.execute_reply":"2024-05-29T06:15:17.227063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not CFG.load_model:\n    joblib.dump(oof_pred, \"oof_pred.pkl\")","metadata":{"papermill":{"duration":0.070326,"end_time":"2024-05-06T18:35:39.332755","exception":false,"start_time":"2024-05-06T18:35:39.262429","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:17.229451Z","iopub.execute_input":"2024-05-29T06:15:17.229798Z","iopub.status.idle":"2024-05-29T06:15:17.235777Z","shell.execute_reply.started":"2024-05-29T06:15:17.229767Z","shell.execute_reply":"2024-05-29T06:15:17.234715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if not CFG.debug:\n#     del oof_df\nif not CFG.load_model:\n    del X, y, weeks, oof_pred, oof_df, feature_imp_df, feature_imp_sorted_df\n    gc.collect()","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"papermill":{"duration":0.314761,"end_time":"2024-05-06T18:35:39.708193","exception":false,"start_time":"2024-05-06T18:35:39.393432","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:17.237209Z","iopub.execute_input":"2024-05-29T06:15:17.237562Z","iopub.status.idle":"2024-05-29T06:15:17.246467Z","shell.execute_reply.started":"2024-05-29T06:15:17.237529Z","shell.execute_reply":"2024-05-29T06:15:17.245353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Data Collection","metadata":{"papermill":{"duration":0.060038,"end_time":"2024-05-06T18:35:39.829230","exception":false,"start_time":"2024-05-06T18:35:39.769192","status":"completed"},"tags":[]}},{"cell_type":"code","source":"if not CFG.load_model:\n    joblib.dump(\n        (base_cat_cols, train_base_cols, cat_credit_bureau_a_cols, all_credit_bureau_a_cols), \n#         (base_cat_cols, train_base_cols), \n        \"train_base_columns.pkl\"\n    )","metadata":{"papermill":{"duration":0.071611,"end_time":"2024-05-06T18:35:39.961592","exception":false,"start_time":"2024-05-06T18:35:39.889981","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:17.248141Z","iopub.execute_input":"2024-05-29T06:15:17.248566Z","iopub.status.idle":"2024-05-29T06:15:17.261556Z","shell.execute_reply.started":"2024-05-29T06:15:17.248533Z","shell.execute_reply":"2024-05-29T06:15:17.260672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if 'WEEK_NUM' not in all_credit_bureau_a_cols:\n    all_credit_bureau_a_cols = list(all_credit_bureau_a_cols)\n    all_credit_bureau_a_cols.append('WEEK_NUM')","metadata":{"papermill":{"duration":0.068441,"end_time":"2024-05-06T18:35:40.090351","exception":false,"start_time":"2024-05-06T18:35:40.021910","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:17.262892Z","iopub.execute_input":"2024-05-29T06:15:17.263179Z","iopub.status.idle":"2024-05-29T06:15:17.270715Z","shell.execute_reply.started":"2024-05-29T06:15:17.263156Z","shell.execute_reply":"2024-05-29T06:15:17.269899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_credit_bureau_a_df = data_nb.prepare_df(\n    cred_b_a_files,\n    data_nb.CFG.test_dir,\n    data_nb.base_agg,\n    agg_chunks=True,\n    cat_encode=True,\n    feat_eng=True,\n    mode=\"test\",\n    cat_cols=cat_credit_bureau_a_cols, \n    train_cols=all_credit_bureau_a_cols,\n\n    isnull_threshold=data_nb.CFG.isnull_threshold, \n    freq_threshold=data_nb.CFG.freq_threshold,\n    freq_threshold_upper=data_nb.CFG.freq_threshold_upper,\n    cat_values_threshold=data_nb.CFG.cat_values_threshold,\n    correlation_threshold=data_nb.CFG.correlation_threshold,\n)\nif 'WEEK_NUM' in test_credit_bureau_a_df.columns:\n    test_credit_bureau_a_df.drop(columns=['WEEK_NUM'], inplace=True)\ndisplay(test_credit_bureau_a_df.shape)","metadata":{"papermill":{"duration":0.981457,"end_time":"2024-05-06T18:35:41.131987","exception":false,"start_time":"2024-05-06T18:35:40.150530","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:17.271714Z","iopub.execute_input":"2024-05-29T06:15:17.271961Z","iopub.status.idle":"2024-05-29T06:15:18.111441Z","shell.execute_reply.started":"2024-05-29T06:15:17.271939Z","shell.execute_reply":"2024-05-29T06:15:18.110533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_credit_bureau_a_df = data_nb.reduce_mem_usage(test_credit_bureau_a_df, float16_as32=False)","metadata":{"papermill":{"duration":0.147281,"end_time":"2024-05-06T18:35:41.340512","exception":false,"start_time":"2024-05-06T18:35:41.193231","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:18.112904Z","iopub.execute_input":"2024-05-29T06:15:18.113554Z","iopub.status.idle":"2024-05-29T06:15:18.196081Z","shell.execute_reply.started":"2024-05-29T06:15:18.113515Z","shell.execute_reply":"2024-05-29T06:15:18.195043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = data_nb.prepare_df(\n#     data_nb.base_files,\n    base_files,\n    data_nb.CFG.test_dir,\n    data_nb.base_agg,\n    mode=\"test\", \n    cat_cols=base_cat_cols, \n    train_cols=train_base_cols,\n    agg_chunks=True,\n    cat_encode=True,\n    \n    isnull_threshold=data_nb.CFG.isnull_threshold, \n    freq_threshold=data_nb.CFG.freq_threshold,\n    freq_threshold_upper=data_nb.CFG.freq_threshold_upper,\n    cat_values_threshold=data_nb.CFG.cat_values_threshold,\n    correlation_threshold=data_nb.CFG.correlation_threshold,\n)\ndisplay(test_df.shape)","metadata":{"papermill":{"duration":3.903904,"end_time":"2024-05-06T18:35:45.593368","exception":false,"start_time":"2024-05-06T18:35:41.689464","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:18.197319Z","iopub.execute_input":"2024-05-29T06:15:18.197635Z","iopub.status.idle":"2024-05-29T06:15:19.574901Z","shell.execute_reply.started":"2024-05-29T06:15:18.197609Z","shell.execute_reply":"2024-05-29T06:15:19.573954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = data_nb.reduce_mem_usage(test_df, float16_as32=False)","metadata":{"papermill":{"duration":0.255198,"end_time":"2024-05-06T18:35:45.913221","exception":false,"start_time":"2024-05-06T18:35:45.658023","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:19.576035Z","iopub.execute_input":"2024-05-29T06:15:19.576308Z","iopub.status.idle":"2024-05-29T06:15:19.731921Z","shell.execute_reply.started":"2024-05-29T06:15:19.576284Z","shell.execute_reply":"2024-05-29T06:15:19.730947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_cat_cnt_cols = test_df[test_df.columns[test_df.columns.str.contains(\"cnt_\")]].select_dtypes(include=['object', 'category']).columns\ndisplay(test_cat_cnt_cols)","metadata":{"_kg_hide-output":false,"papermill":{"duration":0.080178,"end_time":"2024-05-06T18:35:46.992775","exception":false,"start_time":"2024-05-06T18:35:46.912597","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:19.733077Z","iopub.execute_input":"2024-05-29T06:15:19.733349Z","iopub.status.idle":"2024-05-29T06:15:19.744564Z","shell.execute_reply.started":"2024-05-29T06:15:19.733324Z","shell.execute_reply":"2024-05-29T06:15:19.743582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if len(test_cat_cnt_cols) > 0:\n    test_df[test_cat_cnt_cols] = test_df[test_cat_cnt_cols].astype(np.float64)","metadata":{"papermill":{"duration":0.074579,"end_time":"2024-05-06T18:35:47.131009","exception":false,"start_time":"2024-05-06T18:35:47.056430","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:19.745984Z","iopub.execute_input":"2024-05-29T06:15:19.746298Z","iopub.status.idle":"2024-05-29T06:15:19.758022Z","shell.execute_reply.started":"2024-05-29T06:15:19.746274Z","shell.execute_reply":"2024-05-29T06:15:19.757153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = test_df.merge(test_credit_bureau_a_df, on=\"case_id\", how=\"left\") \ndisplay(test_df.shape)","metadata":{"papermill":{"duration":0.101118,"end_time":"2024-05-06T18:35:47.296802","exception":false,"start_time":"2024-05-06T18:35:47.195684","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:19.759131Z","iopub.execute_input":"2024-05-29T06:15:19.759407Z","iopub.status.idle":"2024-05-29T06:15:19.796313Z","shell.execute_reply.started":"2024-05-29T06:15:19.759383Z","shell.execute_reply":"2024-05-29T06:15:19.795373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_credit_bureau_a_df\ngc.collect()","metadata":{"papermill":{"duration":0.308434,"end_time":"2024-05-06T18:35:47.805242","exception":false,"start_time":"2024-05-06T18:35:47.496808","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:19.797452Z","iopub.execute_input":"2024-05-29T06:15:19.797788Z","iopub.status.idle":"2024-05-29T06:15:19.883692Z","shell.execute_reply.started":"2024-05-29T06:15:19.797762Z","shell.execute_reply":"2024-05-29T06:15:19.882716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{"papermill":{"duration":0.063735,"end_time":"2024-05-06T18:35:48.480644","exception":false,"start_time":"2024-05-06T18:35:48.416909","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# def iso_calib(y_pred, y_true, weeks, test_pred):\n#     cv = StratifiedGroupKFold(n_splits=CFG.n_splits, shuffle=False)\n    \n#     for i, (idx_train, idx_valid) in enumerate(cv.split(y_pred, y_true, groups=weeks)):\n#         print(\"Fold: \", i)\n#         X_train, y_train = y_pred.iloc[idx_train], y.iloc[idx_train]\n#         X_valid, y_valid = X.iloc[idx_valid], y.iloc[idx_valid]\n        \n#         isotonic_reg = IsotonicRegression(out_of_bounds='clip')\n#         isotonic_reg.fit(y_pred, y_val)\n#         isotonic_pred_calib = isotonic_reg.predict(test_pred)\n    \n#     return isotonic_pred_calib","metadata":{"execution":{"iopub.status.busy":"2024-05-29T06:15:19.884925Z","iopub.execute_input":"2024-05-29T06:15:19.885276Z","iopub.status.idle":"2024-05-29T06:15:19.893346Z","shell.execute_reply.started":"2024-05-29T06:15:19.885248Z","shell.execute_reply":"2024-05-29T06:15:19.892384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_by_names(models, X):\n    all_pred = np.zeros((X.shape[0]))\n    for model in models:\n        columns = model.feature_names_\n        pred = model.predict_proba(X[columns])[:, 1]\n        all_pred += pred / len(models)\n    return all_pred","metadata":{"papermill":{"duration":0.074971,"end_time":"2024-05-06T18:35:48.620325","exception":false,"start_time":"2024-05-06T18:35:48.545354","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:19.894674Z","iopub.execute_input":"2024-05-29T06:15:19.895034Z","iopub.status.idle":"2024-05-29T06:15:19.905585Z","shell.execute_reply.started":"2024-05-29T06:15:19.895001Z","shell.execute_reply":"2024-05-29T06:15:19.904562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_proba_in_batches(model, data, batch_size=20000):\n    num_samples = len(data)\n    num_batches = int(np.ceil(num_samples / batch_size))\n    probabilities = np.zeros((num_samples,))\n\n    for batch_idx in range(num_batches):\n        print(f\"Processing batch: {batch_idx+1}/{num_batches}\")\n        start_idx = batch_idx * batch_size\n        end_idx = min((batch_idx + 1) * batch_size, num_samples)\n        X_batch = data.iloc[start_idx:end_idx]\n        batch_probs = predict_by_names(model.estimators_, X_batch)\n        probabilities[start_idx:end_idx] = batch_probs\n        gc.collect()\n\n    return probabilities","metadata":{"papermill":{"duration":0.077127,"end_time":"2024-05-06T18:35:48.763708","exception":false,"start_time":"2024-05-06T18:35:48.686581","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:19.906655Z","iopub.execute_input":"2024-05-29T06:15:19.906992Z","iopub.status.idle":"2024-05-29T06:15:19.915767Z","shell.execute_reply.started":"2024-05-29T06:15:19.906966Z","shell.execute_reply":"2024-05-29T06:15:19.915028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = test_df.drop(columns=[\"WEEK_NUM\"] + drop_cols)\ntest_df = test_df.set_index(\"case_id\")\ncat_cols = list(test_df.select_dtypes(\"category\").columns)\ntest_df[cat_cols] = test_df[cat_cols].astype(str)\nprint(\"X_test shape: \", test_df.shape)","metadata":{"papermill":{"duration":0.118134,"end_time":"2024-05-06T18:35:48.946779","exception":false,"start_time":"2024-05-06T18:35:48.828645","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:15:19.916835Z","iopub.execute_input":"2024-05-29T06:15:19.917099Z","iopub.status.idle":"2024-05-29T06:15:19.964881Z","shell.execute_reply.started":"2024-05-29T06:15:19.917077Z","shell.execute_reply":"2024-05-29T06:15:19.963908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = pd.Series(predict_proba_in_batches(model, test_df), index=test_df.index)\ndisplay(y_pred[:10])","metadata":{"papermill":{"duration":0.549093,"end_time":"2024-05-06T18:35:49.560776","exception":false,"start_time":"2024-05-06T18:35:49.011683","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Isotonic Calibration","metadata":{}},{"cell_type":"code","source":"from sklearn.calibration import calibration_curve\nfrom matplotlib import pyplot\n\n# reliability diagram\nfop, mpv = calibration_curve(y, oof_pred, n_bins=10, normalize=True)\n# plot perfectly calibrated\npyplot.plot([0, 1], [0, 1], linestyle='--')\n# plot model reliability\npyplot.plot(mpv, fop, marker='.')\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-29T06:15:54.405407Z","iopub.execute_input":"2024-05-29T06:15:54.405853Z","iopub.status.idle":"2024-05-29T06:15:54.838277Z","shell.execute_reply.started":"2024-05-29T06:15:54.405823Z","shell.execute_reply":"2024-05-29T06:15:54.837311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.isotonic import IsotonicRegression\n\ndef iso_calib(y_pred, y_true, test_pred):\n    isotonic_reg = IsotonicRegression(out_of_bounds='clip')\n    isotonic_reg.fit(y_pred, y_true)\n    isotonic_pred_calib = isotonic_reg.predict(test_pred)\n    \n    return isotonic_pred_calib","metadata":{"execution":{"iopub.status.busy":"2024-05-29T06:16:07.964087Z","iopub.execute_input":"2024-05-29T06:16:07.964448Z","iopub.status.idle":"2024-05-29T06:16:07.969930Z","shell.execute_reply.started":"2024-05-29T06:16:07.964418Z","shell.execute_reply":"2024-05-29T06:16:07.969004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = iso_calib(oof_pred, y, y_pred)\ndisplay(y_pred[:10])","metadata":{"execution":{"iopub.status.busy":"2024-05-29T06:16:11.402795Z","iopub.execute_input":"2024-05-29T06:16:11.403446Z","iopub.status.idle":"2024-05-29T06:16:11.767741Z","shell.execute_reply.started":"2024-05-29T06:16:11.403416Z","shell.execute_reply":"2024-05-29T06:16:11.766801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_df\ngc.collect()","metadata":{"papermill":{"duration":0.309303,"end_time":"2024-05-06T18:35:49.935321","exception":false,"start_time":"2024-05-06T18:35:49.626018","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:16:25.342673Z","iopub.execute_input":"2024-05-29T06:16:25.343460Z","iopub.status.idle":"2024-05-29T06:16:25.423084Z","shell.execute_reply.started":"2024-05-29T06:16:25.343430Z","shell.execute_reply":"2024-05-29T06:16:25.422059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{"papermill":{"duration":0.064501,"end_time":"2024-05-06T18:35:50.064979","exception":false,"start_time":"2024-05-06T18:35:50.000478","status":"completed"},"tags":[]}},{"cell_type":"code","source":"subm_df = pd.read_csv(data_nb.CFG.root_dir / \"sample_submission.csv\")\nsubm_df = subm_df.set_index(\"case_id\")\n\nsubm_df[\"score\"] = y_pred","metadata":{"papermill":{"duration":0.079554,"end_time":"2024-05-06T18:35:50.210067","exception":false,"start_time":"2024-05-06T18:35:50.130513","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:16:27.805209Z","iopub.execute_input":"2024-05-29T06:16:27.805569Z","iopub.status.idle":"2024-05-29T06:16:27.825084Z","shell.execute_reply.started":"2024-05-29T06:16:27.805537Z","shell.execute_reply":"2024-05-29T06:16:27.824068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Check null: \", subm_df[\"score\"].isnull().any())\n\ndisplay(subm_df.head())","metadata":{"papermill":{"duration":0.081386,"end_time":"2024-05-06T18:35:50.356205","exception":false,"start_time":"2024-05-06T18:35:50.274819","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:16:29.517831Z","iopub.execute_input":"2024-05-29T06:16:29.518713Z","iopub.status.idle":"2024-05-29T06:16:29.535414Z","shell.execute_reply.started":"2024-05-29T06:16:29.518673Z","shell.execute_reply":"2024-05-29T06:16:29.534402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if CFG.load_model:\nsubm_df.to_csv(\"submission.csv\")","metadata":{"papermill":{"duration":0.078045,"end_time":"2024-05-06T18:35:50.500069","exception":false,"start_time":"2024-05-06T18:35:50.422024","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-29T06:16:31.805371Z","iopub.execute_input":"2024-05-29T06:16:31.805764Z","iopub.status.idle":"2024-05-29T06:16:31.813272Z","shell.execute_reply.started":"2024-05-29T06:16:31.805732Z","shell.execute_reply":"2024-05-29T06:16:31.812406Z"},"trusted":true},"execution_count":null,"outputs":[]}]}