{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":17000,"databundleVersionId":895292,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:29:19.376597Z","iopub.execute_input":"2025-10-28T20:29:19.376841Z","iopub.status.idle":"2025-10-28T20:29:21.385933Z","shell.execute_reply.started":"2025-10-28T20:29:19.376817Z","shell.execute_reply":"2025-10-28T20:29:21.384809Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv(r'/kaggle/input/cat-in-the-dat-ii/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:30:10.866897Z","iopub.execute_input":"2025-10-28T20:30:10.867206Z","iopub.status.idle":"2025-10-28T20:30:14.354551Z","shell.execute_reply.started":"2025-10-28T20:30:10.867184Z","shell.execute_reply":"2025-10-28T20:30:14.353829Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport sklearn as sk","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:30:49.914118Z","iopub.execute_input":"2025-10-28T20:30:49.914936Z","iopub.status.idle":"2025-10-28T20:30:50.882799Z","shell.execute_reply.started":"2025-10-28T20:30:49.914904Z","shell.execute_reply":"2025-10-28T20:30:50.881792Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:30:58.920500Z","iopub.execute_input":"2025-10-28T20:30:58.921300Z","iopub.status.idle":"2025-10-28T20:30:58.941347Z","shell.execute_reply.started":"2025-10-28T20:30:58.921272Z","shell.execute_reply":"2025-10-28T20:30:58.940616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.describe()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:31:40.537104Z","iopub.execute_input":"2025-10-28T20:31:40.537870Z","iopub.status.idle":"2025-10-28T20:31:40.758207Z","shell.execute_reply.started":"2025-10-28T20:31:40.537843Z","shell.execute_reply":"2025-10-28T20:31:40.757448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x='target', data=train_data, palette='pastel')\nplt.title('Target Class Distribution', fontsize=14)\nplt.xlabel('Target')\nplt.ylabel('Count')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:31:47.672488Z","iopub.execute_input":"2025-10-28T20:31:47.672843Z","iopub.status.idle":"2025-10-28T20:31:47.944344Z","shell.execute_reply.started":"2025-10-28T20:31:47.672822Z","shell.execute_reply":"2025-10-28T20:31:47.943433Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.drop('id', axis=1, inplace=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:32:49.297021Z","iopub.execute_input":"2025-10-28T20:32:49.298232Z","iopub.status.idle":"2025-10-28T20:32:49.401609Z","shell.execute_reply.started":"2025-10-28T20:32:49.298192Z","shell.execute_reply":"2025-10-28T20:32:49.400469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:33:00.129536Z","iopub.execute_input":"2025-10-28T20:33:00.130179Z","iopub.status.idle":"2025-10-28T20:33:00.150768Z","shell.execute_reply.started":"2025-10-28T20:33:00.130153Z","shell.execute_reply":"2025-10-28T20:33:00.149950Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in train_data.columns:\n    print(f\"\\nColumn: {col}\")\n    print(train_data[col].unique())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:33:09.205841Z","iopub.execute_input":"2025-10-28T20:33:09.206122Z","iopub.status.idle":"2025-10-28T20:33:09.654763Z","shell.execute_reply.started":"2025-10-28T20:33:09.206103Z","shell.execute_reply":"2025-10-28T20:33:09.653371Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"binary_cols = ['bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4']\n\nplt.figure(figsize=(12, 6))\n\nfor i, col in enumerate(binary_cols, 1):\n    plt.subplot(1, len(binary_cols), i)\n    train_data[col].value_counts(dropna=False).plot.pie(\n        autopct='%1.1f%%',\n        startangle=90,\n        colors=['#66b3ff', '#99ff99', '#ffcc99'],  # Added color for NaN\n        textprops={'color': 'black', 'fontsize': 10}\n    )\n    plt.title(col, color='black', fontsize=12, weight='bold')\n    plt.ylabel('')\n\nplt.suptitle('Pie Chart of Binary Features (0 vs 1 vs NaN)', color='white', fontsize=16, weight='bold')\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:33:18.784457Z","iopub.execute_input":"2025-10-28T20:33:18.784758Z","iopub.status.idle":"2025-10-28T20:33:19.348040Z","shell.execute_reply.started":"2025-10-28T20:33:18.784734Z","shell.execute_reply":"2025-10-28T20:33:19.347197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.fillna(\"missing\", inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:33:27.257095Z","iopub.execute_input":"2025-10-28T20:33:27.257429Z","iopub.status.idle":"2025-10-28T20:33:28.512124Z","shell.execute_reply.started":"2025-10-28T20:33:27.257386Z","shell.execute_reply":"2025-10-28T20:33:28.511172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import category_encoders as ce\n\nencoder = ce.BinaryEncoder()\nbinary_encoded_data = encoder.fit_transform(train_data)\n\nprint(binary_encoded_data.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:33:54.538029Z","iopub.execute_input":"2025-10-28T20:33:54.538593Z","iopub.status.idle":"2025-10-28T20:34:21.743177Z","shell.execute_reply.started":"2025-10-28T20:33:54.538569Z","shell.execute_reply":"2025-10-28T20:34:21.742326Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%pip install catboost\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:44:27.877817Z","iopub.execute_input":"2025-10-28T20:44:27.878598Z","iopub.status.idle":"2025-10-28T20:44:33.143087Z","shell.execute_reply.started":"2025-10-28T20:44:27.878568Z","shell.execute_reply":"2025-10-28T20:44:33.141622Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from catboost import CatBoostClassifier, Pool\nfrom sklearn.model_selection import train_test_split\n\nX = train_data.drop('target', axis=1)\ny = train_data['target']\n\ncat_features = X.select_dtypes(include=['object', 'category']).columns.tolist()\n\nX[cat_features] = X[cat_features].astype(str)\nX[cat_features] = X[cat_features].fillna('missing')\n\nX_train, X_test, y_train, y_test = train_test_split(\n    X, y, test_size=0.2, random_state=42, stratify=y\n)\n\ntrain_pool = Pool(X_train, label=y_train, cat_features=cat_features)\ntest_pool = Pool(X_test, label=y_test, cat_features=cat_features)\n\nmodel = CatBoostClassifier(\n    iterations=500,\n    learning_rate=0.05,\n    depth=6,\n    loss_function='Logloss',\n    eval_metric='AUC',\n    random_seed=42,\n    verbose=100\n)\n\nmodel.fit(train_pool, eval_set=test_pool)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T20:45:01.142325Z","iopub.execute_input":"2025-10-28T20:45:01.143397Z","iopub.status.idle":"2025-10-28T20:55:55.002590Z","shell.execute_reply.started":"2025-10-28T20:45:01.143363Z","shell.execute_reply":"2025-10-28T20:55:55.001605Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/test.csv\")\n\ntest[cat_features] = test[cat_features].astype(str)\ntest[cat_features] = test[cat_features].fillna('missing')\n\ntest_pool = Pool(test, cat_features=cat_features)\n\n# --------------------------\n# 5. Predict probabilities\n# --------------------------\ntest_pred = model.predict_proba(test_pool)[:, 1]\n\n# --------------------------\n# 6. Save submission file\n# --------------------------\nsubmission = pd.DataFrame({\n    \"id\": test[\"id\"],\n    \"target\": test_pred\n})\n\nsubmission_path = \"/kaggle/working/submission.csv\"\nsubmission.to_csv(submission_path, index=False)\n\nprint(f\"✅ Submission file saved successfully at: {submission_path}\")\n\n# --------------------------\n# 7. Submit to Kaggle\n# --------------------------\n#!kaggle competitions submit -c cat-in-the-dat-ii -f /kaggle/working/submission.csv -m \"CatBoost baseline submission\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T21:41:43.194898Z","iopub.execute_input":"2025-10-28T21:41:43.195299Z","iopub.status.idle":"2025-10-28T21:41:52.865772Z","shell.execute_reply.started":"2025-10-28T21:41:43.195271Z","shell.execute_reply":"2025-10-28T21:41:52.862244Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}