{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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","execution":{"iopub.status.busy":"2023-03-02T21:03:46.879177Z","iopub.execute_input":"2023-03-02T21:03:46.879465Z","iopub.status.idle":"2023-03-02T21:03:46.908725Z","shell.execute_reply.started":"2023-03-02T21:03:46.879428Z","shell.execute_reply":"2023-03-02T21:03:46.907626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-03-02T21:03:50.325819Z","iopub.execute_input":"2023-03-02T21:03:50.326969Z","iopub.status.idle":"2023-03-02T21:03:51.260213Z","shell.execute_reply.started":"2023-03-02T21:03:50.326916Z","shell.execute_reply":"2023-03-02T21:03:51.258822Z"},"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-03-02T21:03:52.392839Z","iopub.execute_input":"2023-03-02T21:03:52.393202Z","iopub.status.idle":"2023-03-02T21:06:11.529656Z","shell.execute_reply.started":"2023-03-02T21:03:52.393167Z","shell.execute_reply":"2023-03-02T21:06:11.528433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape\ntrain1=train","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:06:11.532721Z","iopub.execute_input":"2023-03-02T21:06:11.533178Z","iopub.status.idle":"2023-03-02T21:06:11.538077Z","shell.execute_reply.started":"2023-03-02T21:06:11.533147Z","shell.execute_reply":"2023-03-02T21:06:11.536922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:06:11.539341Z","iopub.execute_input":"2023-03-02T21:06:11.540209Z","iopub.status.idle":"2023-03-02T21:06:11.572761Z","shell.execute_reply.started":"2023-03-02T21:06:11.540167Z","shell.execute_reply":"2023-03-02T21:06:11.571687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(['id'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:06:11.576226Z","iopub.execute_input":"2023-03-02T21:06:11.577109Z","iopub.status.idle":"2023-03-02T21:06:11.667988Z","shell.execute_reply.started":"2023-03-02T21:06:11.577072Z","shell.execute_reply":"2023-03-02T21:06:11.666852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:06:11.669498Z","iopub.execute_input":"2023-03-02T21:06:11.669831Z","iopub.status.idle":"2023-03-02T21:06:11.938820Z","shell.execute_reply.started":"2023-03-02T21:06:11.669796Z","shell.execute_reply":"2023-03-02T21:06:11.937700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:06:11.940089Z","iopub.execute_input":"2023-03-02T21:06:11.940367Z","iopub.status.idle":"2023-03-02T21:06:12.184548Z","shell.execute_reply.started":"2023-03-02T21:06:11.940339Z","shell.execute_reply":"2023-03-02T21:06:12.182480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['click'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:06:12.187850Z","iopub.execute_input":"2023-03-02T21:06:12.188164Z","iopub.status.idle":"2023-03-02T21:06:12.198997Z","shell.execute_reply.started":"2023-03-02T21:06:12.188135Z","shell.execute_reply":"2023-03-02T21:06:12.197837Z"},"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-03-02T21:06:12.200388Z","iopub.execute_input":"2023-03-02T21:06:12.201601Z","iopub.status.idle":"2023-03-02T21:06:12.246575Z","shell.execute_reply.started":"2023-03-02T21:06:12.201562Z","shell.execute_reply":"2023-03-02T21:06:12.245480Z"},"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-03-02T21:06:12.248165Z","iopub.execute_input":"2023-03-02T21:06:12.248719Z","iopub.status.idle":"2023-03-02T21:06:12.276309Z","shell.execute_reply.started":"2023-03-02T21:06:12.248552Z","shell.execute_reply":"2023-03-02T21:06:12.275298Z"},"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-03-02T21:06:12.280846Z","iopub.execute_input":"2023-03-02T21:06:12.281150Z","iopub.status.idle":"2023-03-02T21:06:12.355485Z","shell.execute_reply.started":"2023-03-02T21:06:12.281122Z","shell.execute_reply":"2023-03-02T21:06:12.354462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:06:12.356939Z","iopub.execute_input":"2023-03-02T21:06:12.357305Z","iopub.status.idle":"2023-03-02T21:06:12.562457Z","shell.execute_reply.started":"2023-03-02T21:06:12.357266Z","shell.execute_reply":"2023-03-02T21:06:12.561428Z"},"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-03-02T21:06:12.563898Z","iopub.execute_input":"2023-03-02T21:06:12.564540Z","iopub.status.idle":"2023-03-02T21:06:12.821073Z","shell.execute_reply.started":"2023-03-02T21:06:12.564501Z","shell.execute_reply":"2023-03-02T21:06:12.820053Z"},"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-03-02T21:06:12.822493Z","iopub.execute_input":"2023-03-02T21:06:12.823586Z","iopub.status.idle":"2023-03-02T21:06:13.525064Z","shell.execute_reply.started":"2023-03-02T21:06:12.823547Z","shell.execute_reply":"2023-03-02T21:06:13.523539Z"},"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-03-02T21:06:13.526312Z","iopub.execute_input":"2023-03-02T21:06:13.526774Z","iopub.status.idle":"2023-03-02T21:06:13.665127Z","shell.execute_reply.started":"2023-03-02T21:06:13.526730Z","shell.execute_reply":"2023-03-02T21:06:13.664134Z"},"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-03-02T21:06:13.666480Z","iopub.execute_input":"2023-03-02T21:06:13.667107Z","iopub.status.idle":"2023-03-02T21:06:13.689672Z","shell.execute_reply.started":"2023-03-02T21:06:13.667065Z","shell.execute_reply":"2023-03-02T21:06:13.688606Z"},"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-03-02T21:06:13.692364Z","iopub.execute_input":"2023-03-02T21:06:13.693536Z","iopub.status.idle":"2023-03-02T21:06:14.081000Z","shell.execute_reply.started":"2023-03-02T21:06:13.693497Z","shell.execute_reply":"2023-03-02T21:06:14.079735Z"},"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-03-02T21:06:14.082503Z","iopub.execute_input":"2023-03-02T21:06:14.082961Z","iopub.status.idle":"2023-03-02T21:06:14.392279Z","shell.execute_reply.started":"2023-03-02T21:06:14.082912Z","shell.execute_reply":"2023-03-02T21:06:14.391212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from catboost import CatBoostClassifier","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:08:34.352360Z","iopub.execute_input":"2023-03-02T21:08:34.352961Z","iopub.status.idle":"2023-03-02T21:08:34.358003Z","shell.execute_reply.started":"2023-03-02T21:08:34.352912Z","shell.execute_reply":"2023-03-02T21:08:34.356576Z"},"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-03-02T21:08:43.831002Z","iopub.execute_input":"2023-03-02T21:08:43.831361Z","iopub.status.idle":"2023-03-02T21:08:44.124736Z","shell.execute_reply.started":"2023-03-02T21:08:43.831329Z","shell.execute_reply":"2023-03-02T21:08:44.123450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = CatBoostClassifier(\n    iterations=50,\n    learning_rate=0.5,\n    task_type='GPU',\n    loss_function='Logloss',\n    gpu_ram_part=0.9,\n    boosting_type='Plain',\n    max_ctr_complexity=2,\n    depth=6,\n    gpu_cat_features_storage='CpuPinnedMemory',\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:14:38.338137Z","iopub.execute_input":"2023-03-02T21:14:38.338841Z","iopub.status.idle":"2023-03-02T21:14:38.344233Z","shell.execute_reply.started":"2023-03-02T21:14:38.338805Z","shell.execute_reply":"2023-03-02T21:14:38.342932Z"},"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    cat_features=cat_features,\n    verbose=10,\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:14:39.668397Z","iopub.execute_input":"2023-03-02T21:14:39.669092Z","iopub.status.idle":"2023-03-02T21:14:47.540679Z","shell.execute_reply.started":"2023-03-02T21:14:39.669057Z","shell.execute_reply":"2023-03-02T21:14:47.539810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.get_feature_importance(prettified=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:10:55.730150Z","iopub.execute_input":"2023-03-02T21:10:55.732191Z","iopub.status.idle":"2023-03-02T21:10:55.753240Z","shell.execute_reply.started":"2023-03-02T21:10:55.732153Z","shell.execute_reply":"2023-03-02T21:10:55.752464Z"},"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-03-02T21:10:55.756983Z","iopub.execute_input":"2023-03-02T21:10:55.759011Z","iopub.status.idle":"2023-03-02T21:11:12.657654Z","shell.execute_reply.started":"2023-03-02T21:10:55.758977Z","shell.execute_reply":"2023-03-02T21:11:12.656593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:11:12.660557Z","iopub.execute_input":"2023-03-02T21:11:12.661030Z","iopub.status.idle":"2023-03-02T21:11:12.667810Z","shell.execute_reply.started":"2023-03-02T21:11:12.660989Z","shell.execute_reply":"2023-03-02T21:11:12.666692Z"},"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-03-02T21:11:12.669409Z","iopub.execute_input":"2023-03-02T21:11:12.670072Z","iopub.status.idle":"2023-03-02T21:11:15.393527Z","shell.execute_reply.started":"2023-03-02T21:11:12.670036Z","shell.execute_reply":"2023-03-02T21:11:15.392449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:11:15.395038Z","iopub.execute_input":"2023-03-02T21:11:15.395615Z","iopub.status.idle":"2023-03-02T21:11:20.681556Z","shell.execute_reply.started":"2023-03-02T21:11:15.395578Z","shell.execute_reply":"2023-03-02T21:11:20.680485Z"},"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-03-02T21:15:55.143295Z","iopub.execute_input":"2023-03-02T21:15:55.144367Z","iopub.status.idle":"2023-03-02T21:15:55.149525Z","shell.execute_reply.started":"2023-03-02T21:15:55.144324Z","shell.execute_reply":"2023-03-02T21:15:55.148292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_test = test_df.iloc[:, 0:1]\n","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:16:03.414599Z","iopub.execute_input":"2023-03-02T21:16:03.415545Z","iopub.status.idle":"2023-03-02T21:16:03.470891Z","shell.execute_reply.started":"2023-03-02T21:16:03.415491Z","shell.execute_reply":"2023-03-02T21:16:03.469855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_test.join(pd.DataFrame(y_test)) ","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:16:16.084866Z","iopub.execute_input":"2023-03-02T21:16:16.085223Z","iopub.status.idle":"2023-03-02T21:16:16.089638Z","shell.execute_reply.started":"2023-03-02T21:16:16.085191Z","shell.execute_reply":"2023-03-02T21:16:16.088457Z"},"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-03-02T21:11:36.559139Z","iopub.status.idle":"2023-03-02T21:11:36.559989Z","shell.execute_reply.started":"2023-03-02T21:11:36.559728Z","shell.execute_reply":"2023-03-02T21:11:36.559754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport time\nimport gzip\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom tqdm import tqdm, trange\nfrom matplotlib import pyplot as plt\nfrom IPython.display import clear_output\n\nfrom xgboost import XGBClassifier, DMatrix\n    \nfrom bayes_opt import BayesianOptimization\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score\nfrom sklearn.model_selection import train_test_split, GridSearchCV, StratifiedKFold, KFold\n\npd.set_option('display.max_columns', None)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:07:09.489214Z","iopub.status.idle":"2023-03-02T21:07:09.490114Z","shell.execute_reply.started":"2023-03-02T21:07:09.489846Z","shell.execute_reply":"2023-03-02T21:07:09.489871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/submissioncsv/submission (1).csv')\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T21:07:09.491722Z","iopub.status.idle":"2023-03-02T21:07:09.492276Z","shell.execute_reply.started":"2023-03-02T21:07:09.492023Z","shell.execute_reply":"2023-03-02T21:07:09.492047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}