{"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":"markdown","source":"Kaggle: https://www.kaggle.com/competitions/avazu-ctr-prediction","metadata":{}},{"cell_type":"code","source":"# Imports\nimport pandas as pd\nimport category_encoders as ce\n\n# Viz\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Sklearna\nfrom sklearn import metrics\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\n","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:39:27.113133Z","iopub.execute_input":"2023-05-17T18:39:27.113541Z","iopub.status.idle":"2023-05-17T18:39:29.898182Z","shell.execute_reply.started":"2023-05-17T18:39:27.113506Z","shell.execute_reply":"2023-05-17T18:39:29.897246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = pd.read_csv('/kaggle/input/avazu-ctr-prediction/train.gz', compression='gzip', header=0, sep=',', quotechar='\"')","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:39:29.900093Z","iopub.execute_input":"2023-05-17T18:39:29.901000Z","iopub.status.idle":"2023-05-17T18:43:15.448873Z","shell.execute_reply.started":"2023-05-17T18:39:29.900968Z","shell.execute_reply":"2023-05-17T18:43:15.447530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:43:15.455517Z","iopub.execute_input":"2023-05-17T18:43:15.455997Z","iopub.status.idle":"2023-05-17T18:43:15.513989Z","shell.execute_reply.started":"2023-05-17T18:43:15.455955Z","shell.execute_reply":"2023-05-17T18:43:15.512731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:43:15.515654Z","iopub.execute_input":"2023-05-17T18:43:15.516136Z","iopub.status.idle":"2023-05-17T18:43:15.524947Z","shell.execute_reply.started":"2023-05-17T18:43:15.516094Z","shell.execute_reply":"2023-05-17T18:43:15.523562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EDA","metadata":{}},{"cell_type":"code","source":"train_dataset.describe(include= 'all')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:44:45.937607Z","iopub.execute_input":"2023-05-17T18:44:45.938056Z","iopub.status.idle":"2023-05-17T18:44:45.967802Z","shell.execute_reply.started":"2023-05-17T18:44:45.938019Z","shell.execute_reply":"2023-05-17T18:44:45.966627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:44:48.385103Z","iopub.execute_input":"2023-05-17T18:44:48.385562Z","iopub.status.idle":"2023-05-17T18:46:43.589202Z","shell.execute_reply.started":"2023-05-17T18:44:48.385527Z","shell.execute_reply":"2023-05-17T18:46:43.588094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_dataset.click.value_counts(normalize = True))\nprint(\"\\n\")\nplt.figure()\nsns.countplot(x='click', data=train_dataset)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:47:27.909853Z","iopub.execute_input":"2023-05-17T18:47:27.910287Z","iopub.status.idle":"2023-05-17T18:47:31.406116Z","shell.execute_reply.started":"2023-05-17T18:47:27.910246Z","shell.execute_reply":"2023-05-17T18:47:31.404953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pearson correlation table to find the relationship with output with all input features. \n\ncorr = train_dataset.corr()\nf, ax = plt.subplots(figsize=(10,10))\nsns.heatmap(corr, ax=ax, annot=True,linewidths=3,cmap='YlGn')\nplt.title(\"Pearson correlation of Features\", y=1.05, size=15)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:48:16.277793Z","iopub.execute_input":"2023-05-17T18:48:16.278454Z","iopub.status.idle":"2023-05-17T18:48:46.256054Z","shell.execute_reply.started":"2023-05-17T18:48:16.278397Z","shell.execute_reply":"2023-05-17T18:48:46.255014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking missing data\ntotal = train_dataset.isnull().sum().sort_values(ascending = False)\npercent = (train_dataset.isnull().sum()/train_dataset.isnull().count()*100).sort_values(ascending = False)\nmissing__train_data  = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])\nmissing__train_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:49:05.814409Z","iopub.execute_input":"2023-05-17T18:49:05.814937Z","iopub.status.idle":"2023-05-17T18:54:50.994641Z","shell.execute_reply.started":"2023-05-17T18:49:05.814893Z","shell.execute_reply":"2023-05-17T18:54:50.993174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.columns","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:54:50.996932Z","iopub.execute_input":"2023-05-17T18:54:50.997408Z","iopub.status.idle":"2023-05-17T18:54:51.005370Z","shell.execute_reply.started":"2023-05-17T18:54:50.997377Z","shell.execute_reply":"2023-05-17T18:54:51.004188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:54:51.006761Z","iopub.execute_input":"2023-05-17T18:54:51.007465Z","iopub.status.idle":"2023-05-17T18:54:51.037949Z","shell.execute_reply.started":"2023-05-17T18:54:51.007414Z","shell.execute_reply":"2023-05-17T18:54:51.036820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Dropping few categorical columns\ntrain_dataset.drop(['C18','C20'], axis=1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:54:51.040408Z","iopub.execute_input":"2023-05-17T18:54:51.040734Z","iopub.status.idle":"2023-05-17T18:54:58.346674Z","shell.execute_reply.started":"2023-05-17T18:54:51.040707Z","shell.execute_reply":"2023-05-17T18:54:58.345441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset.rename(columns={'click': 'y'},inplace=True, errors='raise')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Features and Label & split dataset","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_dataset.drop(['click'], axis=1)\ny = train_dataset['click']","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:54:58.348504Z","iopub.execute_input":"2023-05-17T18:54:58.348973Z","iopub.status.idle":"2023-05-17T18:55:04.362914Z","shell.execute_reply.started":"2023-05-17T18:54:58.348931Z","shell.execute_reply":"2023-05-17T18:55:04.361576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T18:55:04.397507Z","iopub.execute_input":"2023-05-17T18:55:04.398213Z","iopub.status.idle":"2023-05-17T18:55:04.426658Z","shell.execute_reply.started":"2023-05-17T18:55:04.398180Z","shell.execute_reply":"2023-05-17T18:55:04.425426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_dataset.click.value_counts(normalize = True))\nprint(\"\\n\")\nplt.figure()\nsns.countplot(x=y)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T19:01:32.379344Z","iopub.execute_input":"2023-05-17T19:01:32.379817Z","iopub.status.idle":"2023-05-17T19:01:36.269285Z","shell.execute_reply.started":"2023-05-17T19:01:32.379783Z","shell.execute_reply":"2023-05-17T19:01:36.268163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Preparing dataset for model training","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_encoder = ce.TargetEncoder()\nX = target_encoder.fit_transform(X, y)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T19:01:38.445262Z","iopub.execute_input":"2023-05-17T19:01:38.445723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, test_size= 0.3, random_state= 42)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Simple regression model","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = MinMaxScaler()\nX_train[X_train.columns] = scaler.fit_transform(X_train[X_train.columns])\nX_train.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LogisticRegression()\nmodel.fit(X_train, y_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test[X_test.columns] = scaler.transform(X_test[X_test.columns])\nX_test.head(2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics.accuracy_score(y_test, y_pred)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}