{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":4120,"databundleVersionId":44256,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport xgboost as xgb\nimport catboost as cb\nfrom sklearn.model_selection import train_test_split, GridSearchCV\nfrom sklearn.preprocessing import OneHotEncoder, LabelEncoder\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.ensemble import RandomForestClassifier\n","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:07.244509Z","iopub.execute_input":"2024-01-28T10:45:07.244854Z","iopub.status.idle":"2024-01-28T10:45:09.559605Z","shell.execute_reply.started":"2024-01-28T10:45:07.244824Z","shell.execute_reply":"2024-01-28T10:45:09.558551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data(nrows=100_000):\n    chunksize = 10 ** 6\n    filename=\"../input/avazu-ctr-prediction/train.gz\"\n    chunks = []\n    with pd.read_csv(filename, chunksize=chunksize, compression='gzip', nrows=nrows) as reader:\n        for chunk in reader:\n            chunks.append(chunk)\n    data = pd.concat(chunks)\n\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:09.561104Z","iopub.execute_input":"2024-01-28T10:45:09.562280Z","iopub.status.idle":"2024-01-28T10:45:09.570765Z","shell.execute_reply.started":"2024-01-28T10:45:09.562239Z","shell.execute_reply":"2024-01-28T10:45:09.568745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def find_best_estimator(params, clf, X, Y, verbose=0):\n    grid_search = GridSearchCV(clf, params, n_jobs=-1, cv=3, scoring='roc_auc')\n    if verbose:\n        grid_search.fit(X, Y, verbose=100)\n    else:\n        grid_search.fit(X, Y)\n    print(grid_search.best_params_)\n    return grid_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:09.572549Z","iopub.execute_input":"2024-01-28T10:45:09.572970Z","iopub.status.idle":"2024-01-28T10:45:09.586813Z","shell.execute_reply.started":"2024-01-28T10:45:09.572935Z","shell.execute_reply":"2024-01-28T10:45:09.585729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nrows_data = 300_000\ndata = load_data(nrows=nrows_data)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:09.588765Z","iopub.execute_input":"2024-01-28T10:45:09.589246Z","iopub.status.idle":"2024-01-28T10:45:10.529492Z","shell.execute_reply.started":"2024-01-28T10:45:09.589217Z","shell.execute_reply":"2024-01-28T10:45:10.528570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()\ndata.info()\ndata.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:10.530797Z","iopub.execute_input":"2024-01-28T10:45:10.531391Z","iopub.status.idle":"2024-01-28T10:45:10.647933Z","shell.execute_reply.started":"2024-01-28T10:45:10.531362Z","shell.execute_reply":"2024-01-28T10:45:10.647083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = data[\"click\"]","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:10.649475Z","iopub.execute_input":"2024-01-28T10:45:10.650101Z","iopub.status.idle":"2024-01-28T10:45:10.654476Z","shell.execute_reply.started":"2024-01-28T10:45:10.650067Z","shell.execute_reply":"2024-01-28T10:45:10.653675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Clicking rate is: {round(Y.sum()/Y.shape[0]*100, 2)}%\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:10.655942Z","iopub.execute_input":"2024-01-28T10:45:10.656579Z","iopub.status.idle":"2024-01-28T10:45:10.664954Z","shell.execute_reply.started":"2024-01-28T10:45:10.656551Z","shell.execute_reply":"2024-01-28T10:45:10.664015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.drop(columns=['id', 'click', 'hour', 'device_id', 'device_ip'])","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:10.667139Z","iopub.execute_input":"2024-01-28T10:45:10.668716Z","iopub.status.idle":"2024-01-28T10:45:10.697308Z","shell.execute_reply.started":"2024-01-28T10:45:10.668687Z","shell.execute_reply":"2024-01-28T10:45:10.696501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:10.698505Z","iopub.execute_input":"2024-01-28T10:45:10.698784Z","iopub.status.idle":"2024-01-28T10:45:10.844470Z","shell.execute_reply.started":"2024-01-28T10:45:10.698759Z","shell.execute_reply":"2024-01-28T10:45:10.843236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enc = OneHotEncoder(handle_unknown='ignore')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:10.850806Z","iopub.execute_input":"2024-01-28T10:45:10.851150Z","iopub.status.idle":"2024-01-28T10:45:10.855189Z","shell.execute_reply.started":"2024-01-28T10:45:10.851122Z","shell.execute_reply":"2024-01-28T10:45:10.854366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_enc = enc.fit_transform(X_train)\nX_test_enc = enc.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:10.856210Z","iopub.execute_input":"2024-01-28T10:45:10.856719Z","iopub.status.idle":"2024-01-28T10:45:11.590068Z","shell.execute_reply.started":"2024-01-28T10:45:10.856689Z","shell.execute_reply":"2024-01-28T10:45:11.588989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train_enc[0].shape)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:11.591389Z","iopub.execute_input":"2024-01-28T10:45:11.591702Z","iopub.status.idle":"2024-01-28T10:45:11.601651Z","shell.execute_reply.started":"2024-01-28T10:45:11.591675Z","shell.execute_reply":"2024-01-28T10:45:11.599886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DT_parameters = {'max_depth': [5, 10, 20],\n                 'max_features': [\"sqrt\", \"log2\", 1.0]}","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:11.603148Z","iopub.execute_input":"2024-01-28T10:45:11.603577Z","iopub.status.idle":"2024-01-28T10:45:11.608346Z","shell.execute_reply.started":"2024-01-28T10:45:11.603549Z","shell.execute_reply":"2024-01-28T10:45:11.607533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decision_tree = DecisionTreeClassifier(criterion='gini', min_samples_split=30)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:11.609643Z","iopub.execute_input":"2024-01-28T10:45:11.610110Z","iopub.status.idle":"2024-01-28T10:45:11.619230Z","shell.execute_reply.started":"2024-01-28T10:45:11.610082Z","shell.execute_reply":"2024-01-28T10:45:11.617987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decision_tree_best = find_best_estimator(DT_parameters, decision_tree, X_train_enc, Y_train)\npos_prob = decision_tree_best.predict_proba(X_test_enc)[:, 1]\nprint(f\"Pole pod krzywą ROC dla zbioru testowego wynosi: {roc_auc_score(Y_test, pos_prob)}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:11.620443Z","iopub.execute_input":"2024-01-28T10:45:11.620721Z","iopub.status.idle":"2024-01-28T10:45:33.150192Z","shell.execute_reply.started":"2024-01-28T10:45:11.620700Z","shell.execute_reply":"2024-01-28T10:45:33.149099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_parameters = {'max_depth': [5, 10, 20],\n                 'n_estimators': [50, 100, 200]}","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:33.151478Z","iopub.execute_input":"2024-01-28T10:45:33.151750Z","iopub.status.idle":"2024-01-28T10:45:33.155641Z","shell.execute_reply.started":"2024-01-28T10:45:33.151726Z","shell.execute_reply":"2024-01-28T10:45:33.154923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_forest = RandomForestClassifier(n_estimators=100, criterion='gini', min_samples_split=30, n_jobs=-1)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:33.156381Z","iopub.execute_input":"2024-01-28T10:45:33.156630Z","iopub.status.idle":"2024-01-28T10:45:33.175860Z","shell.execute_reply.started":"2024-01-28T10:45:33.156609Z","shell.execute_reply":"2024-01-28T10:45:33.174432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_forest_best = find_best_estimator(rf_parameters, random_forest, X_train_enc, Y_train)\npos_prob = random_forest_best.predict_proba(X_test_enc)[:, 1]\nprint(f\"Pole pod krzywą ROC dla zbioru testowego wynosi: {roc_auc_score(Y_test, pos_prob)}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:45:33.177417Z","iopub.execute_input":"2024-01-28T10:45:33.177850Z","iopub.status.idle":"2024-01-28T10:49:56.329133Z","shell.execute_reply.started":"2024-01-28T10:45:33.177813Z","shell.execute_reply":"2024-01-28T10:49:56.327826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_parameters = {'max_depth': [5, 10, 20],\n                 'learning_rate': [0.01, 0.1, 0.5]}","metadata":{"execution":{"iopub.status.busy":"2024-01-28T12:56:02.592222Z","iopub.status.idle":"2024-01-28T12:56:02.592815Z","shell.execute_reply.started":"2024-01-28T12:56:02.592561Z","shell.execute_reply":"2024-01-28T12:56:02.592585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_model = xgb.XGBClassifier(learning_rate=0.1, max_depth=10, n_estimators=100)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T12:56:02.969123Z","iopub.execute_input":"2024-01-28T12:56:02.969525Z","iopub.status.idle":"2024-01-28T12:56:02.975388Z","shell.execute_reply.started":"2024-01-28T12:56:02.969496Z","shell.execute_reply":"2024-01-28T12:56:02.974197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_best = find_best_estimator(xgb_parameters, xgb_model, X_train_enc, Y_train)\npos_prob = xgb_best.predict_proba(X_test_enc)[:, 1]\nprint(f\"Pole pod krzywą ROC dla zbioru testowego wynosi: {roc_auc_score(Y_test, pos_prob)}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T12:56:04.938652Z","iopub.execute_input":"2024-01-28T12:56:04.939033Z","iopub.status.idle":"2024-01-28T13:02:57.408228Z","shell.execute_reply.started":"2024-01-28T12:56:04.939002Z","shell.execute_reply":"2024-01-28T13:02:57.407101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cb_parameters = {\"depth\": [5, 10, 20],\n            \"learning_rate\": [0.01, 0.1, 0.5],\n            \"iterations\": [100]}","metadata":{"execution":{"iopub.status.busy":"2024-01-28T13:02:57.410407Z","iopub.execute_input":"2024-01-28T13:02:57.410757Z","iopub.status.idle":"2024-01-28T13:02:57.415965Z","shell.execute_reply.started":"2024-01-28T13:02:57.410726Z","shell.execute_reply":"2024-01-28T13:02:57.414773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cb_model = cb.CatBoostClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T13:02:57.416886Z","iopub.execute_input":"2024-01-28T13:02:57.417149Z","iopub.status.idle":"2024-01-28T13:02:57.426712Z","shell.execute_reply.started":"2024-01-28T13:02:57.417127Z","shell.execute_reply":"2024-01-28T13:02:57.424866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cb_best = find_best_estimator(cb_parameters, cb_model, X_train_enc, Y_train, 100)\npos_prob = cb_best.predict_proba(X_test_enc)[:, 1]\nprint(f\"Pole pod krzywą ROC dla zbioru testowego wynosi: {roc_auc_score(Y_test, pos_prob)}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T13:02:57.428440Z","iopub.execute_input":"2024-01-28T13:02:57.428791Z","iopub.status.idle":"2024-01-28T13:06:32.428773Z","shell.execute_reply.started":"2024-01-28T13:02:57.428764Z","shell.execute_reply":"2024-01-28T13:06:32.427633Z"},"trusted":true},"execution_count":null,"outputs":[]}]}