{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31040,"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-06-28T22:35:59.291809Z","iopub.execute_input":"2025-06-28T22:35:59.292562Z","iopub.status.idle":"2025-06-28T22:35:59.302965Z","shell.execute_reply.started":"2025-06-28T22:35:59.292536Z","shell.execute_reply":"2025-06-28T22:35:59.302234Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:35:59.855443Z","iopub.execute_input":"2025-06-28T22:35:59.855974Z","iopub.status.idle":"2025-06-28T22:36:01.562182Z","shell.execute_reply.started":"2025-06-28T22:35:59.855926Z","shell.execute_reply":"2025-06-28T22:36:01.561304Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:36:01.563747Z","iopub.execute_input":"2025-06-28T22:36:01.564109Z","iopub.status.idle":"2025-06-28T22:36:01.584132Z","shell.execute_reply.started":"2025-06-28T22:36:01.564080Z","shell.execute_reply":"2025-06-28T22:36:01.583365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:36:02.455468Z","iopub.execute_input":"2025-06-28T22:36:02.456117Z","iopub.status.idle":"2025-06-28T22:36:02.461874Z","shell.execute_reply.started":"2025-06-28T22:36:02.456090Z","shell.execute_reply":"2025-06-28T22:36:02.460995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['ord_2'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:36:02.993606Z","iopub.execute_input":"2025-06-28T22:36:02.994165Z","iopub.status.idle":"2025-06-28T22:36:03.043053Z","shell.execute_reply.started":"2025-06-28T22:36:02.994138Z","shell.execute_reply":"2025-06-28T22:36:03.042155Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.ord_2.fillna(\"NONE\").value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:36:04.144058Z","iopub.execute_input":"2025-06-28T22:36:04.144362Z","iopub.status.idle":"2025-06-28T22:36:04.230348Z","shell.execute_reply.started":"2025-06-28T22:36:04.144340Z","shell.execute_reply":"2025-06-28T22:36:04.229553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.ord_4 = df.ord_4.fillna(\"NONE\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:36:05.550030Z","iopub.execute_input":"2025-06-28T22:36:05.550759Z","iopub.status.idle":"2025-06-28T22:36:05.595750Z","shell.execute_reply.started":"2025-06-28T22:36:05.550730Z","shell.execute_reply":"2025-06-28T22:36:05.595113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['ord_4'].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:38:51.707144Z","iopub.execute_input":"2025-06-28T22:38:51.707842Z","iopub.status.idle":"2025-06-28T22:38:51.713084Z","shell.execute_reply.started":"2025-06-28T22:38:51.707815Z","shell.execute_reply":"2025-06-28T22:38:51.712346Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.loc[df['ord_4'].value_counts()[df['ord_4']].values < 2000,'ord_4']='RARE'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:40:06.086329Z","iopub.execute_input":"2025-06-28T22:40:06.086640Z","iopub.status.idle":"2025-06-28T22:40:06.181892Z","shell.execute_reply.started":"2025-06-28T22:40:06.086617Z","shell.execute_reply":"2025-06-28T22:40:06.181270Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:40:52.994400Z","iopub.execute_input":"2025-06-28T22:40:52.995226Z","iopub.status.idle":"2025-06-28T22:40:53.046280Z","shell.execute_reply.started":"2025-06-28T22:40:52.995191Z","shell.execute_reply":"2025-06-28T22:40:53.045391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mapping = {\n    \"Freezing\": 0,\n    \"Warm\" : 1 ,\n    \"Cold\" : 2 ,\n    \"Boiling Hot\": 3 ,\n    \"Hot\": 4 ,\n    \"Lava Hot\": 5\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:06:00.468569Z","iopub.execute_input":"2025-06-28T22:06:00.468877Z","iopub.status.idle":"2025-06-28T22:06:00.483106Z","shell.execute_reply.started":"2025-06-28T22:06:00.468850Z","shell.execute_reply":"2025-06-28T22:06:00.482183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.loc[:,'ord_2'] = df.ord_2.map(mapping)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:06:00.484102Z","iopub.execute_input":"2025-06-28T22:06:00.484354Z","iopub.status.idle":"2025-06-28T22:06:00.577071Z","shell.execute_reply.started":"2025-06-28T22:06:00.484334Z","shell.execute_reply":"2025-06-28T22:06:00.576100Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:06:00.577974Z","iopub.execute_input":"2025-06-28T22:06:00.578287Z","iopub.status.idle":"2025-06-28T22:06:00.954750Z","shell.execute_reply.started":"2025-06-28T22:06:00.578257Z","shell.execute_reply":"2025-06-28T22:06:00.953829Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\nexample = np.array(\n    [\n        [0,0,1],\n        [1,0,0],\n        [1,0,1]\n    ]\n)\n\nprint(example.nbytes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:06:00.957465Z","iopub.execute_input":"2025-06-28T22:06:00.958174Z","iopub.status.idle":"2025-06-28T22:06:00.964610Z","shell.execute_reply.started":"2025-06-28T22:06:00.958141Z","shell.execute_reply":"2025-06-28T22:06:00.963219Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[df.ord_2 == 3].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:06:00.965522Z","iopub.execute_input":"2025-06-28T22:06:00.965844Z","iopub.status.idle":"2025-06-28T22:06:01.047210Z","shell.execute_reply.started":"2025-06-28T22:06:00.965818Z","shell.execute_reply":"2025-06-28T22:06:01.046256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.groupby(['ord_2'])['id'].count()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:06:01.048087Z","iopub.execute_input":"2025-06-28T22:06:01.048374Z","iopub.status.idle":"2025-06-28T22:06:01.103038Z","shell.execute_reply.started":"2025-06-28T22:06:01.048347Z","shell.execute_reply":"2025-06-28T22:06:01.102318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.ord_2.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:29:16.748999Z","iopub.execute_input":"2025-06-28T22:29:16.749293Z","iopub.status.idle":"2025-06-28T22:29:16.787614Z","shell.execute_reply.started":"2025-06-28T22:29:16.749272Z","shell.execute_reply":"2025-06-28T22:29:16.786881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:06:29.187149Z","iopub.execute_input":"2025-06-28T22:06:29.187434Z","iopub.status.idle":"2025-06-28T22:06:31.131601Z","shell.execute_reply.started":"2025-06-28T22:06:29.187413Z","shell.execute_reply":"2025-06-28T22:06:31.130928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:06:40.713667Z","iopub.execute_input":"2025-06-28T22:06:40.714539Z","iopub.status.idle":"2025-06-28T22:06:42.499330Z","shell.execute_reply.started":"2025-06-28T22:06:40.714506Z","shell.execute_reply":"2025-06-28T22:06:42.498643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.loc[:,'target'] = -1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:07:29.937436Z","iopub.execute_input":"2025-06-28T22:07:29.938063Z","iopub.status.idle":"2025-06-28T22:07:29.942870Z","shell.execute_reply.started":"2025-06-28T22:07:29.938037Z","shell.execute_reply":"2025-06-28T22:07:29.942134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" data = pd.concat([df_train, df_test]).reset_index(drop=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:08:07.537813Z","iopub.execute_input":"2025-06-28T22:08:07.538450Z","iopub.status.idle":"2025-06-28T22:08:08.534043Z","shell.execute_reply.started":"2025-06-28T22:08:07.538426Z","shell.execute_reply":"2025-06-28T22:08:08.533368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:15:47.958475Z","iopub.execute_input":"2025-06-28T22:15:47.959115Z","iopub.status.idle":"2025-06-28T22:15:48.477777Z","shell.execute_reply.started":"2025-06-28T22:15:47.959090Z","shell.execute_reply":"2025-06-28T22:15:48.476992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [x for x in df_train.columns if x not in ['id','target']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:17:15.739651Z","iopub.execute_input":"2025-06-28T22:17:15.740219Z","iopub.status.idle":"2025-06-28T22:17:15.744371Z","shell.execute_reply.started":"2025-06-28T22:17:15.740185Z","shell.execute_reply":"2025-06-28T22:17:15.743594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:17:20.434425Z","iopub.execute_input":"2025-06-28T22:17:20.434998Z","iopub.status.idle":"2025-06-28T22:17:20.440492Z","shell.execute_reply.started":"2025-06-28T22:17:20.434971Z","shell.execute_reply":"2025-06-28T22:17:20.439786Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn import preprocessing","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:20:50.492742Z","iopub.execute_input":"2025-06-28T22:20:50.493299Z","iopub.status.idle":"2025-06-28T22:20:51.056342Z","shell.execute_reply.started":"2025-06-28T22:20:50.493277Z","shell.execute_reply":"2025-06-28T22:20:51.055634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for feature in features :\n    lbl_enc = preprocessing.LabelEncoder()\n    temp_col = data[feature].fillna(\"NONE\").astype(str).values\n    data.loc[:, feature] = lbl_enc.fit_transform(temp_col)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:20:51.768281Z","iopub.execute_input":"2025-06-28T22:20:51.769242Z","iopub.status.idle":"2025-06-28T22:20:59.793404Z","shell.execute_reply.started":"2025-06-28T22:20:51.769213Z","shell.execute_reply":"2025-06-28T22:20:59.792735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = data[data.target != -1].reset_index(drop=True)\ndf_test = data[data.target == -1].reset_index(drop=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-28T22:21:57.299536Z","iopub.execute_input":"2025-06-28T22:21:57.299863Z","iopub.status.idle":"2025-06-28T22:21:57.654591Z","shell.execute_reply.started":"2025-06-28T22:21:57.299840Z","shell.execute_reply":"2025-06-28T22:21:57.653907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn import linear_model\nfrom sklearn import metrics \nfrom sklearn import preprocessing \nimport pandas as pd\nfrom sklearn import model_selection","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T08:21:38.255064Z","iopub.execute_input":"2025-06-29T08:21:38.255409Z","iopub.status.idle":"2025-06-29T08:21:38.260535Z","shell.execute_reply.started":"2025-06-29T08:21:38.255384Z","shell.execute_reply":"2025-06-29T08:21:38.259331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T08:18:02.785736Z","iopub.execute_input":"2025-06-29T08:18:02.786123Z","iopub.status.idle":"2025-06-29T08:18:06.987178Z","shell.execute_reply.started":"2025-06-29T08:18:02.786093Z","shell.execute_reply":"2025-06-29T08:18:06.985584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['kfold'] = -1\ndf = df.sample(frac=1).reset_index(drop=True)\ny = df.target.values\nkf = model_selection.StratifiedKFold(n_splits=5)\nfor f,(t_,v_) in enumerate(kf.split(X=df,y=y)):\n    df.loc[v_,'kfold']=f","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T08:22:31.710790Z","iopub.execute_input":"2025-06-29T08:22:31.711121Z","iopub.status.idle":"2025-06-29T08:22:32.342721Z","shell.execute_reply.started":"2025-06-29T08:22:31.711097Z","shell.execute_reply":"2025-06-29T08:22:32.341561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T08:22:35.714198Z","iopub.execute_input":"2025-06-29T08:22:35.714540Z","iopub.status.idle":"2025-06-29T08:22:36.072661Z","shell.execute_reply.started":"2025-06-29T08:22:35.714516Z","shell.execute_reply":"2025-06-29T08:22:36.071216Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = ['ord_1', 'bin_1']\ndf.loc[:,x]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T08:30:15.898288Z","iopub.execute_input":"2025-06-29T08:30:15.898632Z","iopub.status.idle":"2025-06-29T08:30:15.915144Z","shell.execute_reply.started":"2025-06-29T08:30:15.898602Z","shell.execute_reply":"2025-06-29T08:30:15.914045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def run(fold):\n    feature = [f for f in df.columns if f not in (\"id\",\"target\",\"kfold\") ]\n    for col in feature :\n        df.loc[:,col] = df[col].astype(str).fillna(\"NONE\")\n    df_train = df[df.kfold != fold].reset_index(drop=True)\n    df_test = df[df.kfold == fold].reset_index(drop=True)\n    ohe = preprocessing.OneHotEncoder()\n    full_data = pd.concat([df_train[feature], df_test[feature]], axis=0)\n    ohe.fit(full_data[feature])\n    x_train = ohe.transform(df_train[feature])\n    x_test  = ohe.transform(df_test[feature])\n    model = linear_model.LogisticRegression()\n    model.fit(x_train, df_train.target.values)\n    test_preds = model.predict_proba(x_test)[:,1]\n\n    auc = metrics.roc_auc_score(df_test.target.values, test_preds)\n    print(auc)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T08:39:41.475249Z","iopub.execute_input":"2025-06-29T08:39:41.475606Z","iopub.status.idle":"2025-06-29T08:39:41.483186Z","shell.execute_reply.started":"2025-06-29T08:39:41.475581Z","shell.execute_reply":"2025-06-29T08:39:41.482136Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if __name__ == \"__main__\":\n    for i in range(5):\n        run(i)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T08:49:30.545607Z","iopub.execute_input":"2025-06-29T08:49:30.546295Z","iopub.status.idle":"2025-06-29T08:50:49.569507Z","shell.execute_reply.started":"2025-06-29T08:49:30.546268Z","shell.execute_reply":"2025-06-29T08:50:49.568436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}