{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":8540,"databundleVersionId":862041,"sourceType":"competition"},{"sourceId":7624767,"sourceType":"datasetVersion","datasetId":4441829},{"sourceId":7788617,"sourceType":"datasetVersion","datasetId":4558812}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\nfrom sklearn.ensemble import GradientBoostingClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.linear_model import LogisticRegression\nfrom tqdm import tqdm\nfrom imblearn.over_sampling import SMOTE\nfrom sklearn.utils import resample\nfrom sklearn.model_selection import cross_val_score\nimport xgboost as xgb\nimport lightgbm as lgb\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.ensemble import AdaBoostClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.svm import LinearSVC\nfrom sklearn.calibration import CalibratedClassifierCV\nfrom sklearn.ensemble import StackingClassifier\nfrom scipy.stats import mode\nfrom catboost import CatBoostClassifier, Pool\nfrom skopt import BayesSearchCV\nimport torch\nimport numpy as np\nfrom sklearn.preprocessing import MinMaxScaler, LabelEncoder, StandardScaler\nfrom sklearn.metrics import log_loss\nfrom sklearn.pipeline import make_pipeline\n\nimport gc \nfrom sklearn.model_selection import train_test_split, RandomizedSearchCV\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import roc_auc_score\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"700af4b2-ca93-4a2f-b7de-0fef3c212eb6","_cell_guid":"34ea337a-c686-4618-bfe8-ed89858725b8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:30:52.840803Z","iopub.execute_input":"2024-03-17T10:30:52.841096Z","iopub.status.idle":"2024-03-17T10:30:57.825949Z","shell.execute_reply.started":"2024-03-17T10:30:52.841046Z","shell.execute_reply":"2024-03-17T10:30:57.825108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_original = pd.read_csv('/kaggle/input/khois-adclick-data/test.csv')\ntrain_original = pd.read_csv('/kaggle/input/khois-adclick-data/train_resampled.csv')","metadata":{"_uuid":"45ea90fc-a81b-4a0f-a334-148d814c683f","_cell_guid":"9ddfadf3-666d-43b5-9be1-9d10a05c95c4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:30:57.827010Z","iopub.execute_input":"2024-03-17T10:30:57.827641Z","iopub.status.idle":"2024-03-17T10:30:58.019267Z","shell.execute_reply.started":"2024-03-17T10:30:57.827610Z","shell.execute_reply":"2024-03-17T10:30:58.018430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train_original.copy()\ntest = test_original.copy()","metadata":{"_uuid":"de2fa6c9-bdd3-4dbb-aa6c-7e04f4cbf072","_cell_guid":"36cb5987-aee7-4e57-8b0c-3dba906aff9f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:30:58.020401Z","iopub.execute_input":"2024-03-17T10:30:58.020695Z","iopub.status.idle":"2024-03-17T10:30:58.028002Z","shell.execute_reply.started":"2024-03-17T10:30:58.020669Z","shell.execute_reply":"2024-03-17T10:30:58.027104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"_uuid":"eedbd5f1-ab35-40b7-adbe-a41b8a17f449","_cell_guid":"0d6c387c-ef0d-4e41-b0ee-ac2c2c2a6ffd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:30:58.031213Z","iopub.execute_input":"2024-03-17T10:30:58.031498Z","iopub.status.idle":"2024-03-17T10:30:58.046456Z","shell.execute_reply.started":"2024-03-17T10:30:58.031473Z","shell.execute_reply":"2024-03-17T10:30:58.045516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:30:58.047439Z","iopub.execute_input":"2024-03-17T10:30:58.047693Z","iopub.status.idle":"2024-03-17T10:30:58.060044Z","shell.execute_reply.started":"2024-03-17T10:30:58.047669Z","shell.execute_reply":"2024-03-17T10:30:58.059102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(labels='Unnamed: 0', axis=1, inplace=True)\ntest.drop(labels='Unnamed: 0', axis=1, inplace=True)","metadata":{"_uuid":"164d1413-ceb7-4175-8f13-0a52f68bc183","_cell_guid":"a5cd4d34-b3fd-40fc-98f6-d0261741cbb7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:30:58.061087Z","iopub.execute_input":"2024-03-17T10:30:58.061368Z","iopub.status.idle":"2024-03-17T10:30:58.076521Z","shell.execute_reply.started":"2024-03-17T10:30:58.061344Z","shell.execute_reply":"2024-03-17T10:30:58.075772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:30:58.077489Z","iopub.execute_input":"2024-03-17T10:30:58.077809Z","iopub.status.idle":"2024-03-17T10:30:58.090558Z","shell.execute_reply.started":"2024-03-17T10:30:58.077783Z","shell.execute_reply":"2024-03-17T10:30:58.089682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:30:58.091669Z","iopub.execute_input":"2024-03-17T10:30:58.092252Z","iopub.status.idle":"2024-03-17T10:30:58.106630Z","shell.execute_reply.started":"2024-03-17T10:30:58.092220Z","shell.execute_reply":"2024-03-17T10:30:58.105791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Initial Processing","metadata":{"_uuid":"d1af0853-06e6-4242-ae8a-2041a3cdb0a5","_cell_guid":"9e64cb4f-d3cf-4a32-b29e-dd968b6d2040","trusted":true}},{"cell_type":"markdown","source":"### Missing Values","metadata":{"_uuid":"3572b4c1-f150-4e5f-bfbc-ed65f038e19d","_cell_guid":"437f7759-ca8d-4907-b4fb-7731ce373ebe","trusted":true}},{"cell_type":"code","source":"train.isnull().sum(axis = 0)","metadata":{"_uuid":"6fea26ac-727f-4957-9b5f-76b0c2f1e834","_cell_guid":"a72f3c65-72d8-4301-983e-6a48182d68b6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:30:58.107679Z","iopub.execute_input":"2024-03-17T10:30:58.107942Z","iopub.status.idle":"2024-03-17T10:30:58.134669Z","shell.execute_reply.started":"2024-03-17T10:30:58.107918Z","shell.execute_reply":"2024-03-17T10:30:58.133820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking missing data\ntotal = train.isnull().sum().sort_values(ascending = False)\npercent = (train.isnull().sum()/train.isnull().count()*100).sort_values(ascending = False)\nmissing_train_data  = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])\nmissing_train_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:30:58.135759Z","iopub.execute_input":"2024-03-17T10:30:58.136105Z","iopub.status.idle":"2024-03-17T10:30:58.195087Z","shell.execute_reply.started":"2024-03-17T10:30:58.136052Z","shell.execute_reply":"2024-03-17T10:30:58.194198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### What happened to attributed_time?","metadata":{"_uuid":"113c9fba-de23-4cfd-a312-98d7aec6ef47","_cell_guid":"e4d0b636-77ed-4e3e-b7f8-4d7770485f0f","trusted":true}},{"cell_type":"code","source":"temp1 = train.loc[~train.attributed_time.isnull(),'is_attributed']\nif np.sum(temp1.values) == len(temp1):\n  print('Same')\nelse:\n  print('No')\nprint('-'*50)\ntemp2 = train.loc[train.attributed_time.isnull(),'is_attributed']\nif np.sum(temp2.values) == 0:\n  print('Same')\nelse:\n  print('No')\n\n# whenever is_attributed == 1, then there is a corresponding time in attributed_time","metadata":{"_uuid":"c0f4d662-425f-4345-9541-7f59657df17b","_cell_guid":"db2c7f54-156e-48b7-a094-c25333a1240d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:30:58.196289Z","iopub.execute_input":"2024-03-17T10:30:58.196536Z","iopub.status.idle":"2024-03-17T10:30:58.216141Z","shell.execute_reply.started":"2024-03-17T10:30:58.196513Z","shell.execute_reply":"2024-03-17T10:30:58.215101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Label Distribution","metadata":{"_uuid":"682e36fb-c15d-46e7-9b78-178594cea178","_cell_guid":"4f8904f0-2954-41f9-8980-7317c968f426","trusted":true}},{"cell_type":"code","source":"fig = plt.figure(figsize=(6,5))\nplt.bar(train.is_attributed.value_counts().index, train.is_attributed.value_counts().values)\nplt.xlabel('labels')\nplt.ylabel('counts')\nplt.xticks([0,1])\nplt.show()","metadata":{"_uuid":"f224d6b9-a9ad-44c6-bcd1-8aa5df4b7775","_cell_guid":"d342da90-ac37-41e3-9294-d672f00cd87c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:30:58.220214Z","iopub.execute_input":"2024-03-17T10:30:58.220505Z","iopub.status.idle":"2024-03-17T10:30:58.362701Z","shell.execute_reply.started":"2024-03-17T10:30:58.220481Z","shell.execute_reply":"2024-03-17T10:30:58.361804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Feature Distribution","metadata":{"_uuid":"fdf820c9-4d18-4f8c-8d14-c0a54eee4a50","_cell_guid":"bd4fd867-7054-4e8d-92cc-f6c2533558ef","trusted":true}},{"cell_type":"code","source":"fig = plt.figure(figsize=(15,12))\nfor i, col in enumerate(['ip','app','device','os','channel']):\n  plt.subplot(3,2,i+1)\n  sns.histplot(data=train, x=col, label = col, kde=True)\nfig.tight_layout(pad=1.0)","metadata":{"_uuid":"75015512-e1b6-4001-97c2-6ae5f679cecb","_cell_guid":"f0dace91-8bf7-45c7-bb1d-6327a8b17e9e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:30:58.363670Z","iopub.execute_input":"2024-03-17T10:30:58.363960Z","iopub.status.idle":"2024-03-17T10:31:07.948599Z","shell.execute_reply.started":"2024-03-17T10:30:58.363934Z","shell.execute_reply":"2024-03-17T10:31:07.947605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Click Time Transformation","metadata":{"_uuid":"159a8433-3a4f-4cc4-a776-bda29467c229","_cell_guid":"814a15e8-6b7f-4831-8495-45a46a9e5d2c","trusted":true}},{"cell_type":"code","source":"def transformation(df):\n  df['click_time'] = pd.to_datetime(df.click_time)\n  df['day_of_week'] = df.click_time.dt.day_of_week\n  df['day'] = df.click_time.dt.day\n  df['hour'] = df.click_time.dt.hour\n  df['minute'] = df.click_time.dt.minute\n  df['second'] = df.click_time.dt.second\n  #df.drop(labels='click_time', axis=1, inplace = True)\n  return df\n\n# transformation\ntrain = transformation(train)\ntest = transformation(test)","metadata":{"_uuid":"b65c101a-3fff-491f-acd4-fe1aa56b6289","_cell_guid":"fd47a937-1dbd-4db2-8d8e-2efbe247a9dd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:31:07.950149Z","iopub.execute_input":"2024-03-17T10:31:07.950545Z","iopub.status.idle":"2024-03-17T10:31:08.021392Z","shell.execute_reply.started":"2024-03-17T10:31:07.950507Z","shell.execute_reply":"2024-03-17T10:31:08.020114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:31:08.022590Z","iopub.execute_input":"2024-03-17T10:31:08.022879Z","iopub.status.idle":"2024-03-17T10:31:08.036777Z","shell.execute_reply.started":"2024-03-17T10:31:08.022852Z","shell.execute_reply":"2024-03-17T10:31:08.035884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Deleting variables","metadata":{"_uuid":"75dbec6f-fcc8-4f63-8d36-eed61443e9d2","_cell_guid":"5157c386-511c-404d-9492-13652a50e54e","trusted":true}},{"cell_type":"code","source":"# store the label\ntrain_labels = train.is_attributed.values\ntest_labels = test.is_attributed.values\n\n# drop labels and attributed_time since it represnets the same info as the is_attributed\ntrain.drop(labels = ['attributed_time', 'is_attributed'], axis = 1, inplace = True)\ntest.drop(labels = ['attributed_time', 'is_attributed'], axis = 1, inplace = True)","metadata":{"_uuid":"317ed048-bd30-4fa0-b938-bf68d66963a8","_cell_guid":"100f348c-dc24-4ac3-8277-5b89379dde11","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T10:31:08.037861Z","iopub.execute_input":"2024-03-17T10:31:08.038140Z","iopub.status.idle":"2024-03-17T10:31:08.052731Z","shell.execute_reply.started":"2024-03-17T10:31:08.038115Z","shell.execute_reply":"2024-03-17T10:31:08.051880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:31:08.054294Z","iopub.execute_input":"2024-03-17T10:31:08.054701Z","iopub.status.idle":"2024-03-17T10:31:08.068556Z","shell.execute_reply.started":"2024-03-17T10:31:08.054667Z","shell.execute_reply":"2024-03-17T10:31:08.067623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def Normalized(df):\n#     df_col_names = df.columns\n#     x = df.values \n#     min_max_scaler = MinMaxScaler()\n#     x_scaled = min_max_scaler.fit_transform(x)\n#     df = pd.DataFrame(x_scaled)\n#     df.columns = df_col_names","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:31:08.069793Z","iopub.execute_input":"2024-03-17T10:31:08.070435Z","iopub.status.idle":"2024-03-17T10:31:08.077989Z","shell.execute_reply.started":"2024-03-17T10:31:08.070400Z","shell.execute_reply":"2024-03-17T10:31:08.077256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.preprocessing import MinMaxScaler\n\ndef Normalized(df):\n    numeric_columns = df.select_dtypes(include=['number']).columns  #selects only numeric columns\n    df_numeric = df[numeric_columns]\n    \n    min_max_scaler = MinMaxScaler()\n    x_scaled = min_max_scaler.fit_transform(df_numeric)\n    \n    df_normalized = pd.DataFrame(x_scaled, columns=df_numeric.columns)\n    return df_normalized\n","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:31:08.078890Z","iopub.execute_input":"2024-03-17T10:31:08.079178Z","iopub.status.idle":"2024-03-17T10:31:08.088514Z","shell.execute_reply.started":"2024-03-17T10:31:08.079150Z","shell.execute_reply":"2024-03-17T10:31:08.087726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def Normalized(df):\n#     df_col_names = df.columns\n#     x = df.values \n#     min_max_scaler = MinMaxScaler()\n#     x_scaled = min_max_scaler.fit_transform(x)\n#     df_normalized = pd.DataFrame(x_scaled, columns=df_col_names)\n#     return df_normalized","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:31:08.089650Z","iopub.execute_input":"2024-03-17T10:31:08.089917Z","iopub.status.idle":"2024-03-17T10:31:08.098606Z","shell.execute_reply.started":"2024-03-17T10:31:08.089893Z","shell.execute_reply":"2024-03-17T10:31:08.097577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Normalize')\ndf_train = Normalized(train)\ndf_train","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:31:08.099769Z","iopub.execute_input":"2024-03-17T10:31:08.100091Z","iopub.status.idle":"2024-03-17T10:31:08.152608Z","shell.execute_reply.started":"2024-03-17T10:31:08.100046Z","shell.execute_reply":"2024-03-17T10:31:08.151682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}