<jupyter_start><jupyter_text>Click Through Rate (CTR) Prediction  In this project we will work with an advertising data set, indicating whether or not a particular internet user clicked on an advertisement.We will create a logistic regression model that will predict whether or not a user will click on an ad, based on his/her features. Also, then we will try to improve the accuracy of our model with random forest classifier.  1. Loading Data and Packages<jupyter_code># importing Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>DataThe 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<jupyter_code># Loading the dataset
dataset = pd.read_csv('advertising.csv')
dataset.head()
dataset.info()
dataset.describe()<jupyter_output><empty_output><jupyter_text>2. Exploratory Analysis Checking out the distribution of user age.<jupyter_code>plt.hist(dataset['Age'],bins=30,edgecolor='black')
plt<jupyter_output><empty_output><jupyter_text>Checking out the relationship between age and daily time spent on site.<jupyter_code>sns.jointplot('Age','Daily Time Spent on Site',dataset)
sns.plt.ylim(20, 100)<jupyter_output>/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_axes.py:6521: MatplotlibDeprecationWarning: 
The 'normed' kwarg was deprecated in Matplotlib 2.1 and will be removed in 3.1. Use 'density' instead.
  alternative="'density'", removal="3.1")<jupyter_text>And the relationship between daily time spent on site and daily internet usage.<jupyter_code>sns.jointplot('Daily Time Spent on Site','Daily Internet Usage',dataset)<jupyter_output>/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_axes.py:6521: MatplotlibDeprecationWarning: 
The 'normed' kwarg was deprecated in Matplotlib 2.1 and will be removed in 3.1. Use 'density' instead.
  alternative="'density'", removal="3.1")<jupyter_text>Finally, a pairplot to visualise everything else, colored on the basis of whether they clicked the ad or not.<jupyter_code>sns.pairplot(dataset,hue='Clicked on Ad')<jupyter_output><empty_output><jupyter_text>3. Data Preprocessing<jupyter_code>dataset.columns<jupyter_output><empty_output><jupyter_text>As we can't directly use the 'Country' feature (because it's a categorical string), we have to find another way to feed it into the model.One way to go about this is to drop the feature, but we risk losing useful information.So, what we can do is, convert the categorical feature into dummy variables using pandas.<jupyter_code>countries = pd.get_dummies(dataset['Country'],drop_first=True)<jupyter_output><empty_output><jupyter_text>Concatenating dummy variables with the original dataset, and dropping other features.<jupyter_code>dataset = pd.concat([dataset,countries],axis=1)
dataset.head()<jupyter_output><empty_output><jupyter_text>Dropping redudant columns as they are not useful in predicting whether the user clicked on the ad or not.<jupyter_code>dataset.drop(['Country','Ad Topic Line','City','Timestamp'],axis=1,inplace=True)
dataset.head()
X = dataset.drop('Clicked on Ad',axis=1)
y = dataset['Clicked on Ad']
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)<jupyter_output><empty_output><jupyter_text>4. Fitting the Logistic Regression classifier model<jupyter_code># Fitting LogisticRegression classifier to the Training set
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state=42)
classifier.fit(X_train, y_train)
# Predicting the Test Set Results
y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cm
# Calculating the model accuracy
logistic_train_accuracy = classifier.score(X_train, y_train)*100
logistic_test_accuracy = classifier.score(X_test, y_test)*100
print('-------------Model Performance-------------')
print('Logistic Train Accuracy : ' + str(logistic_train_accuracy))
print('Logistic Test Accuracy : ' + str(logistic_test_accuracy))
from sklearn.metrics import classification_report
print(classification_report(y_test,y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.82      0.97      0.89       120
           1       0.97      0.81      0.88       130

   micro avg       0.89      0.89      0.89       250
   macro avg       0.90      0.89      0.89       250
weighted avg       0.90      0.89      0.89       250<jupyter_text>5. Fitting the Random Forest Classifier model<jupyter_code># Fitting RandomForest classifier to the Training set
from sklearn.ensemble import RandomForestClassifier
classifier_rf =  RandomForestClassifier(n_estimators=100, criterion='entropy', random_state=42)
classifier_rf.fit(X_train, y_train)
# Predicting the Test set results
y_pred_rf = classifier_rf.predict(X_test)
# Making the Confusion Matrix
cm_rf = confusion_matrix(y_test, y_pred_rf)
cm_rf
# Calculating the model accuracy
randomForest_train_accuracy = classifier_rf.score(X_train, y_train)*100
randomForest_test_accuracy = classifier_rf.score(X_test, y_test)*100
print('-----------------Model Performance-----------------')
print('Random Forest Train Accuracy : ' + str(randomForest_train_accuracy))
print('Random Forest Test Accuracy : ' + str(randomForest_test_accuracy))
print(classification_report(y_test,y_pred_rf))<jupyter_output>precision    recall  f1-score   support

           0       0.94      0.93      0.94       120
           1       0.94      0.95      0.94       130

   micro avg       0.94      0.94      0.94       250
   macro avg       0.94      0.94      0.94       250
weighted avg       0.94      0.94      0.94       250
{'fraction_non_alphanumeric': 0.09273619243551195, 'fraction_numerical': 0.030226301630004747, 'mean_word_length': 4.592920353982301, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '448349', 'license': 'MIT', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1910, 'n_words': 597, 'path': 'CTR Prediction/ctr.ipynb', 'repo_name': 'theDeepanshuMourya/Machine-Learning-Projetcs', 'size': '6319'}