<jupyter_start><jupyter_text>Background This project deals with artificial advertising data set, indicating whether or not a particular internet user clicked on an Advertisement. This dataset can be explored to train a model that can predict whether or not the new users will click on an ad based on their various low-level features.This data set contains the following features:* 'Daily Time Spent on Site': consumer time on site in minutes* 'Age': cutomer age in years* 'Area Income': Avg. Income of geographical area of consumer* 'Daily Internet Usage': Avg. minutes a day consumer is on the internet* 'Ad Topic Line': Headline of the advertisement* 'City': City of consumer* 'Male': Whether or not consumer was male* 'Country': Country of consumer* 'Timestamp': Time at which consumer clicked on Ad or closed window* 'Clicked on Ad': 0 or 1 indicated clicking on Ad Dataset overview<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_style('white')
df_ad = pd.read_csv('Data/advertising.csv')
df_ad.head(3)
df_ad.info()
df_ad.isnull().any()
df_ad.describe()<jupyter_output><empty_output><jupyter_text>EDA Age distribution of the dataset<jupyter_code>sns.set_context('notebook',font_scale=1.5)
sns.distplot(df_ad.Age,bins=30,kde=False,color='red')
plt.show()<jupyter_output><empty_output><jupyter_text>pairplot of dataset defined by `Clicked on Ad`<jupyter_code>import warnings
warnings.filterwarnings('ignore') #### since the target variable is numeric, the joint plot by the target variable generates the warning.
sns.pairplot(df_ad,hue='Clicked on Ad')<jupyter_output><empty_output><jupyter_text>Model training: Basic Logistic Regression<jupyter_code>from sklearn.model_selection import train_test_split
X = df_ad[['Daily Time Spent on Site', 'Age', 'Area Income',
       'Daily Internet Usage', 'Male']]
y = df_ad['Clicked on Ad']
X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=100)<jupyter_output><empty_output><jupyter_text>training<jupyter_code>from sklearn.linear_model import LogisticRegression
lr = LogisticRegression().fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>Predictions and Evaluations<jupyter_code>from sklearn.metrics import classification_report,confusion_matrix
y_predict = lr.predict(X_test)
pd.DataFrame(confusion_matrix(y_test,y_predict),index=['True 0','True 1'],
             columns=['Predicted 0','Predicted 1'])
print(classification_report(y_test,y_predict))<jupyter_output>precision    recall  f1-score   support

           0       0.86      0.92      0.89       119
           1       0.93      0.86      0.89       131

   micro avg       0.89      0.89      0.89       250
   macro avg       0.89      0.89      0.89       250
weighted avg       0.89      0.89      0.89       250<jupyter_text>Model training: Optimized Logistic Regression<jupyter_code>from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV

scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
x_test_scaled = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>3-fold CV grid search<jupyter_code>grid_param = {'C':[0.01,0.03,0.1,0.3,1,3,10]}
grid_lr = GridSearchCV(LogisticRegression(),grid_param,cv=3).fit(X_train_scaled,y_train)
print('best regularization parameter: {}'.format(grid_lr.best_params_))
print('best CV score: {}'.format(grid_lr.best_score_.round(3)))<jupyter_output>best regularization parameter: {'C': 0.3}
best CV score: 0.971<jupyter_text>Predictions and Evaluations<jupyter_code>y_predict_2 = grid_lr.predict(x_test_scaled)
pd.DataFrame(confusion_matrix(y_test,y_predict_2),index=['True 0','True 1'],
             columns=['Predicted 0','Predicted 1'])
print(classification_report(y_test,y_predict_2))<jupyter_output>precision    recall  f1-score   support

           0       0.94      1.00      0.97       119
           1       1.00      0.94      0.97       131

   micro avg       0.97      0.97      0.97       250
   macro avg       0.97      0.97      0.97       250
weighted avg       0.97      0.97      0.97       250
{'fraction_non_alphanumeric': 0.09915966386554621, 'fraction_numerical': 0.04081632653061224, 'mean_word_length': 4.368556701030927, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '667535', 'license': 'MIT', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1356, 'n_words': 356, 'path': 'Mini capstone projects/Ad click prediction_Logistic Regression.ipynb', 'repo_name': 'sungsujaing/DataScience_MachineLearning_Portfolio', 'size': '4165'}