<jupyter_start><jupyter_text>Advertising Data - Example Caseusing Python and Scikit-learn to apply logistic regression classification to a fictional case:- advertising company is testing a specific type of online advertising campaign- they would like to identify user that would click on a specific new advertising based on their features before playing a the ad- therfore, they did some testing and recorded the data (see below)- they try to create a model that will predict whether or not a user will click on an ad based off the features of that userThis 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(advertising data is fictional)<jupyter_code># importing needed libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# import data
df = pd.read_csv('advertising.csv')
# view data
df.head(3)
df.info()
# exploratory analysis
df.describe()
# show 'Age' distribution on a histogram for better overview of customers
sns.distplot(df['Age'], kde=False, color='#2f6f40', bins=30)<jupyter_output><empty_output><jupyter_text>Users are mostly of younger age (<40)<jupyter_code># comparing 'Age' versus 'Area Income'
sns.jointplot(df['Age'],df['Area Income'], color='#2f6f40')<jupyter_output><empty_output><jupyter_text>Interestingly, there seems to be no particular strong relationship between 'Age' and 'Area Income' when looking at visualization. Potentially with increase in age you start to move into areas with higher income, but this falls off even later in life.<jupyter_code># now focusing on 'Daily Time spent on site' versus 'Age' using kde visualization
sns.jointplot(df['Age'],df['Daily Time Spent on Site'], kind='kde', color='#2f6f40')<jupyter_output><empty_output><jupyter_text>A slight relationship between both variables could be interpreted, with higher age correlating with daily less time spent on the site.<jupyter_code># for last detailed comparison, we compare 'Daily Internet Usage' and 'Daily Time Spent on Site'
sns.jointplot(df['Daily Time Spent on Site'], df['Daily Internet Usage'], color='#2f6f40')<jupyter_output><empty_output><jupyter_text>A split among the test user can be observed, thos with less internet usage on a daily basis and low to mid time spent on the site vs. those with high amount of daily internet usage and a lot of time spent on the site.<jupyter_code># finally, comapring all variables split by users that did not vs. did click on the ad in the test run
sns.pairplot(data=df, hue='Clicked on Ad', palette='GnBu_r')<jupyter_output><empty_output><jupyter_text>User that clicked on the ad (green) seamingly spent less time on the site (graph row:1, column:1), have less daily internet usage (graph r:4, c:4) which is also supported by graph in row 4, column 1.    It also seems like the more time the user spents on the site and the higher the area income, the less likely they were to click on the ad (graph r:3, c:1).<jupyter_code>df.info()
# after first insights, a logistic regression model will help to predict future useres
# first, string rows with multiple categorial values (as data has too few test users compared to features of these variables)
# as well as timestamp columns are droped (Country, Ad Topic Line, City) for simplicity
df_lr = df.drop(labels=['Ad Topic Line', 'City', 'Country', 'Timestamp'], axis=1)
df_lr.head()
# create df for predictor and response variable
X = df_lr.drop(labels=['Clicked on Ad'], axis=1)
y = df_lr['Clicked on Ad']
# create train and test data using sklearn
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# create logistic regression model object and train model
from sklearn.linear_model import LogisticRegression
logmodel = LogisticRegression()
logmodel.fit(X_train,y_train)
# get predictions and evaluate model (precision, recall, f1-score)
predictions = logmodel.predict(X_test)

from sklearn.metrics import classification_report
print(classification_report(y_test,predictions))
# create confusion matrix and extract true / false positives & negatives
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test,predictions)

tn, fp, fn, tp = confusion_matrix(y_test, predictions).ravel()
(tn, fp, fn, tp)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08027667155732551, 'fraction_numerical': 0.007545587927059317, 'mean_word_length': 5.82689556509299, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '558178', 'license': 'MIT', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1321, 'n_words': 620, 'path': '02_Logistic-Regression_Advertising-Data/Logistic-Regression_Advertising-Data.ipynb', 'repo_name': 'danczw/Repository-for-ML-Beginners', 'size': '4771'}