{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[],"dockerImageVersionId":30407,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%matplotlib inline\n%config InlineBackend.figure_format = 'svg'\n\nimport warnings\nimport gc\nimport time\nimport itertools\nwarnings.filterwarnings('ignore')\nimport numpy as np\nimport pandas as pd\nfrom catboost import CatBoostClassifier\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import confusion_matrix, log_loss, roc_auc_score\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nimport gzip","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:03:05.603339Z","iopub.execute_input":"2023-11-15T21:03:05.604042Z","iopub.status.idle":"2023-11-15T21:03:07.030007Z","shell.execute_reply.started":"2023-11-15T21:03:05.604009Z","shell.execute_reply":"2023-11-15T21:03:07.028859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chunksize = 10 ** 7\nnum_of_chunk = 0\ntrain = pd.DataFrame()\n\nfor chunk in pd.read_csv('/kaggle/input/avazu-ctr-prediction/train.gz',chunksize=chunksize):\n    num_of_chunk = num_of_chunk + 1\n    train = pd.concat([train,chunk.sample(frac=0.01)],axis = 0,ignore_index = True)\n    print(f'NUMBER {str(num_of_chunk)} is done.')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:03:07.032135Z","iopub.execute_input":"2023-11-15T21:03:07.034362Z","iopub.status.idle":"2023-11-15T21:05:42.427293Z","shell.execute_reply.started":"2023-11-15T21:03:07.034326Z","shell.execute_reply":"2023-11-15T21:05:42.426103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:42.428845Z","iopub.execute_input":"2023-11-15T21:05:42.429296Z","iopub.status.idle":"2023-11-15T21:05:42.437822Z","shell.execute_reply.started":"2023-11-15T21:05:42.429219Z","shell.execute_reply":"2023-11-15T21:05:42.436627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:42.440925Z","iopub.execute_input":"2023-11-15T21:05:42.441385Z","iopub.status.idle":"2023-11-15T21:05:42.470001Z","shell.execute_reply.started":"2023-11-15T21:05:42.441327Z","shell.execute_reply":"2023-11-15T21:05:42.468948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(['id'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:42.471190Z","iopub.execute_input":"2023-11-15T21:05:42.471589Z","iopub.status.idle":"2023-11-15T21:05:42.561587Z","shell.execute_reply.started":"2023-11-15T21:05:42.471548Z","shell.execute_reply":"2023-11-15T21:05:42.560652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:42.562662Z","iopub.execute_input":"2023-11-15T21:05:42.562961Z","iopub.status.idle":"2023-11-15T21:05:43.008250Z","shell.execute_reply.started":"2023-11-15T21:05:42.562932Z","shell.execute_reply":"2023-11-15T21:05:43.007179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:43.009554Z","iopub.execute_input":"2023-11-15T21:05:43.009877Z","iopub.status.idle":"2023-11-15T21:05:43.255516Z","shell.execute_reply.started":"2023-11-15T21:05:43.009843Z","shell.execute_reply":"2023-11-15T21:05:43.254407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['click'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:43.257024Z","iopub.execute_input":"2023-11-15T21:05:43.257645Z","iopub.status.idle":"2023-11-15T21:05:43.268697Z","shell.execute_reply.started":"2023-11-15T21:05:43.257596Z","shell.execute_reply":"2023-11-15T21:05:43.267720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"click rate : {100*round(sum(train['click']==1)/train.shape[0],2)}%\")","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:43.270161Z","iopub.execute_input":"2023-11-15T21:05:43.270480Z","iopub.status.idle":"2023-11-15T21:05:43.336053Z","shell.execute_reply.started":"2023-11-15T21:05:43.270451Z","shell.execute_reply":"2023-11-15T21:05:43.335001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['hour'] = pd.to_datetime(train['hour'], format = '%y%m%d%H')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:43.340526Z","iopub.execute_input":"2023-11-15T21:05:43.340859Z","iopub.status.idle":"2023-11-15T21:05:43.367258Z","shell.execute_reply.started":"2023-11-15T21:05:43.340828Z","shell.execute_reply":"2023-11-15T21:05:43.366182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['hour_of_day'] = train['hour'].dt.hour\ntrain['weekday'] = (train['hour'].dt.dayofweek)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:43.368608Z","iopub.execute_input":"2023-11-15T21:05:43.368973Z","iopub.status.idle":"2023-11-15T21:05:43.443521Z","shell.execute_reply.started":"2023-11-15T21:05:43.368941Z","shell.execute_reply":"2023-11-15T21:05:43.442502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:43.444794Z","iopub.execute_input":"2023-11-15T21:05:43.445096Z","iopub.status.idle":"2023-11-15T21:05:43.646139Z","shell.execute_reply.started":"2023-11-15T21:05:43.445067Z","shell.execute_reply":"2023-11-15T21:05:43.645048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby('weekday')['click'].sum().plot.bar(figsize=(6,6))\nplt.ylabel('Number of clicks')\nplt.title('Number of clicks by weekday')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:43.647550Z","iopub.execute_input":"2023-11-15T21:05:43.647861Z","iopub.status.idle":"2023-11-15T21:05:43.931799Z","shell.execute_reply.started":"2023-11-15T21:05:43.647832Z","shell.execute_reply":"2023-11-15T21:05:43.930431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='hour_of_day',hue='click',data=train)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:43.933635Z","iopub.execute_input":"2023-11-15T21:05:43.935734Z","iopub.status.idle":"2023-11-15T21:05:44.693886Z","shell.execute_reply.started":"2023-11-15T21:05:43.935669Z","shell.execute_reply":"2023-11-15T21:05:44.692433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"obj_cols = train.select_dtypes(include = ['object']).columns.tolist()\nobj_cols","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:44.696788Z","iopub.execute_input":"2023-11-15T21:05:44.698080Z","iopub.status.idle":"2023-11-15T21:05:44.833321Z","shell.execute_reply.started":"2023-11-15T21:05:44.698017Z","shell.execute_reply":"2023-11-15T21:05:44.832265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for col in obj_cols: train[col] = train[col].apply(lambda x: hash(x))\ntrain.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:44.834775Z","iopub.execute_input":"2023-11-15T21:05:44.835275Z","iopub.status.idle":"2023-11-15T21:05:44.857675Z","shell.execute_reply.started":"2023-11-15T21:05:44.835229Z","shell.execute_reply":"2023-11-15T21:05:44.856421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=train.drop(['click','hour'],axis=1)\ny=train['click']\nx","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:44.859051Z","iopub.execute_input":"2023-11-15T21:05:44.859348Z","iopub.status.idle":"2023-11-15T21:05:45.250627Z","shell.execute_reply.started":"2023-11-15T21:05:44.859319Z","shell.execute_reply":"2023-11-15T21:05:45.249568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x.drop(obj_cols,axis=1,inplace=True)\nx","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:45.252207Z","iopub.execute_input":"2023-11-15T21:05:45.252682Z","iopub.status.idle":"2023-11-15T21:05:45.569952Z","shell.execute_reply.started":"2023-11-15T21:05:45.252637Z","shell.execute_reply":"2023-11-15T21:05:45.568843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from catboost import CatBoostClassifier","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:45.571364Z","iopub.execute_input":"2023-11-15T21:05:45.571810Z","iopub.status.idle":"2023-11-15T21:05:45.576845Z","shell.execute_reply.started":"2023-11-15T21:05:45.571768Z","shell.execute_reply":"2023-11-15T21:05:45.575844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_features = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,21,22]\nX_train, X_test, y_train, y_test = train_test_split(x, y, test_size = .2, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:45.578530Z","iopub.execute_input":"2023-11-15T21:05:45.578913Z","iopub.status.idle":"2023-11-15T21:05:45.843641Z","shell.execute_reply.started":"2023-11-15T21:05:45.578873Z","shell.execute_reply":"2023-11-15T21:05:45.842515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = CatBoostClassifier(\n    iterations=100,\n    task_type='GPU',\n    loss_function='Logloss',\n    # gpu_ram_part=0.9,\n    gpu_cat_features_storage='CpuPinnedMemory',\n    langevin=True, depth=6, l2_leaf_reg=7, random_strength=3\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:45.844862Z","iopub.execute_input":"2023-11-15T21:05:45.845172Z","iopub.status.idle":"2023-11-15T21:05:45.853139Z","shell.execute_reply.started":"2023-11-15T21:05:45.845142Z","shell.execute_reply":"2023-11-15T21:05:45.852133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n    X_train, y_train,\n    eval_set=(X_test, y_test),\n    use_best_model=True, \n    cat_features=cat_features,\n    verbose=10,\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:05:45.854562Z","iopub.execute_input":"2023-11-15T21:05:45.854911Z","iopub.status.idle":"2023-11-15T21:07:14.977146Z","shell.execute_reply.started":"2023-11-15T21:05:45.854875Z","shell.execute_reply":"2023-11-15T21:07:14.976024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.get_feature_importance(prettified=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:07:14.978787Z","iopub.execute_input":"2023-11-15T21:07:14.979277Z","iopub.status.idle":"2023-11-15T21:07:14.993704Z","shell.execute_reply.started":"2023-11-15T21:07:14.979231Z","shell.execute_reply":"2023-11-15T21:07:14.992735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/avazu-ctr-prediction/test.gz\", header=0, dtype=str)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:07:14.995028Z","iopub.execute_input":"2023-11-15T21:07:14.995409Z","iopub.status.idle":"2023-11-15T21:07:36.056508Z","shell.execute_reply.started":"2023-11-15T21:07:14.995349Z","shell.execute_reply":"2023-11-15T21:07:36.055414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:07:36.057782Z","iopub.execute_input":"2023-11-15T21:07:36.058123Z","iopub.status.idle":"2023-11-15T21:07:36.064975Z","shell.execute_reply.started":"2023-11-15T21:07:36.058090Z","shell.execute_reply":"2023-11-15T21:07:36.064021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['hour'] = pd.to_datetime(train['hour'], format = '%y%m%d%H')\ntest_df['hour_of_day'] = train['hour'].dt.hour\ntest_df['weekday'] = (train['hour'].dt.dayofweek)\ntest=test_df.drop(['id','hour'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:07:36.066150Z","iopub.execute_input":"2023-11-15T21:07:36.066522Z","iopub.status.idle":"2023-11-15T21:07:39.138931Z","shell.execute_reply.started":"2023-11-15T21:07:36.066490Z","shell.execute_reply":"2023-11-15T21:07:39.137844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:07:39.141049Z","iopub.execute_input":"2023-11-15T21:07:39.141402Z","iopub.status.idle":"2023-11-15T21:07:43.778942Z","shell.execute_reply.started":"2023-11-15T21:07:39.141348Z","shell.execute_reply":"2023-11-15T21:07:43.777749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns[22]","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:07:43.785554Z","iopub.execute_input":"2023-11-15T21:07:43.785939Z","iopub.status.idle":"2023-11-15T21:07:43.794653Z","shell.execute_reply.started":"2023-11-15T21:07:43.785905Z","shell.execute_reply":"2023-11-15T21:07:43.793620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['hour_of_day'] = test['hour_of_day'].fillna(-1).astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:07:43.795768Z","iopub.execute_input":"2023-11-15T21:07:43.796050Z","iopub.status.idle":"2023-11-15T21:07:43.849773Z","shell.execute_reply.started":"2023-11-15T21:07:43.796022Z","shell.execute_reply":"2023-11-15T21:07:43.848946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['weekday'] = test['weekday'].fillna(-1).astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:07:43.850924Z","iopub.execute_input":"2023-11-15T21:07:43.851213Z","iopub.status.idle":"2023-11-15T21:07:43.887356Z","shell.execute_reply.started":"2023-11-15T21:07:43.851185Z","shell.execute_reply":"2023-11-15T21:07:43.886357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = model.predict(test, \n                        prediction_type='Probability', \n                        ntree_start=0, ntree_end=model.get_best_iteration(), \n                        thread_count=-1, verbose=None)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:07:43.888600Z","iopub.execute_input":"2023-11-15T21:07:43.888867Z","iopub.status.idle":"2023-11-15T21:08:08.093536Z","shell.execute_reply.started":"2023-11-15T21:07:43.888841Z","shell.execute_reply":"2023-11-15T21:08:08.092435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_test = test_df[['id']]\n","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:08:08.094918Z","iopub.execute_input":"2023-11-15T21:08:08.095207Z","iopub.status.idle":"2023-11-15T21:08:08.150584Z","shell.execute_reply.started":"2023-11-15T21:08:08.095179Z","shell.execute_reply":"2023-11-15T21:08:08.149742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_test","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:08:08.151684Z","iopub.execute_input":"2023-11-15T21:08:08.151973Z","iopub.status.idle":"2023-11-15T21:08:08.164167Z","shell.execute_reply.started":"2023-11-15T21:08:08.151946Z","shell.execute_reply":"2023-11-15T21:08:08.163146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_test.join(pd.DataFrame(y_test)) ","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:08:08.165480Z","iopub.execute_input":"2023-11-15T21:08:08.165953Z","iopub.status.idle":"2023-11-15T21:08:08.246257Z","shell.execute_reply.started":"2023-11-15T21:08:08.165918Z","shell.execute_reply":"2023-11-15T21:08:08.245206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.read_csv(\"/kaggle/input/avazu-ctr-prediction/sampleSubmission.gz\")\nsubmission_df[\"click\"] = y_test[:, 1]\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:08:08.247418Z","iopub.execute_input":"2023-11-15T21:08:08.247708Z","iopub.status.idle":"2023-11-15T21:08:26.859116Z","shell.execute_reply.started":"2023-11-15T21:08:08.247680Z","shell.execute_reply":"2023-11-15T21:08:26.858183Z"},"trusted":true},"execution_count":null,"outputs":[]}]}