{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.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":14999,"databundleVersionId":653984,"sourceType":"competition"}],"dockerImageVersionId":29662,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Kaggle - Categorical Feature Encoding Challenge\n## Target Prediction Analysis (Cat in the Dat) in Python using sklearn package","metadata":{"_uuid":"e7496cbe-5c0a-4ef3-8c93-248414db67bd","_cell_guid":"9a358394-ebff-4393-ae1b-bb3e3517e940","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"markdown","source":"## ⚒️ Importing Libraries & Reading Data","metadata":{"_uuid":"3a3278a3-de60-42da-8525-6be4d7323fb3","_cell_guid":"1c042036-b567-466e-8951-215f32856dd0","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"# Importing required packages\n\nimport numpy as np\nimport pandas as pd\npd.set_option('display.max_columns', 50)\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom sklearn.model_selection import train_test_split,GridSearchCV\nfrom sklearn.metrics import accuracy_score, f1_score, roc_auc_score,confusion_matrix,roc_curve,auc\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.preprocessing import StandardScaler,MinMaxScaler,PolynomialFeatures,LabelEncoder,OneHotEncoder\nfrom sklearn.decomposition import PCA\nfrom sklearn.neural_network import MLPClassifier","metadata":{"_uuid":"74623882-18c8-4104-a312-8afa717c4206","_cell_guid":"3f880fd0-ab18-48a2-a595-9387ef517d39","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:36:56.481446Z","iopub.execute_input":"2026-07-18T13:36:56.481835Z","iopub.status.idle":"2026-07-18T13:36:56.491070Z","shell.execute_reply.started":"2026-07-18T13:36:56.481804Z","shell.execute_reply":"2026-07-18T13:36:56.488921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/competitions/cat-in-the-dat-ii/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/competitions/cat-in-the-dat-ii/test.csv\")\ntest_ids = test[\"id\"]","metadata":{"_uuid":"a0621dd1-c9e8-463c-bbcb-f128dca94d87","_cell_guid":"8357ed16-8fe1-458d-84ce-f2c15bafa23a","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:36:56.493304Z","iopub.execute_input":"2026-07-18T13:36:56.493976Z","iopub.status.idle":"2026-07-18T13:36:58.453863Z","shell.execute_reply.started":"2026-07-18T13:36:56.493941Z","shell.execute_reply":"2026-07-18T13:36:58.452834Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 🌟 Exploratory Data Analysis (EDA)","metadata":{"_uuid":"1b99a48c-ed6d-498e-b7a9-f384dee7079f","_cell_guid":"d2cb4937-89d1-4540-a226-b8a0edf51c11","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"# Check number of features and data points in train and test\nprint(\"Number of data points in train: %d\" % train.shape[0])\nprint(\"Number of features in train: %d\" % train.shape[1])\n\nprint(\"Number of data points in test: %d\" % test.shape[0])\nprint(\"Number of features in test: %d\" % test.shape[1])","metadata":{"_uuid":"fb8ee1af-60f5-4cc4-81a7-e9216470ef95","_cell_guid":"65f859ec-142e-4c15-9ad1-27e71e5fbdbc","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:36:58.454870Z","iopub.execute_input":"2026-07-18T13:36:58.455067Z","iopub.status.idle":"2026-07-18T13:36:58.460956Z","shell.execute_reply.started":"2026-07-18T13:36:58.455049Z","shell.execute_reply":"2026-07-18T13:36:58.459666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"_uuid":"d07a1571-013e-45c9-91e9-39832672f229","_cell_guid":"e91810e6-8828-4e34-8176-dc633f8a5548","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:36:58.461944Z","iopub.execute_input":"2026-07-18T13:36:58.462208Z","iopub.status.idle":"2026-07-18T13:36:58.551094Z","shell.execute_reply.started":"2026-07-18T13:36:58.462183Z","shell.execute_reply":"2026-07-18T13:36:58.550307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.tail(10).T","metadata":{"_uuid":"b7812c99-99c8-443d-b04f-98de1857e722","_cell_guid":"a41100c6-438c-4762-8731-4cf9c1d5c4e7","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:36:58.553083Z","iopub.execute_input":"2026-07-18T13:36:58.553356Z","iopub.status.idle":"2026-07-18T13:36:58.565611Z","shell.execute_reply.started":"2026-07-18T13:36:58.553336Z","shell.execute_reply":"2026-07-18T13:36:58.564887Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Unique values in each column\nfor col in train.columns:\n    print(\"Unique entries in\",col,\" -\", train[col].nunique())","metadata":{"_uuid":"efef8252-1370-4c10-b961-43aefd18e2ac","_cell_guid":"dac9b4dc-6f72-419b-a0d4-bd9eb9889d9b","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:36:58.566915Z","iopub.execute_input":"2026-07-18T13:36:58.567206Z","iopub.status.idle":"2026-07-18T13:36:58.868664Z","shell.execute_reply.started":"2026-07-18T13:36:58.567178Z","shell.execute_reply":"2026-07-18T13:36:58.867561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['bin_3'] = train[\"bin_3\"].apply(lambda x: 0 if x == \"F\" else 1)\ntrain['bin_4'] = train[\"bin_4\"].apply(lambda x: 0 if x == \"N\" else 1)\n\ntest['bin_3'] = test[\"bin_3\"].apply(lambda x: 0 if x == \"F\" else 1)\ntest['bin_4'] = test[\"bin_4\"].apply(lambda x: 0 if x == \"N\" else 1)\n\ntrain['ord_5a'] = train[\"ord_5\"].str[0]\ntrain['ord_5b'] = train[\"ord_5\"].str[1]\n\ntest['ord_5a'] = test[\"ord_5\"].str[0]\ntest['ord_5b'] = test[\"ord_5\"].str[1]","metadata":{"_uuid":"05d6eb58-f8fd-424c-af3a-bf89f9273601","_cell_guid":"a8e40b29-1139-44dc-9b03-b28fba97b4a1","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:36:58.869523Z","iopub.execute_input":"2026-07-18T13:36:58.869758Z","iopub.status.idle":"2026-07-18T13:36:59.668140Z","shell.execute_reply.started":"2026-07-18T13:36:58.869736Z","shell.execute_reply":"2026-07-18T13:36:59.667087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"_uuid":"4c0bacad-0359-480b-9d26-8c18d1eb7028","_cell_guid":"db31dcbd-db3e-49c9-97b6-3b9399a6b28c","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:36:59.669435Z","iopub.execute_input":"2026-07-18T13:36:59.669689Z","iopub.status.idle":"2026-07-18T13:37:00.035917Z","shell.execute_reply.started":"2026-07-18T13:36:59.669668Z","shell.execute_reply":"2026-07-18T13:37:00.035135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.info()","metadata":{"_uuid":"d0364ed6-590e-498f-9762-864256737668","_cell_guid":"51ccd308-551d-4874-9a88-a1f28bbb05dd","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:37:00.037012Z","iopub.execute_input":"2026-07-18T13:37:00.037253Z","iopub.status.idle":"2026-07-18T13:37:00.283613Z","shell.execute_reply.started":"2026-07-18T13:37:00.037234Z","shell.execute_reply":"2026-07-18T13:37:00.282159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Checking for NULL/missing values\ntrain.isnull().sum()","metadata":{"_uuid":"039c244a-f486-4fef-8451-c1d107c4db12","_cell_guid":"34a9dcb3-768e-4517-80e9-5665f68c6701","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:37:00.284710Z","iopub.execute_input":"2026-07-18T13:37:00.285038Z","iopub.status.idle":"2026-07-18T13:37:00.642402Z","shell.execute_reply.started":"2026-07-18T13:37:00.285008Z","shell.execute_reply":"2026-07-18T13:37:00.641220Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Checking for NULL/missing values\ntest.isnull().sum()","metadata":{"_uuid":"6f7f3efb-6bcf-4483-926a-4e236ee6c0e4","_cell_guid":"fad753b4-8c18-4d1d-b63b-a0fcf1dfccb5","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:37:00.643481Z","iopub.execute_input":"2026-07-18T13:37:00.643790Z","iopub.status.idle":"2026-07-18T13:37:00.884963Z","shell.execute_reply.started":"2026-07-18T13:37:00.643757Z","shell.execute_reply":"2026-07-18T13:37:00.884257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Univariate analysis\nprint(train['target'].value_counts(),'\\n')\nprint(train['target'].value_counts(normalize=True)*100,'\\n')\nsns.countplot(train[\"target\"])","metadata":{"_uuid":"4dd521f9-efdc-4999-97e1-c35854734ce2","_cell_guid":"0036dc04-4fee-4922-a8b1-dfadcab2578d","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T13:37:00.885728Z","iopub.execute_input":"2026-07-18T13:37:00.885919Z","iopub.status.idle":"2026-07-18T14:38:29.023882Z","shell.execute_reply.started":"2026-07-18T13:37:00.885895Z","shell.execute_reply":"2026-07-18T14:38:29.022894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"_uuid":"9a3d98b1-5b73-4ae6-b852-8090c597c962","_cell_guid":"631d75e7-e6d1-40ee-b503-dc7a9e0c489e","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:29.025287Z","iopub.execute_input":"2026-07-18T14:38:29.026007Z","iopub.status.idle":"2026-07-18T14:38:29.044066Z","shell.execute_reply.started":"2026-07-18T14:38:29.025984Z","shell.execute_reply":"2026-07-18T14:38:29.043362Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['ord_1'].str.lower().value_counts().sort_values()","metadata":{"_uuid":"df26fb18-578c-4792-9435-b2bdb794c61d","_cell_guid":"bf58afa8-fec4-46f4-b267-04fd080b6c1f","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:29.047191Z","iopub.execute_input":"2026-07-18T14:38:29.048043Z","iopub.status.idle":"2026-07-18T14:38:29.248566Z","shell.execute_reply.started":"2026-07-18T14:38:29.047985Z","shell.execute_reply":"2026-07-18T14:38:29.247480Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['ord_2'].str.lower().value_counts().sort_values()","metadata":{"_uuid":"45d7f244-502c-4955-9241-0a62c5598549","_cell_guid":"41a9916a-393c-46a1-ad57-d93240144902","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:29.249485Z","iopub.execute_input":"2026-07-18T14:38:29.249707Z","iopub.status.idle":"2026-07-18T14:38:29.466293Z","shell.execute_reply.started":"2026-07-18T14:38:29.249687Z","shell.execute_reply":"2026-07-18T14:38:29.465330Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"_uuid":"98b0c69b-eba0-4cc4-8466-acb687cc59b6","_cell_guid":"edbe2c69-182d-42bd-b725-56b752c81014","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:29.467579Z","iopub.execute_input":"2026-07-18T14:38:29.467842Z","iopub.status.idle":"2026-07-18T14:38:29.486305Z","shell.execute_reply.started":"2026-07-18T14:38:29.467816Z","shell.execute_reply":"2026-07-18T14:38:29.485570Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"_uuid":"3cf6ee56-0537-4648-a650-94b00c66051e","_cell_guid":"e18159ce-07b7-4db4-abfc-d2abf7a97083","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:29.487285Z","iopub.execute_input":"2026-07-18T14:38:29.487489Z","iopub.status.idle":"2026-07-18T14:38:29.517807Z","shell.execute_reply.started":"2026-07-18T14:38:29.487471Z","shell.execute_reply":"2026-07-18T14:38:29.516643Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 🔍 Preparding Data for modelling","metadata":{"_uuid":"f6a7c512-0942-4d03-9f20-a5277f528175","_cell_guid":"35c143fa-6135-4db4-838d-4cdec003abe8","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"high_card_feats = ['nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9']\nfor col in high_card_feats:\n    train[f'hash_{col}'] = train[col].apply( lambda x: hash(str(x)) % 5000 )\n    test[f'hash_{col}'] = test[col].apply( lambda x: hash(str(x)) % 5000 )\n\ntrain.drop([\"id\",\"nom_5\",\"nom_6\",\"nom_7\",\"nom_8\",\"nom_9\",\"ord_5\"], axis=1, inplace=True)\ntest.drop([\"id\",\"nom_5\",\"nom_6\",\"nom_7\",\"nom_8\",\"nom_9\",\"ord_5\"], axis=1, inplace=True)","metadata":{"_uuid":"52c4c09a-a1ae-4ce2-b089-9a19ffa8d92e","_cell_guid":"0e8fdf62-af9c-462e-a2ec-b436efcc4c64","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:29.519014Z","iopub.execute_input":"2026-07-18T14:38:29.519322Z","iopub.status.idle":"2026-07-18T14:38:31.130431Z","shell.execute_reply.started":"2026-07-18T14:38:29.519296Z","shell.execute_reply":"2026-07-18T14:38:31.129685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"_uuid":"61282835-6a7e-4c0c-914a-bb54ab0df333","_cell_guid":"c6621316-2eff-426e-80b0-3082da86b813","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:31.131313Z","iopub.execute_input":"2026-07-18T14:38:31.131573Z","iopub.status.idle":"2026-07-18T14:38:31.149022Z","shell.execute_reply.started":"2026-07-18T14:38:31.131556Z","shell.execute_reply":"2026-07-18T14:38:31.148196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"_uuid":"9f1cbecb-9f17-436a-9017-0a075ff6bc0e","_cell_guid":"401641bf-01de-4299-a277-b49d0892e18c","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:31.150651Z","iopub.execute_input":"2026-07-18T14:38:31.151008Z","iopub.status.idle":"2026-07-18T14:38:31.175983Z","shell.execute_reply.started":"2026-07-18T14:38:31.150975Z","shell.execute_reply":"2026-07-18T14:38:31.175151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train_new = pd.DataFrame()\n# le = LabelEncoder()\n# for c in train.columns:\n#     if(train[c].dtype == 'object'): train_new[c] = le.fit_transform(train[c])\n#     else:      train_new[c] = train[c]\n\n# train_new.head()","metadata":{"_uuid":"9c5a8fdf-d572-49c7-82ff-fa5170583f71","_cell_guid":"9c1b1d9f-18cb-4e67-aa76-5e09f9c3117c","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:31.176950Z","iopub.execute_input":"2026-07-18T14:38:31.177230Z","iopub.status.idle":"2026-07-18T14:38:31.192284Z","shell.execute_reply.started":"2026-07-18T14:38:31.177208Z","shell.execute_reply":"2026-07-18T14:38:31.190892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_new = pd.DataFrame()\n# for c in test.columns:\n#     if(test[c].dtype == 'object'): test_new[c] = le.transform(test[c])\n#     else:      test_new[c] = test[c]\n\n# test_new.head()","metadata":{"_uuid":"e6bd71c9-e711-4ccc-a24f-a507c4f83e4d","_cell_guid":"f15bf217-f6ad-4d52-a1b8-166ffa05a37e","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:31.193280Z","iopub.execute_input":"2026-07-18T14:38:31.193532Z","iopub.status.idle":"2026-07-18T14:38:31.211982Z","shell.execute_reply.started":"2026-07-18T14:38:31.193503Z","shell.execute_reply":"2026-07-18T14:38:31.211168Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# One Hot Encoding\n\n# str_cols= train.loc[:, train.dtypes=='object'].columns.tolist()\n# str_cols\n\n# train = pd.get_dummies(train, columns=str_cols, drop_first=True)\n# test = pd.get_dummies(test, columns=str_cols, drop_first=True)\n\n# onehot_enc = OneHotEncoder()\n# # train = onehot_enc.fit_transform(train)\n# # test = onehot_enc.transform(test)\n\n# import category_encoders as ce\n# ohe = ce.OneHotEncoder(handle_unknown='ignore', use_cat_names=True)\n# train = ohe.fit_transform(train)\n# test = ohe.transform(test)","metadata":{"_uuid":"b6a65ddb-47a4-4487-b621-53c8b8175cb1","_cell_guid":"f2bb2129-34d8-4e0b-b7d4-7b9a21c713a0","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:31.213381Z","iopub.execute_input":"2026-07-18T14:38:31.213608Z","iopub.status.idle":"2026-07-18T14:38:31.230020Z","shell.execute_reply.started":"2026-07-18T14:38:31.213583Z","shell.execute_reply":"2026-07-18T14:38:31.229193Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target = train.pop('target')\ndata = pd.concat([train, test])\ndummies = pd.get_dummies(data, columns=data.columns, drop_first=True, sparse=True)\ntrain = dummies.iloc[:train.shape[0], :]\ntest = dummies.iloc[train.shape[0]:, :]\ntrain = train.sparse.to_coo().tocsr()\ntest = test.sparse.to_coo().tocsr()","metadata":{"_uuid":"bb1f9bc9-f972-42fc-80eb-9527e8158fc0","_cell_guid":"0cbf78ad-a103-4b30-967c-c6c4312fc4eb","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:31.231251Z","iopub.execute_input":"2026-07-18T14:38:31.231535Z","iopub.status.idle":"2026-07-18T14:38:41.617453Z","shell.execute_reply.started":"2026-07-18T14:38:31.231509Z","shell.execute_reply":"2026-07-18T14:38:41.616358Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train2 = train.drop(['target'], axis=1)\n# target = train[\"target\"]\n# train2 = train2.loc[:, test.columns]","metadata":{"_uuid":"04daa752-5970-42d6-9730-e84efd1867ac","_cell_guid":"56a579c3-ae58-40b9-9618-7d6710d31b84","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.618596Z","iopub.execute_input":"2026-07-18T14:38:41.618893Z","iopub.status.idle":"2026-07-18T14:38:41.623236Z","shell.execute_reply.started":"2026-07-18T14:38:41.618845Z","shell.execute_reply":"2026-07-18T14:38:41.622184Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Using imblearn for Balancing Data\n# from imblearn.over_sampling import SMOTE\n# sm = SMOTE(random_state=2019)\n\n# from imblearn.over_sampling import ADASYN\n# sm = ADASYN()\n\n# from imblearn.over_sampling import SVMSMOTE\n# sm = SVMSMOTE(random_state=2019)\n\n# from imblearn.combine import SMOTETomek\n# sm = SMOTETomek(ratio='auto')\n\n# from imblearn.combine import SMOTEENN\n# sm = SMOTEENN(random_state=2019)\n\n# train2, target = sm.fit_sample(train2, target.ravel())\n\n# from collections import Counter\n# print('Resampled dataset shape %s' % Counter(target))\n\n# from imblearn.under_sampling import NearMiss\n# nr = NearMiss()\n# train2, target = nr.fit_sample(train2, target.ravel())\n# np.bincount(target)","metadata":{"_uuid":"b5bb0c55-238b-4073-b146-ff3bdc49b023","_cell_guid":"0e1f409f-af6d-4638-97b9-656093219aad","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.624202Z","iopub.execute_input":"2026-07-18T14:38:41.624435Z","iopub.status.idle":"2026-07-18T14:38:41.646256Z","shell.execute_reply.started":"2026-07-18T14:38:41.624388Z","shell.execute_reply":"2026-07-18T14:38:41.645174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Scaling Data\n# #scaler = MinMaxScaler()\n# scaler = StandardScaler()\n# train2 = scaler.fit_transform(train2)\n# test = scaler.transform(test)","metadata":{"_uuid":"24d3c8db-321b-4cbe-85f0-5a6b711a9460","_cell_guid":"2a324c6d-b248-46d7-ab04-3a1de6680a83","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.647086Z","iopub.execute_input":"2026-07-18T14:38:41.647445Z","iopub.status.idle":"2026-07-18T14:38:41.665228Z","shell.execute_reply.started":"2026-07-18T14:38:41.647423Z","shell.execute_reply":"2026-07-18T14:38:41.664177Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# poly = PolynomialFeatures(degree=1)\n# train2 = poly.fit_transform(train2)\n# test = poly.transform(test)\n# poly\n# print(\"train2 shape:\", train2.shape)\n\n# # # train2 shape: (14272, 44)","metadata":{"_uuid":"936e3d7f-6589-4820-b203-a12e49daee3c","_cell_guid":"077f7a50-5e33-4fe9-b71d-81224423fdd5","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.666674Z","iopub.execute_input":"2026-07-18T14:38:41.666986Z","iopub.status.idle":"2026-07-18T14:38:41.683040Z","shell.execute_reply.started":"2026-07-18T14:38:41.666956Z","shell.execute_reply":"2026-07-18T14:38:41.682027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# pca = PCA(random_state=2019)\n# #pca = PCA(random_state=2019, n_components=200)\n# train2 = pca.fit_transform(train2)\n\n# test = pca.transform(test)\n# pca","metadata":{"_uuid":"9a290c30-34c0-4e5d-9e71-f4e72a71fe9e","_cell_guid":"5f7bab87-e16f-41ba-9755-63df32ceb23e","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.684003Z","iopub.execute_input":"2026-07-18T14:38:41.684246Z","iopub.status.idle":"2026-07-18T14:38:41.705145Z","shell.execute_reply.started":"2026-07-18T14:38:41.684225Z","shell.execute_reply":"2026-07-18T14:38:41.704154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(train, target, test_size=0.25, random_state=2026)\nprint(x_train.shape, x_val.shape)","metadata":{"_uuid":"5ff454f5-1530-47d8-b8b7-99ddc364a4a0","_cell_guid":"6b2b2922-0d93-4b13-8fdd-d2ae0c7cf88d","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.706279Z","iopub.execute_input":"2026-07-18T14:38:41.706573Z","iopub.status.idle":"2026-07-18T14:38:41.819518Z","shell.execute_reply.started":"2026-07-18T14:38:41.706542Z","shell.execute_reply":"2026-07-18T14:38:41.818610Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 💎 Machine Learning/Neural Networks","metadata":{"_uuid":"182e87be-2b0b-479c-9ede-97df47cdd2c4","_cell_guid":"4e07b86c-8a25-4c19-9445-8d8fdd3b4f61","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"sns.set('talk', 'whitegrid', 'dark', font_scale=1, font='Ricty',rc={\"lines.linewidth\": 2, 'grid.linestyle': '--'})\n\n# Receiver Operating Characteristic\ndef plotAUC(truth, pred, lab):\n    fpr, tpr, _ = roc_curve(truth,pred)\n    roc_auc = auc(fpr, tpr)\n    lw = 2\n    c = (np.random.rand(), np.random.rand(), np.random.rand())\n    plt.plot(fpr, tpr, color= c,lw=lw, label= lab +'(AUC = %0.2f)' % roc_auc)\n    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')\n    plt.xlim([0.0, 1.0])\n    plt.ylim([0.0, 1.0])\n    plt.xlabel('False Positive Rate')\n    plt.ylabel('True Positive Rate')\n    plt.title('ROC curve')\n    plt.legend(loc=\"lower right\")","metadata":{"_uuid":"89916944-2502-4ce7-bdb5-23032448e340","_cell_guid":"9e5a5d79-25c2-46a5-8e2d-3ee6190eb97d","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.820643Z","iopub.execute_input":"2026-07-18T14:38:41.820868Z","iopub.status.idle":"2026-07-18T14:38:41.827687Z","shell.execute_reply.started":"2026-07-18T14:38:41.820849Z","shell.execute_reply":"2026-07-18T14:38:41.826831Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# #clf_NN = MLPClassifier(random_state=2019, hidden_layer_sizes=(100, 100, 100))\n# clf_NN = MLPClassifier(activation='tanh', alpha=0.0001, max_iter=200, hidden_layer_sizes=(50, 50, 50), random_state=2019, solver='sgd')\n# clf_NN.fit(x_train, y_train)\n# y_val_pred = clf_NN.predict(x_val)\n# predictprob = clf_NN.predict_proba(x_val)[:,1]\n# y_val_pred\n# y_pred = clf_NN.predict(test)","metadata":{"_uuid":"5458a97f-cac6-43d0-a600-9916958097ea","_cell_guid":"979dd5ee-e669-4d76-90a7-3f1ce1a4b384","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.829165Z","iopub.execute_input":"2026-07-18T14:38:41.829448Z","iopub.status.idle":"2026-07-18T14:38:41.853096Z","shell.execute_reply.started":"2026-07-18T14:38:41.829424Z","shell.execute_reply":"2026-07-18T14:38:41.851971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#clf_NN","metadata":{"_uuid":"05c52638-0977-4ded-b2b9-8181903b9a33","_cell_guid":"1cca8b9d-e8a8-44cb-9b40-6807a9e386e0","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.854024Z","iopub.execute_input":"2026-07-18T14:38:41.854275Z","iopub.status.idle":"2026-07-18T14:38:41.876004Z","shell.execute_reply.started":"2026-07-18T14:38:41.854255Z","shell.execute_reply":"2026-07-18T14:38:41.874480Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Random Forest\n# rf = RandomForestClassifier(n_estimators=800, random_state = 2019).fit(x_train, y_train)\n# rf","metadata":{"_uuid":"a4963a9d-ae74-47c1-a9f6-bfeabc21abff","_cell_guid":"208d67df-dcee-4c13-8a35-cca0e8b6061a","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.877399Z","iopub.execute_input":"2026-07-18T14:38:41.877676Z","iopub.status.idle":"2026-07-18T14:38:41.897778Z","shell.execute_reply.started":"2026-07-18T14:38:41.877652Z","shell.execute_reply":"2026-07-18T14:38:41.896711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rf = LogisticRegression(C=0.1338, solver=\"lbfgs\", tol=0.003, max_iter=4000)\nrf.fit(x_train, y_train)","metadata":{"_uuid":"f08554c8-c4cc-4a6b-996b-4d85b41f5591","_cell_guid":"10f0a187-9ed9-4262-9f08-3fb5985a1dca","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:41.899207Z","iopub.execute_input":"2026-07-18T14:38:41.899471Z","iopub.status.idle":"2026-07-18T14:38:42.509604Z","shell.execute_reply.started":"2026-07-18T14:38:41.899448Z","shell.execute_reply":"2026-07-18T14:38:42.509068Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_val_pred = rf.predict(x_val)\npredictprob = rf.predict_proba(x_val)[:,1]\ny_pred = rf.predict(test)\ny_pred","metadata":{"_uuid":"dafb91cb-4c82-4a43-9072-60b75e22b785","_cell_guid":"add4a671-ae5a-4cbb-9430-1f548d137abe","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:42.510342Z","iopub.execute_input":"2026-07-18T14:38:42.510533Z","iopub.status.idle":"2026-07-18T14:38:42.565897Z","shell.execute_reply.started":"2026-07-18T14:38:42.510514Z","shell.execute_reply":"2026-07-18T14:38:42.565423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Accuracy = accuracy_score(y_val, y_val_pred)\nprint(Accuracy)\nplotAUC(y_val, predictprob, 'MLP')\nplt.show()\n\n# ROC 0.745\n# AUC 0.76","metadata":{"_uuid":"8ebf812d-4d07-4aa5-86dc-25df5db2a1d2","_cell_guid":"807410b6-5165-4114-9c44-5c28f84cfae5","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:42.567397Z","iopub.execute_input":"2026-07-18T14:38:42.567608Z","iopub.status.idle":"2026-07-18T14:38:42.792414Z","shell.execute_reply.started":"2026-07-18T14:38:42.567589Z","shell.execute_reply":"2026-07-18T14:38:42.791368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Confusion Matrix\ncm = confusion_matrix(y_val, y_val_pred).T\ncm = cm.astype('float')/cm.sum(axis=0)\nax = sns.heatmap(cm, annot=True, cmap='Blues');\nax.set_xlabel('True Label',size=12)\nax.set_ylabel('Predicted Label',size=12)\n\n# # TP 0.815 TN 0.44\n# RF # TP 0.96 TN 0.17\n# SMOTE TP 0.96 TN 0.63","metadata":{"_uuid":"d830c09a-a990-45e3-868a-e2f58a6dd6cf","_cell_guid":"bdb5ec32-54c0-4623-b87b-f494346c5ab9","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:42.793372Z","iopub.execute_input":"2026-07-18T14:38:42.793585Z","iopub.status.idle":"2026-07-18T14:38:43.040867Z","shell.execute_reply.started":"2026-07-18T14:38:42.793565Z","shell.execute_reply":"2026-07-18T14:38:43.039626Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 💥 Predicting Cat in the Dat","metadata":{"_uuid":"40d09efe-159d-407f-812f-bccb6e92ff4a","_cell_guid":"932cafc3-f966-4eaa-9bcd-645466a68a37","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"submission = pd.DataFrame(data = {\"id\":test_ids, \"target\":y_pred})\nprint(submission['target'].value_counts())\nsubmission.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"_uuid":"72b00e5c-be29-4932-9736-1cf198c71006","_cell_guid":"6e238181-245f-48b6-8b14-375117f26cd5","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2026-07-18T14:38:43.041872Z","iopub.execute_input":"2026-07-18T14:38:43.042162Z","iopub.status.idle":"2026-07-18T14:38:43.287787Z","shell.execute_reply.started":"2026-07-18T14:38:43.042127Z","shell.execute_reply":"2026-07-18T14:38:43.286793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"_uuid":"4a6fe67c-a154-4d58-b819-8e7049f41a49","_cell_guid":"944f18ac-ab13-4025-8b2d-529e3e3e9bae","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null}]}