{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":50160,"databundleVersionId":7921029}],"dockerImageVersionId":31328,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n\nBASE_PATH = \"/kaggle/input/competitions/home-credit-credit-risk-model-stability/csv_files/\"\n\nprint(os.listdir(BASE_PATH + \"train\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:24:24.069404Z","iopub.execute_input":"2026-04-21T17:24:24.069656Z","iopub.status.idle":"2026-04-21T17:24:24.092621Z","shell.execute_reply.started":"2026-04-21T17:24:24.069632Z","shell.execute_reply":"2026-04-21T17:24:24.091869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom glob import glob\nimport gc","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:24:26.195840Z","iopub.execute_input":"2026-04-21T17:24:26.196480Z","iopub.status.idle":"2026-04-21T17:24:26.972635Z","shell.execute_reply.started":"2026-04-21T17:24:26.196448Z","shell.execute_reply":"2026-04-21T17:24:26.971866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BASE = \"/kaggle/input/competitions/home-credit-credit-risk-model-stability/csv_files/\"\n\ntrain_base = pd.read_csv(BASE + \"train/train_base.csv\")\ntest_base  = pd.read_csv(BASE + \"test/test_base.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:24:43.261837Z","iopub.execute_input":"2026-04-21T17:24:43.262665Z","iopub.status.idle":"2026-04-21T17:24:44.439584Z","shell.execute_reply.started":"2026-04-21T17:24:43.262633Z","shell.execute_reply":"2026-04-21T17:24:44.438643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_base","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:24:52.189795Z","iopub.execute_input":"2026-04-21T17:24:52.190707Z","iopub.status.idle":"2026-04-21T17:24:52.223528Z","shell.execute_reply.started":"2026-04-21T17:24:52.190661Z","shell.execute_reply":"2026-04-21T17:24:52.222841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_base","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:25:00.634031Z","iopub.execute_input":"2026-04-21T17:25:00.634362Z","iopub.status.idle":"2026-04-21T17:25:00.642886Z","shell.execute_reply.started":"2026-04-21T17:25:00.634336Z","shell.execute_reply":"2026-04-21T17:25:00.642057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import gc\nimport pandas as pd\n\ndef process_files(files, prefix):\n    result = None\n    \n    for i, f in enumerate(files):\n        print(\"Processing:\", f)\n\n        df = pd.read_csv(f, low_memory=False)\n\n        df = df.loc[:, ~df.columns.duplicated()]\n\n        if \"case_id\" not in df.columns:\n            continue\n\n        # اختيار numeric فقط\n        num_cols = [c for c in df.columns if df[c].dtype != \"object\" and c != \"case_id\"]\n        cols = [\"case_id\"] + num_cols[:10]\n\n        df = df[cols]\n\n        # aggregation\n        agg = df.groupby(\"case_id\").agg([\"mean\", \"max\"])\n\n        # flatten columns\n        agg.columns = [f\"{prefix}_{i}_{c[0]}_{c[1]}\" for c in agg.columns]\n        agg = agg.reset_index()\n\n        if result is None:\n            result = agg\n        else:\n            result = result.merge(agg, on=\"case_id\", how=\"outer\")\n\n        del df, agg\n        gc.collect()\n\n    return result","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:25:58.689638Z","iopub.execute_input":"2026-04-21T17:25:58.690355Z","iopub.status.idle":"2026-04-21T17:25:58.699386Z","shell.execute_reply.started":"2026-04-21T17:25:58.690296Z","shell.execute_reply":"2026-04-21T17:25:58.698561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from glob import glob\n\n# static\ntrain_static = process_files(\n    glob(BASE + \"train/train_static_0_*.csv\"),\n    prefix=\"static\"\n)\n\ntest_static = process_files(\n    glob(BASE + \"test/test_static_0_*.csv\"),\n    prefix=\"static\"\n)\n\n# credit bureau\ntrain_cb = process_files(\n    glob(BASE + \"train/train_credit_bureau_a_1_*.csv\"),\n    prefix=\"cb\"\n)\n\ntest_cb = process_files(\n    glob(BASE + \"test/test_credit_bureau_a_1_*.csv\"),\n    prefix=\"cb\"\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:26:20.564813Z","iopub.execute_input":"2026-04-21T17:26:20.565630Z","iopub.status.idle":"2026-04-21T17:30:54.500730Z","shell.execute_reply.started":"2026-04-21T17:26:20.565598Z","shell.execute_reply":"2026-04-21T17:30:54.500035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train_base.copy()\ntest  = test_base.copy()\n\nfor df in [train_static, train_cb]:\n    train = train.merge(df, on=\"case_id\", how=\"left\")\n    test  = test.merge(df, on=\"case_id\", how=\"left\")\n\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:34:37.761286Z","iopub.execute_input":"2026-04-21T17:34:37.762036Z","iopub.status.idle":"2026-04-21T17:34:41.245922Z","shell.execute_reply.started":"2026-04-21T17:34:37.762001Z","shell.execute_reply":"2026-04-21T17:34:41.245195Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def fix_missing(df):\n    missing_cols = {\n        col + \"_missing\": df[col].isna().astype(\"int8\")\n        for col in df.columns\n        if col != \"case_id\"\n    }\n    \n    df_missing = pd.DataFrame(missing_cols)\n    \n    df = pd.concat([df, df_missing], axis=1)\n    \n    df = df.fillna(-1)\n    \n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:36:54.320262Z","iopub.execute_input":"2026-04-21T17:36:54.320715Z","iopub.status.idle":"2026-04-21T17:36:54.325535Z","shell.execute_reply.started":"2026-04-21T17:36:54.320684Z","shell.execute_reply":"2026-04-21T17:36:54.324881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.copy()\ntest = test.copy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:37:05.595279Z","iopub.execute_input":"2026-04-21T17:37:05.596043Z","iopub.status.idle":"2026-04-21T17:37:07.169987Z","shell.execute_reply.started":"2026-04-21T17:37:05.596012Z","shell.execute_reply":"2026-04-21T17:37:07.169397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:37:14.401504Z","iopub.execute_input":"2026-04-21T17:37:14.401886Z","iopub.status.idle":"2026-04-21T17:37:14.413841Z","shell.execute_reply.started":"2026-04-21T17:37:14.401847Z","shell.execute_reply":"2026-04-21T17:37:14.413002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:37:36.156929Z","iopub.execute_input":"2026-04-21T17:37:36.157227Z","iopub.status.idle":"2026-04-21T17:37:36.373819Z","shell.execute_reply.started":"2026-04-21T17:37:36.157202Z","shell.execute_reply":"2026-04-21T17:37:36.372588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:39:30.534135Z","iopub.execute_input":"2026-04-21T17:39:30.534530Z","iopub.status.idle":"2026-04-21T17:39:30.699731Z","shell.execute_reply.started":"2026-04-21T17:39:30.534499Z","shell.execute_reply":"2026-04-21T17:39:30.698996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_df = df.drop(columns=[\"case_id\"]).isna().astype(\"int8\")\nmissing_df.columns = [c + \"_missing\" for c in missing_df.columns]\n\ndf = pd.concat([df, missing_df], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:39:40.162534Z","iopub.execute_input":"2026-04-21T17:39:40.162971Z","iopub.status.idle":"2026-04-21T17:39:41.650136Z","shell.execute_reply.started":"2026-04-21T17:39:40.162941Z","shell.execute_reply":"2026-04-21T17:39:41.649256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.fillna(-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:40:55.077472Z","iopub.execute_input":"2026-04-21T17:40:55.078173Z","iopub.status.idle":"2026-04-21T17:40:55.537254Z","shell.execute_reply.started":"2026-04-21T17:40:55.078137Z","shell.execute_reply":"2026-04-21T17:40:55.536606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.duplicated().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:41:04.362032Z","iopub.execute_input":"2026-04-21T17:41:04.362749Z","iopub.status.idle":"2026-04-21T17:41:07.963972Z","shell.execute_reply.started":"2026-04-21T17:41:04.362718Z","shell.execute_reply":"2026-04-21T17:41:07.963346Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:41:12.091584Z","iopub.execute_input":"2026-04-21T17:41:12.092006Z","iopub.status.idle":"2026-04-21T17:41:12.284079Z","shell.execute_reply.started":"2026-04-21T17:41:12.091975Z","shell.execute_reply":"2026-04-21T17:41:12.283325Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\nnumeric_cols = df.select_dtypes(include=[np.number]).columns\n\ntop_cols = df[numeric_cols].var().sort_values(ascending=False).head(20).index\n\nplot_df = df[top_cols].fillna(-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:41:20.692282Z","iopub.execute_input":"2026-04-21T17:41:20.692698Z","iopub.status.idle":"2026-04-21T17:41:22.429583Z","shell.execute_reply.started":"2026-04-21T17:41:20.692668Z","shell.execute_reply":"2026-04-21T17:41:22.428621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(18, 10))\n\nnp.log1p(plot_df).boxplot(rot=90, grid=False)\n\nplt.title(\"Log-Scaled Boxplot (Better Outlier Visibility)\", fontsize=16)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:41:30.677960Z","iopub.execute_input":"2026-04-21T17:41:30.678504Z","iopub.status.idle":"2026-04-21T17:41:51.000413Z","shell.execute_reply.started":"2026-04-21T17:41:30.678471Z","shell.execute_reply":"2026-04-21T17:41:50.999746Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target = \"target\"\n\ny = train[target]\ntrain = train.drop(columns=[target])\n\n# حذف missing العالي\nmissing = train.isnull().mean()\ncols = missing[missing < 0.7].index\n\ntrain = train[cols]\ntest  = test[cols.drop(target, errors='ignore')]\n\n# fillna\ntrain = train.fillna(-1)\ntest  = test.fillna(-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:42:30.553837Z","iopub.execute_input":"2026-04-21T17:42:30.554282Z","iopub.status.idle":"2026-04-21T17:42:32.050601Z","shell.execute_reply.started":"2026-04-21T17:42:30.554251Z","shell.execute_reply":"2026-04-21T17:42:32.049800Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in train.columns:\n    if train[col].dtype == \"float64\":\n        train[col] = train[col].astype(\"float32\")\n        test[col]  = test[col].astype(\"float32\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:42:39.829869Z","iopub.execute_input":"2026-04-21T17:42:39.830266Z","iopub.status.idle":"2026-04-21T17:42:40.003159Z","shell.execute_reply.started":"2026-04-21T17:42:39.830237Z","shell.execute_reply":"2026-04-21T17:42:40.002581Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.select_dtypes(include=[np.number])\ntest = test.select_dtypes(include=[np.number])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:42:48.137415Z","iopub.execute_input":"2026-04-21T17:42:48.138182Z","iopub.status.idle":"2026-04-21T17:42:48.464098Z","shell.execute_reply.started":"2026-04-21T17:42:48.138151Z","shell.execute_reply":"2026-04-21T17:42:48.463243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train.dtypes.value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:42:57.783070Z","iopub.execute_input":"2026-04-21T17:42:57.783611Z","iopub.status.idle":"2026-04-21T17:42:57.789286Z","shell.execute_reply.started":"2026-04-21T17:42:57.783581Z","shell.execute_reply":"2026-04-21T17:42:57.788373Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\n\ntrain = train_base.copy()\ntest = test_base.copy()\n\nfor df in [train_static, train_cb]:\n    train = train.merge(df, on=\"case_id\", how=\"left\")\n    test = test.merge(df, on=\"case_id\", how=\"left\")\n\ny = train[\"target\"]\ntrain = train.drop(columns=[\"target\"])\n\ntrain = train.apply(pd.to_numeric, errors=\"coerce\")\ntest = test.apply(pd.to_numeric, errors=\"coerce\")\n\ntrain = train.fillna(-1).astype(\"float32\")\ntest = test.fillna(-1).astype(\"float32\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:43:06.534243Z","iopub.execute_input":"2026-04-21T17:43:06.534656Z","iopub.status.idle":"2026-04-21T17:43:43.418957Z","shell.execute_reply.started":"2026-04-21T17:43:06.534627Z","shell.execute_reply":"2026-04-21T17:43:43.418264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(\n    train, y, test_size=0.2, random_state=42, stratify=y\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:43:43.420262Z","iopub.execute_input":"2026-04-21T17:43:43.421167Z","iopub.status.idle":"2026-04-21T17:43:45.196086Z","shell.execute_reply.started":"2026-04-21T17:43:43.421119Z","shell.execute_reply":"2026-04-21T17:43:45.195482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"normalizer = tf.keras.layers.Normalization()\nnormalizer.adapt(X_train.values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:43:57.726060Z","iopub.execute_input":"2026-04-21T17:43:57.726416Z","iopub.status.idle":"2026-04-21T17:43:59.987460Z","shell.execute_reply.started":"2026-04-21T17:43:57.726387Z","shell.execute_reply":"2026-04-21T17:43:59.986541Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inputs = tf.keras.Input(shape=(X_train.shape[1],))\nx = normalizer(inputs)\n\nx = tf.keras.layers.Dense(512, activation=\"relu\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Dropout(0.4)(x)\n\nx = tf.keras.layers.Dense(256, activation=\"relu\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Dropout(0.3)(x)\n\nx = tf.keras.layers.Dense(128, activation=\"relu\")(x)\nx = tf.keras.layers.Dropout(0.2)(x)\n\noutputs = tf.keras.layers.Dense(1, activation=\"sigmoid\")(x)\n\nmodel = tf.keras.Model(inputs, outputs)\n\nmodel.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),\n    loss=\"binary_crossentropy\",\n    metrics=[tf.keras.metrics.AUC(name=\"auc\")]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:44:15.154704Z","iopub.execute_input":"2026-04-21T17:44:15.155510Z","iopub.status.idle":"2026-04-21T17:44:16.574765Z","shell.execute_reply.started":"2026-04-21T17:44:15.155477Z","shell.execute_reply":"2026-04-21T17:44:16.574076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"callbacks = [\n    tf.keras.callbacks.EarlyStopping(\n        monitor=\"val_auc\",\n        patience=5,\n        mode=\"max\",\n        restore_best_weights=True\n    ),\n    tf.keras.callbacks.ReduceLROnPlateau(\n        monitor=\"val_auc\",\n        factor=0.5,\n        patience=2,\n        mode=\"max\",\n        min_lr=1e-6\n    )\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:44:27.587881Z","iopub.execute_input":"2026-04-21T17:44:27.588358Z","iopub.status.idle":"2026-04-21T17:44:27.592819Z","shell.execute_reply.started":"2026-04-21T17:44:27.588324Z","shell.execute_reply":"2026-04-21T17:44:27.592038Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train, y_train,\n    validation_data=(X_val, y_val),\n    epochs=50,\n    batch_size=1024,\n    callbacks=callbacks,\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:44:43.498500Z","iopub.execute_input":"2026-04-21T17:44:43.499417Z","iopub.status.idle":"2026-04-21T17:46:41.681044Z","shell.execute_reply.started":"2026-04-21T17:44:43.499384Z","shell.execute_reply":"2026-04-21T17:46:41.680417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\nfrom lightgbm import LGBMClassifier\nfrom sklearn.metrics import roc_auc_score\n\nmodel = LGBMClassifier(\n    n_estimators=8000,\n    learning_rate=0.01,\n    num_leaves=128,\n    subsample=0.8,\n    colsample_bytree=0.7,\n    random_state=42\n)\n\nmodel.fit(\n    X_train, y_train,\n    eval_set=[(X_val, y_val)],\n    eval_metric=\"auc\",\n    callbacks=[\n        lgb.early_stopping(300),\n        lgb.log_evaluation(100)\n    ]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:47:52.910706Z","iopub.execute_input":"2026-04-21T17:47:52.911556Z","iopub.status.idle":"2026-04-21T17:56:49.171098Z","shell.execute_reply.started":"2026-04-21T17:47:52.911523Z","shell.execute_reply":"2026-04-21T17:56:49.170475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = model.predict_proba(X_val)[:, 1]\nprint(\"AUC:\", roc_auc_score(y_val, pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T17:57:43.813924Z","iopub.execute_input":"2026-04-21T17:57:43.815446Z","iopub.status.idle":"2026-04-21T17:58:40.144335Z","shell.execute_reply.started":"2026-04-21T17:57:43.815409Z","shell.execute_reply":"2026-04-21T17:58:40.143404Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}