{"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":"# 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-23T19:05:01.778796Z","iopub.execute_input":"2025-10-23T19:05:01.779149Z","iopub.status.idle":"2025-10-23T19:05:01.786089Z","shell.execute_reply.started":"2025-10-23T19:05:01.779124Z","shell.execute_reply":"2025-10-23T19:05:01.784806Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ====================================================\n# 1️⃣ Import Libraries\n# ====================================================\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import roc_auc_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-23T20:00:49.974144Z","iopub.execute_input":"2025-10-23T20:00:49.974533Z","iopub.status.idle":"2025-10-23T20:00:49.981095Z","shell.execute_reply.started":"2025-10-23T20:00:49.974507Z","shell.execute_reply":"2025-10-23T20:00:49.979635Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ====================================================\n# 2️⃣ Load Data\n# ====================================================\ntrain = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/test.csv\")\nsubmission = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/sample_submission.csv\")\n\nprint(\"Train shape:\", train.shape)\nprint(\"Test shape:\", test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-23T20:01:10.309385Z","iopub.execute_input":"2025-10-23T20:01:10.309742Z","iopub.status.idle":"2025-10-23T20:01:15.035385Z","shell.execute_reply.started":"2025-10-23T20:01:10.309717Z","shell.execute_reply":"2025-10-23T20:01:15.034370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ====================================================\n# 3️⃣ Exploratory Data Analysis (EDA)\n# ====================================================\nprint(\"\\n🧭 Dataset info:\")\nprint(train.info())\n\nprint(\"\\n📊 Basic statistics:\")\nprint(train.describe())\n\n# Target distribution\nsns.countplot(x=\"target\", data=train)\nplt.title(\"Target Distribution\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-23T20:01:50.608579Z","iopub.execute_input":"2025-10-23T20:01:50.609027Z","iopub.status.idle":"2025-10-23T20:01:51.560527Z","shell.execute_reply.started":"2025-10-23T20:01:50.608991Z","shell.execute_reply":"2025-10-23T20:01:51.559494Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ====================================================\n# 4️⃣ Handle Missing Data (Fixed Version)\n# ====================================================\nprint(\"\\n🚨 Missing values before imputation:\")\nprint(train.isnull().sum()[train.isnull().sum() > 0])\n\n# Separate target\ntarget = train[\"target\"]\ntrain_features = train.drop(\"target\", axis=1)\n\n# Identify numeric and categorical columns\nnum_cols = train_features.select_dtypes(include=[\"int64\", \"float64\"]).columns.tolist()\ncat_cols = train_features.select_dtypes(include=[\"object\"]).columns.tolist()\n\n# Numeric imputer\nnum_imputer = SimpleImputer(strategy=\"median\")\ntrain_features[num_cols] = num_imputer.fit_transform(train_features[num_cols])\ntest[num_cols] = num_imputer.transform(test[num_cols])\n\n# Categorical imputer\ncat_imputer = SimpleImputer(strategy=\"most_frequent\")\ntrain_features[cat_cols] = cat_imputer.fit_transform(train_features[cat_cols])\ntest[cat_cols] = cat_imputer.transform(test[cat_cols])\n\n# Reattach target\ntrain_imputed = train_features.copy()\ntrain_imputed[\"target\"] = target\ntest_imputed = test.copy()\n\nprint(\"\\n✅ Missing values imputed successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-23T20:03:06.551284Z","iopub.execute_input":"2025-10-23T20:03:06.551658Z","iopub.status.idle":"2025-10-23T20:03:11.014364Z","shell.execute_reply.started":"2025-10-23T20:03:06.551632Z","shell.execute_reply":"2025-10-23T20:03:11.013168Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ====================================================\n# 5️⃣ Feature Engineering (Fixed & Safe Encoding)\n# ====================================================\ncols_to_encode = [col for col in train_imputed.columns if col not in [\"id\", \"target\"]]\ncommon_cols = [col for col in cols_to_encode if col in test_imputed.columns]\n\nprint(f\"\\n🔧 Encoding {len(common_cols)} columns...\")\n\ntrain_encoded = train_imputed.copy()\ntest_encoded = test_imputed.copy()\n\nfor col in common_cols:\n    if train_encoded[col].dtype == \"object\" or test_encoded[col].dtype == \"object\":\n        le = LabelEncoder()\n        all_values = list(train_encoded[col].astype(str)) + list(test_encoded[col].astype(str))\n        le.fit(all_values)\n        train_encoded[col] = le.transform(train_encoded[col].astype(str))\n        test_encoded[col] = le.transform(test_encoded[col].astype(str))\n\nprint(\"\\n✅ Feature encoding completed!\")\n\n# Prepare final data\nX = train_encoded.drop([\"id\", \"target\"], axis=1)\ny = train_encoded[\"target\"]\nX_test = test_encoded.drop([\"id\"], axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-23T20:04:41.481944Z","iopub.execute_input":"2025-10-23T20:04:41.482352Z","iopub.status.idle":"2025-10-23T20:04:58.472332Z","shell.execute_reply.started":"2025-10-23T20:04:41.482325Z","shell.execute_reply":"2025-10-23T20:04:58.471229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ====================================================\n# 6️⃣ Train / Validation Split\n# ====================================================\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-23T20:07:40.444403Z","iopub.execute_input":"2025-10-23T20:07:40.444758Z","iopub.status.idle":"2025-10-23T20:07:40.800998Z","shell.execute_reply.started":"2025-10-23T20:07:40.444734Z","shell.execute_reply":"2025-10-23T20:07:40.800102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ====================================================\n# 7️⃣  Model Training (Final Safe Version)\n# ====================================================\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import roc_auc_score\n\n# 1️⃣ Check and handle NaNs\nprint(\"\\n🔎 Checking for NaN values before training...\")\nprint(\"NaN count in X:\", X.isnull().sum().sum())\nprint(\"NaN count in y:\", y.isnull().sum())\n\n# Replace any remaining NaN with 0\nX = X.fillna(0)\nX_test = X_test.fillna(0)\n\n# 2️⃣ Ensure everything is numeric\nnon_numeric_cols = X.select_dtypes(exclude=[\"int64\", \"float64\"]).columns\nif len(non_numeric_cols) > 0:\n    print(f\"⚠️ Found non-numeric columns: {list(non_numeric_cols)} — converting them.\")\n    for col in non_numeric_cols:\n        X[col] = pd.factorize(X[col])[0]\n\n# 3️⃣ Split the data\nX_train, X_valid, y_train, y_valid = train_test_split(\n    X, y, test_size=0.2, random_state=42\n)\n\n# 4️⃣ Sanity check\nprint(\"\\n✅ Data check before training:\")\nprint(\"X_train shape:\", X_train.shape)\nprint(\"X_valid shape:\", X_valid.shape)\nprint(\"Unique y values:\", y_train.unique())\n\n# 5️⃣ Train the model\nmodel = RandomForestClassifier(\n    n_estimators=300,\n    max_depth=15,\n    random_state=42,\n    n_jobs=-1\n)\n\nprint(\"\\n🚀 Training the Random Forest model...\")\nmodel.fit(X_train, y_train)\n\n# 6️⃣ Validation\ny_pred = model.predict_proba(X_valid)[:, 1]\nscore = roc_auc_score(y_valid, y_pred)\nprint(f\"\\n🎯 ROC-AUC Score on validation set: {score:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-23T20:10:43.693922Z","iopub.execute_input":"2025-10-23T20:10:43.694360Z","iopub.status.idle":"2025-10-23T20:13:15.199809Z","shell.execute_reply.started":"2025-10-23T20:10:43.694334Z","shell.execute_reply":"2025-10-23T20:13:15.198473Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ====================================================\n# 9️⃣ Test Predictions (Fixed Feature Alignment)\n# ====================================================\nprint(\"\\n🔎 Checking feature alignment between train and test...\")\n\nmissing_cols_in_test = [col for col in X.columns if col not in X_test.columns]\nextra_cols_in_test = [col for col in X_test.columns if col not in X.columns]\n\nprint(\"Missing in test:\", missing_cols_in_test)\nprint(\"Extra in test:\", extra_cols_in_test)\n\n# Add missing columns as 0\nfor col in missing_cols_in_test:\n    X_test[col] = 0\n\n# Remove extra columns\nX_test = X_test[[col for col in X.columns]]\n\nassert X_test.shape[1] == X.shape[1], \"❌ Feature mismatch after alignment!\"\n\nprint(\"\\n✅ Features aligned correctly!\")\n\nprint(\"\\n🚀 Generating test predictions...\")\ntest_pred = model.predict_proba(X_test)[:, 1]\n\n# Create submission file\nsubmission[\"target\"] = test_pred\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"\\n✅ submission.csv has been created successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-23T20:14:34.729302Z","iopub.execute_input":"2025-10-23T20:14:34.729655Z","iopub.status.idle":"2025-10-23T20:14:45.872290Z","shell.execute_reply.started":"2025-10-23T20:14:34.729634Z","shell.execute_reply":"2025-10-23T20:14:45.871273Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ====================================================\n# 🔟 Quick Sanity Test (Optional)\n# ====================================================\nsample_data = X_valid.sample(5, random_state=1)\nsample_pred = model.predict_proba(sample_data)[:, 1]\nprint(\"\\n🧪 Sample predictions:\", sample_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-23T20:15:21.909246Z","iopub.execute_input":"2025-10-23T20:15:21.909586Z","iopub.status.idle":"2025-10-23T20:15:22.017724Z","shell.execute_reply.started":"2025-10-23T20:15:21.909564Z","shell.execute_reply":"2025-10-23T20:15:22.016692Z"}},"outputs":[],"execution_count":null}]}