{"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":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\nimport warnings\nimport gzip\nimport lightgbm\n\nwarnings.filterwarnings(\"ignore\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:20:33.035741Z","iopub.execute_input":"2026-06-16T04:20:33.035946Z","iopub.status.idle":"2026-06-16T04:20:38.908503Z","shell.execute_reply.started":"2026-06-16T04:20:33.035919Z","shell.execute_reply":"2026-06-16T04:20:38.907766Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TRAIN_PATH = \"/kaggle/input/competitions/avazu-ctr-prediction/train.gz\"\nSAMPLE_SIZE = 5000000\nRANDOM_SEED = 42","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:20:38.910171Z","iopub.execute_input":"2026-06-16T04:20:38.910403Z","iopub.status.idle":"2026-06-16T04:20:38.913893Z","shell.execute_reply.started":"2026-06-16T04:20:38.910386Z","shell.execute_reply":"2026-06-16T04:20:38.913185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"COL_NOTES = {\n    \"id\":           \"Ad identifier (unique per row)\",\n    \"click\":        \"Target: 1 = clicked, 0 = not clicked\",\n    \"hour\":         \"YYMMDDHH format — encodes date AND hour\",\n    \"C1\":           \"Anonymized categorical\",\n    \"banner_pos\":   \"Position of the banner on the page\",\n    \"site_id\":      \"Site identifier\",\n    \"site_domain\":  \"Domain the site belongs to\",\n    \"site_category\":\"Category of the site\",\n    \"app_id\":       \"App identifier\",\n    \"app_domain\":   \"Domain of the app\",\n    \"app_category\": \"Category of the app\",\n    \"device_id\":    \"Device identifier (very high cardinality)\",\n    \"device_ip\":    \"IP address (very high cardinality)\",\n    \"device_model\": \"Device model\",\n    \"device_type\":  \"Type of device (phone/tablet/…)\",\n    \"device_conn_type\": \"Connection type (wifi/3g/…)\",\n    \"C14–C21\":      \"Anonymized categoricals\",\n}\n\nfor k,v in COL_NOTES.items():\n    print(f\"{k:20s}:   {v}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:20:38.914756Z","iopub.execute_input":"2026-06-16T04:20:38.914990Z","iopub.status.idle":"2026-06-16T04:20:38.934388Z","shell.execute_reply.started":"2026-06-16T04:20:38.914972Z","shell.execute_reply":"2026-06-16T04:20:38.933684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with gzip.open(TRAIN_PATH,'rb') as f:\n    total_rows = sum(1 for _ in f)\n\nprint(\"Total number of rows:\", total_rows)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:20:38.935401Z","iopub.execute_input":"2026-06-16T04:20:38.935655Z","iopub.status.idle":"2026-06-16T04:21:11.323077Z","shell.execute_reply.started":"2026-06-16T04:20:38.935639Z","shell.execute_reply":"2026-06-16T04:21:11.322057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"skip_prob = 1-(SAMPLE_SIZE/total_rows)\nskip_rows = np.random.RandomState(RANDOM_SEED).choice(\n    range(1,total_rows+1),\n    size = (total_rows-SAMPLE_SIZE),\n    replace=False\n)\nprint(skip_prob)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:21:11.324241Z","iopub.execute_input":"2026-06-16T04:21:11.324537Z","iopub.status.idle":"2026-06-16T04:21:16.362962Z","shell.execute_reply.started":"2026-06-16T04:21:11.324454Z","shell.execute_reply":"2026-06-16T04:21:16.362095Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(TRAIN_PATH,skiprows=skip_rows)\nprint(f\"Loaded sample of rows {df.shape[0]} and columns {df.shape[1]}\")\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:21:16.363799Z","iopub.execute_input":"2026-06-16T04:21:16.364041Z","iopub.status.idle":"2026-06-16T04:22:30.925610Z","shell.execute_reply.started":"2026-06-16T04:21:16.364019Z","shell.execute_reply":"2026-06-16T04:22:30.924639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['hour_str'] = df['hour'].astype(str)\ndf['date'] = pd.to_datetime(df['hour_str'], format=\"%y%m%d%H\")\ndf['hour_of_day'] = df['date'].dt.hour\ndf['day_of_week'] = df['date'].dt.dayofweek\ndf['date_only'] = df['date'].dt.date\ndf['day'] = df['date'].dt.day","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:22:42.962549Z","iopub.execute_input":"2026-06-16T04:22:42.962831Z","iopub.status.idle":"2026-06-16T04:22:45.467926Z","shell.execute_reply.started":"2026-06-16T04:22:42.962810Z","shell.execute_reply":"2026-06-16T04:22:45.467231Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = df[df['day']<=29]\nval = df[df['day']==30]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:25:39.802419Z","iopub.execute_input":"2026-06-16T04:25:39.802708Z","iopub.status.idle":"2026-06-16T04:25:40.614003Z","shell.execute_reply.started":"2026-06-16T04:25:39.802688Z","shell.execute_reply":"2026-06-16T04:25:40.613173Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = train['click'].values\ny_val = val['click'].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:25:40.922750Z","iopub.execute_input":"2026-06-16T04:25:40.922982Z","iopub.status.idle":"2026-06-16T04:25:40.929289Z","shell.execute_reply.started":"2026-06-16T04:25:40.922964Z","shell.execute_reply":"2026-06-16T04:25:40.928583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BASE_FEATURES = [\n    \"C1\", \"banner_pos\", \"site_category\", \"app_category\",\n    \"device_type\", \"device_conn_type\", \"C15\", \"C16\", \"C18\",\n    \"hour_of_day\", \"day_of_week\"\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:22:47.843188Z","iopub.execute_input":"2026-06-16T04:22:47.843419Z","iopub.status.idle":"2026-06-16T04:22:47.848108Z","shell.execute_reply.started":"2026-06-16T04:22:47.843402Z","shell.execute_reply":"2026-06-16T04:22:47.847368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"CAT_COLS = [\"C1\", \"banner_pos\", \"site_category\", \"app_category\",\n            \"device_type\", \"device_conn_type\", \"C15\", \"C16\", \"C18\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:26:16.032522Z","iopub.execute_input":"2026-06-16T04:26:16.032799Z","iopub.status.idle":"2026-06-16T04:26:16.037114Z","shell.execute_reply.started":"2026-06-16T04:26:16.032777Z","shell.execute_reply":"2026-06-16T04:26:16.036450Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def to_cat(df, cols):\n    df = df.copy()\n    for c in cols:\n        df[c] = df[c].astype(\"category\")\n    return df\n \nX_train_13 = to_cat(train[BASE_FEATURES], CAT_COLS)\nX_val_13   = to_cat(val[BASE_FEATURES],   CAT_COLS)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-16T04:26:17.723068Z","iopub.execute_input":"2026-06-16T04:26:17.723329Z","iopub.status.idle":"2026-06-16T04:26:18.530537Z","shell.execute_reply.started":"2026-06-16T04:26:17.723298Z","shell.execute_reply":"2026-06-16T04:26:18.529796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}