<jupyter_start><jupyter_text>Logistic Regression Project In this project we will be working with a fake advertising data set, indicating whether or not a particular internet user clicked on an Advertisement.**We will try to create a model that will predict whether or not they will click on an ad based off the features of that user.**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 Import Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Get the Data<jupyter_code>ad_data = pd.read_csv('advertising.csv')
ad_data.head()
ad_data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 10 columns):
 #   Column                    Non-Null Count  Dtype  
---  ------                    --------------  -----  
 0   Daily Time Spent on Site  1000 non-null   float64
 1   Age                       1000 non-null   int64  
 2   Area Income               1000 non-null   float64
 3   Daily Internet Usage      1000 non-null   float64
 4   Ad Topic Line             1000 non-null   object 
 5   City                      1000 non-null   object 
 6   Male                      1000 non-null   int64  
 7   Country                   1000 non-null   object 
 8   Timestamp                 1000 non-null   object 
 9   Clicked on Ad             1000 non-null   int64  
dtypes: float64(3), int64(3), object(4)
memory usage: 78.2+ KB<jupyter_text>-------  Exploratory Data AnalysisLet's use seaborn to explore the data!Try recreating the plots shown below!**Create a histogram of the Age**<jupyter_code>ad_data.columns
sns.displot(data=ad_data, x='Age', bins=30);
plt.xlim(0, 65)<jupyter_output><empty_output><jupyter_text>------ **Create a jointplot showing Area Income versus Age.**<jupyter_code>sns.jointplot(data=ad_data, x='Age', y='Area Income');<jupyter_output><empty_output><jupyter_text>**Create a jointplot showing the kde distributions of Daily Time spent on site vs. Age.**<jupyter_code>sns.jointplot(data=ad_data, x='Age', y='Daily Time Spent on Site', kind='kde', shade=True, fill=True, color='red');<jupyter_output><empty_output><jupyter_text>------- **Create a jointplot of 'Daily Time Spent on Site' vs. 'Daily Internet Usage'**<jupyter_code>sns.jointplot(data=ad_data, x='Daily Time Spent on Site', y='Daily Internet Usage', color='green');<jupyter_output><empty_output><jupyter_text>------ **Finally, create a pairplot with the hue defined by the 'Clicked on Ad' column feature.**<jupyter_code>sns.pairplot(data=ad_data, hue='Clicked on Ad');<jupyter_output><empty_output><jupyter_text>Logistic RegressionNow it's time to do a train test split, and train our model!You'll have the freedom here to choose columns that you want to train on!<jupyter_code>ad_data.columns
ad_data.head(2)
ad_data['Ad Topic Line'].nunique()
ad_data['City'].nunique()
# separate features and labels
X = ad_data[['Daily Time Spent on Site', 'Age', 'Area Income',
       'Daily Internet Usage', 'Male']]
             
y = ad_data['Clicked on Ad']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, )
X_train.shape, X_test.shape, y_train.shape, y_test.shape
from sklearn.linear_model import LogisticRegression
log_r = LogisticRegression(max_iter=1000)
log_r.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Predictions and Evaluations<jupyter_code>predictions = log_r.predict(X_test)
from sklearn.metrics import classification_report, plot_confusion_matrix
print(classification_report(y_test, predictions))
plot_confusion_matrix(log_r, X_test, y_test);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1024785510009533, 'fraction_numerical': 0.022402287893231648, 'mean_word_length': 4.118292682926829, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '139938', 'license': 'MIT', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1273, 'n_words': 432, 'path': 'Data Science and Machine Learning Bootcamp - JP/13-Logistic-Regression/02-Logistic Regression - Advertisement Project.ipynb', 'repo_name': 'ptyadana/probability-and-statistics-for-business-and-data-science', 'size': '4196'}