{"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":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\ndf = pd.read_csv(r\"/kaggle/input/cat-in-the-dat-ii/train.csv\")\n\ndf.drop('id', axis=1, inplace=True)\ndf.head()\ndf.info()\ndf.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T08:01:35.188289Z","iopub.execute_input":"2025-10-28T08:01:35.188613Z","iopub.status.idle":"2025-10-28T08:01:40.187411Z","shell.execute_reply.started":"2025-10-28T08:01:35.188588Z","shell.execute_reply":"2025-10-28T08:01:40.186523Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Select important features\nselected_features = [\n    'bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4',\n    'ord_0', 'ord_1', 'ord_2',\n    'nom_0', 'nom_1', 'nom_2', 'nom_3',\n    'day', 'month', 'target'\n]\n\ndf = df[selected_features]\n\n# Convert binary features to numeric\nbin_map = {'F': 0, 'T': 1, 'N': 0, 'Y': 1}\nfor col in ['bin_3', 'bin_4']:\n    df[col] = df[col].replace(bin_map)\n\nfor col in ['bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4']:\n    df[col] = pd.to_numeric(df[col], errors='coerce')\n\nfor col in df.columns:\n    if df[col].dtype == 'object':\n        df[col].fillna(df[col].mode()[0], inplace=True)\n    else:\n        df[col].fillna(df[col].median(), inplace=True)\n\nfor col in ['ord_1', 'ord_2']:\n    df[col] = df[col].astype('category').cat.codes\n\nprint(\"Dataset Overview\")\nprint(f\"Shape: {df.shape[0]} rows × {df.shape[1]} columns\\n\")\n\nprint(\"First 5 rows:\")\nprint(df.head(), \"\\n\")\n\nprint(\"Target distribution:\")\nprint(df['target'].value_counts(normalize=True).round(3), \"\\n\")\n\nprint(\"Data types:\")\nprint(df.dtypes, \"\\n\")\n\nprint(\"Summary statistics:\")\nprint(df.describe(include=[np.number]).T, \"\\n\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T08:01:40.188917Z","iopub.execute_input":"2025-10-28T08:01:40.189187Z","iopub.status.idle":"2025-10-28T08:01:41.822923Z","shell.execute_reply.started":"2025-10-28T08:01:40.189165Z","shell.execute_reply":"2025-10-28T08:01:41.821949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Unique value counts for categorical columns\ncategorical_cols = df.select_dtypes(include='object').columns.tolist()\nprint(\"Unique value counts for categorical columns:\")\nfor col in categorical_cols:\n    print(f\"{col}: {df[col].nunique()} unique values\")\n    print(df[col].value_counts().head(), \"\\n\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T08:01:41.823894Z","iopub.execute_input":"2025-10-28T08:01:41.824200Z","iopub.status.idle":"2025-10-28T08:01:42.145660Z","shell.execute_reply.started":"2025-10-28T08:01:41.824175Z","shell.execute_reply":"2025-10-28T08:01:42.143587Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Target distribution\nplt.figure(figsize=(6, 4))\nsns.countplot(x='target', data=df)\nplt.title(\"Target Class Distribution\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T08:01:42.147976Z","iopub.execute_input":"2025-10-28T08:01:42.148359Z","iopub.status.idle":"2025-10-28T08:01:42.344941Z","shell.execute_reply.started":"2025-10-28T08:01:42.148335Z","shell.execute_reply":"2025-10-28T08:01:42.343910Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Boxplot: ord_0 vs target\nplt.figure(figsize=(6, 4))\nsns.boxplot(x='target', y='ord_0', data=df)\nplt.title(\"ord_0 vs Target\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T08:01:42.345946Z","iopub.execute_input":"2025-10-28T08:01:42.346268Z","iopub.status.idle":"2025-10-28T08:01:42.586476Z","shell.execute_reply.started":"2025-10-28T08:01:42.346239Z","shell.execute_reply":"2025-10-28T08:01:42.585461Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Barplot: mean target by nom_1\nplt.figure(figsize=(6, 4))\ndf.groupby('nom_1')['target'].mean().sort_values().plot(kind='bar')\nplt.title(\"Mean Target by nom_1\")\nplt.ylabel(\"Target Rate\")\nplt.xticks(rotation=45)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T08:01:42.587394Z","iopub.execute_input":"2025-10-28T08:01:42.587698Z","iopub.status.idle":"2025-10-28T08:01:42.787971Z","shell.execute_reply.started":"2025-10-28T08:01:42.587677Z","shell.execute_reply":"2025-10-28T08:01:42.786889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Scatterplot: ord_2 vs day\nplt.figure(figsize=(6, 4))\nsns.scatterplot(x='ord_2', y='day', hue='target', data=df, alpha=0.6)\nplt.title(\"ord_2 vs day\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T08:01:42.789035Z","iopub.execute_input":"2025-10-28T08:01:42.789703Z","iopub.status.idle":"2025-10-28T08:02:09.755232Z","shell.execute_reply.started":"2025-10-28T08:01:42.789656Z","shell.execute_reply":"2025-10-28T08:02:09.754337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Pairplot: bin_0, bin_1, month\nsns.pairplot(df[['bin_0', 'bin_1', 'month', 'target']], hue='target')\nplt.suptitle(\"bin_0, bin_1, month\", y=1.02)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T08:02:09.756181Z","iopub.execute_input":"2025-10-28T08:02:09.756439Z","iopub.status.idle":"2025-10-28T08:05:18.709126Z","shell.execute_reply.started":"2025-10-28T08:02:09.756417Z","shell.execute_reply":"2025-10-28T08:05:18.708154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Correlation heatmap (numeric only)\nnumeric_cols = [\n    'bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4',\n    'ord_0', 'ord_1', 'ord_2', 'day', 'month', 'target'\n]\nplt.figure(figsize=(10, 8))\nsns.heatmap(df[numeric_cols].corr(), annot=True, cmap='coolwarm', fmt=\".2f\")\nplt.title(\"Correlation Matrix\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T08:05:18.710083Z","iopub.execute_input":"2025-10-28T08:05:18.710328Z","iopub.status.idle":"2025-10-28T08:05:19.472827Z","shell.execute_reply.started":"2025-10-28T08:05:18.710310Z","shell.execute_reply":"2025-10-28T08:05:19.471402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport lightgbm as lgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.preprocessing import OrdinalEncoder\n\ntrain=pd.read_csv(r\"/kaggle/input/cat-in-the-dat-ii/train.csv\")\ntest = pd.read_csv(r\"/kaggle/input/cat-in-the-dat-ii/test.csv\")\nsample_submission = pd.read_csv(r\"/kaggle/input/cat-in-the-dat-ii/sample_submission.csv\")\n\n# Separate features and target\nX = train.drop(['id', 'target'], axis=1)\ny = train['target']\nX_test = test.drop(['id'], axis=1)\n\n# missing values\nfor col in X.columns:\n    if X[col].isnull().sum() > 0:\n        if X[col].dtype == 'object':\n            X[col].fillna('missing', inplace=True)\n            X_test[col].fillna('missing', inplace=True)\n        else:\n            median = X[col].median()\n            X[col].fillna(median, inplace=True)\n            X_test[col].fillna(median, inplace=True)\n\n# Encoding categorical features\ncat_cols = X.select_dtypes(include='object').columns.tolist()\nencoder = OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-1)\nX[cat_cols] = encoder.fit_transform(X[cat_cols])\nX_test[cat_cols] = encoder.transform(X_test[cat_cols])\n\n# Split into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(\n    X, y, test_size=0.2, random_state=42, stratify=y)\n\n# LightGBM model\nmodel = lgb.LGBMClassifier(\n    n_estimators=2000,\n    learning_rate=0.03,\n    objective='binary',\n    random_state=42)\n\n# Train the model\nmodel.fit(X_train, y_train)\n\n# Evaluate on validation set\nval_preds = model.predict_proba(X_val)[:, 1]\nauc = roc_auc_score(y_val, val_preds)\nprint(\"ROC AUC on validation set:\", round(auc, 5))\n\n# Predict on test set\ntest_preds = model.predict_proba(X_test)[:, 1]\n\n# submission file\nsubmission = pd.DataFrame({\n    'id': test['id'],\n    'target': test_preds\n})\nsubmission.to_csv(\"submission.csv\", index=False)\nprint (submission.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-28T08:05:19.475554Z","iopub.execute_input":"2025-10-28T08:05:19.475894Z","iopub.status.idle":"2025-10-28T08:07:13.152594Z","shell.execute_reply.started":"2025-10-28T08:05:19.475863Z","shell.execute_reply":"2025-10-28T08:07:13.151713Z"}},"outputs":[],"execution_count":null}]}