{"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":4120,"databundleVersionId":44256,"sourceType":"competition"}],"dockerImageVersionId":31089,"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-07-15T17:32:09.500813Z","iopub.execute_input":"2025-07-15T17:32:09.501061Z","iopub.status.idle":"2025-07-15T17:32:11.777013Z","shell.execute_reply.started":"2025-07-15T17:32:09.501040Z","shell.execute_reply":"2025-07-15T17:32:11.776044Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import gzip\nimport shutil\n\ninput_path = '/kaggle/input/avazu-ctr-prediction/train.gz'\noutput_path = '/kaggle/working/train.csv'\n\nwith gzip.open(input_path, 'rb') as f_in:\n    with open(output_path, 'wb') as f_out:\n        shutil.copyfileobj(f_in, f_out)\n\nprint(\"Extraction complete! File saved as:\", output_path)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:33:40.697720Z","iopub.execute_input":"2025-07-15T17:33:40.698069Z","iopub.status.idle":"2025-07-15T17:34:36.705431Z","shell.execute_reply.started":"2025-07-15T17:33:40.698046Z","shell.execute_reply":"2025-07-15T17:34:36.703613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n_rows = 300000\n\n# Load 300,000 rows from the compressed train.gz file\ndf = pd.read_csv(\"/kaggle/working/train.csv\", nrows=n_rows)\n\n# Preview the data\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:35:25.994083Z","iopub.execute_input":"2025-07-15T17:35:25.995089Z","iopub.status.idle":"2025-07-15T17:35:27.854954Z","shell.execute_reply.started":"2025-07-15T17:35:25.995038Z","shell.execute_reply":"2025-07-15T17:35:27.854151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y = df['click'].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:35:32.340081Z","iopub.execute_input":"2025-07-15T17:35:32.340521Z","iopub.status.idle":"2025-07-15T17:35:32.347453Z","shell.execute_reply.started":"2025-07-15T17:35:32.340490Z","shell.execute_reply":"2025-07-15T17:35:32.346197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\nfrom sklearn.model_selection import train_test_split, GridSearchCV\nfrom sklearn.tree import DecisionTreeClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:35:35.378937Z","iopub.execute_input":"2025-07-15T17:35:35.379279Z","iopub.status.idle":"2025-07-15T17:35:37.018817Z","shell.execute_reply.started":"2025-07-15T17:35:35.379245Z","shell.execute_reply":"2025-07-15T17:35:37.017812Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.drop(['click', 'id', 'hour', 'device_id', 'device_ip', 'site_id', 'site_domain', 'site_category', 'app_id', 'app_domain', 'app_category', 'device_id', 'device_ip', 'device_model'], axis=1).values\n# X = df.drop(columns=[\"id\", \"click\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:35:44.579527Z","iopub.execute_input":"2025-07-15T17:35:44.579982Z","iopub.status.idle":"2025-07-15T17:35:44.593737Z","shell.execute_reply.started":"2025-07-15T17:35:44.579958Z","shell.execute_reply":"2025-07-15T17:35:44.592801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"enc = OneHotEncoder(handle_unknown= 'ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:35:47.455105Z","iopub.execute_input":"2025-07-15T17:35:47.455498Z","iopub.status.idle":"2025-07-15T17:35:47.461436Z","shell.execute_reply.started":"2025-07-15T17:35:47.455471Z","shell.execute_reply":"2025-07-15T17:35:47.460224Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# from sklearn.compose import make_column_selector\n# categorical_selector = make_column_selector(dtype_include='object')\n# categorical_columns = categorical_selector(X)\n\n# print(\"Categorical Columns:\", categorical_columns)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 80/20 split for training and testing\n\nX_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:36:24.796187Z","iopub.execute_input":"2025-07-15T17:36:24.796496Z","iopub.status.idle":"2025-07-15T17:36:24.883592Z","shell.execute_reply.started":"2025-07-15T17:36:24.796474Z","shell.execute_reply":"2025-07-15T17:36:24.882571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nenc_X_train =  enc.fit_transform(X_train)\nprint(enc_X_train[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:36:30.398369Z","iopub.execute_input":"2025-07-15T17:36:30.398799Z","iopub.status.idle":"2025-07-15T17:36:30.839425Z","shell.execute_reply.started":"2025-07-15T17:36:30.398765Z","shell.execute_reply":"2025-07-15T17:36:30.838349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"enc_X_test = enc.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:36:36.157161Z","iopub.execute_input":"2025-07-15T17:36:36.157557Z","iopub.status.idle":"2025-07-15T17:36:36.234662Z","shell.execute_reply.started":"2025-07-15T17:36:36.157525Z","shell.execute_reply":"2025-07-15T17:36:36.233715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"decision_tree = DecisionTreeClassifier(criterion='gini', min_samples_split=30)\nparameters = {'max_depth': [3, 10, None]}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:36:40.108934Z","iopub.execute_input":"2025-07-15T17:36:40.109228Z","iopub.status.idle":"2025-07-15T17:36:40.114445Z","shell.execute_reply.started":"2025-07-15T17:36:40.109207Z","shell.execute_reply":"2025-07-15T17:36:40.113101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"grid_search = GridSearchCV(decision_tree, parameters, n_jobs=-1, cv=3, scoring='roc_auc')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:36:42.922091Z","iopub.execute_input":"2025-07-15T17:36:42.922467Z","iopub.status.idle":"2025-07-15T17:36:42.926668Z","shell.execute_reply.started":"2025-07-15T17:36:42.922441Z","shell.execute_reply":"2025-07-15T17:36:42.925851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"grid_search.fit(X_train, Y_train)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:37:34.368230Z","iopub.execute_input":"2025-07-15T17:37:34.369027Z","iopub.status.idle":"2025-07-15T17:37:36.330515Z","shell.execute_reply.started":"2025-07-15T17:37:34.368999Z","shell.execute_reply":"2025-07-15T17:37:36.329723Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(grid_search.best_params_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:37:39.723636Z","iopub.execute_input":"2025-07-15T17:37:39.723963Z","iopub.status.idle":"2025-07-15T17:37:39.728679Z","shell.execute_reply.started":"2025-07-15T17:37:39.723941Z","shell.execute_reply":"2025-07-15T17:37:39.727721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"decision_tree_best = grid_search.best_estimator_\nprob = decision_tree_best.predict_proba(X_test)[:, 1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:36:56.203735Z","iopub.execute_input":"2025-07-15T17:36:56.204085Z","iopub.status.idle":"2025-07-15T17:36:56.217544Z","shell.execute_reply.started":"2025-07-15T17:36:56.204062Z","shell.execute_reply":"2025-07-15T17:36:56.216550Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score, log_loss\nauc = roc_auc_score(Y_test, prob)\nloss = log_loss(Y_test, prob)\n\nprint(f\"AUC Score: {auc:.4f}\")\nprint(f\"Log Loss: {loss:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-15T17:37:00.554345Z","iopub.execute_input":"2025-07-15T17:37:00.554691Z","iopub.status.idle":"2025-07-15T17:37:00.592883Z","shell.execute_reply.started":"2025-07-15T17:37:00.554665Z","shell.execute_reply":"2025-07-15T17:37:00.591903Z"}},"outputs":[],"execution_count":null}]}