Logistic Regression Project




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

Import a few libraries you think you'll need (Or just import them as you go along!)


In [6]:
import pandas as pd
import sklearn
import seaborn as sns
import matplotlib.pyplot as plt

%matplotlib inline

Get the Data

Read in the advertising.csv file and set it to a data frame called ad_data.


In [2]:
ad_data = pd.read_csv('./advertising.csv')

Check the head of ad_data


In [3]:
ad_data.head()


Out[3]:
Daily Time Spent on Site Age Area Income Daily Internet Usage Ad Topic Line City Male Country Timestamp Clicked on Ad
0 68.95 35 61833.90 256.09 Cloned 5thgeneration orchestration Wrightburgh 0 Tunisia 2016-03-27 00:53:11 0
1 80.23 31 68441.85 193.77 Monitored national standardization West Jodi 1 Nauru 2016-04-04 01:39:02 0
2 69.47 26 59785.94 236.50 Organic bottom-line service-desk Davidton 0 San Marino 2016-03-13 20:35:42 0
3 74.15 29 54806.18 245.89 Triple-buffered reciprocal time-frame West Terrifurt 1 Italy 2016-01-10 02:31:19 0
4 68.37 35 73889.99 225.58 Robust logistical utilization South Manuel 0 Iceland 2016-06-03 03:36:18 0

Use info and describe() on ad_data


In [4]:
ad_data.info()


<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 10 columns):
Daily Time Spent on Site    1000 non-null float64
Age                         1000 non-null int64
Area Income                 1000 non-null float64
Daily Internet Usage        1000 non-null float64
Ad Topic Line               1000 non-null object
City                        1000 non-null object
Male                        1000 non-null int64
Country                     1000 non-null object
Timestamp                   1000 non-null object
Clicked on Ad               1000 non-null int64
dtypes: float64(3), int64(3), object(4)
memory usage: 78.2+ KB

In [5]:
ad_data.describe()


Out[5]:
Daily Time Spent on Site Age Area Income Daily Internet Usage Male Clicked on Ad
count 1000.000000 1000.000000 1000.000000 1000.000000 1000.000000 1000.00000
mean 65.000200 36.009000 55000.000080 180.000100 0.481000 0.50000
std 15.853615 8.785562 13414.634022 43.902339 0.499889 0.50025
min 32.600000 19.000000 13996.500000 104.780000 0.000000 0.00000
25% 51.360000 29.000000 47031.802500 138.830000 0.000000 0.00000
50% 68.215000 35.000000 57012.300000 183.130000 0.000000 0.50000
75% 78.547500 42.000000 65470.635000 218.792500 1.000000 1.00000
max 91.430000 61.000000 79484.800000 269.960000 1.000000 1.00000

Exploratory Data Analysis

Let's use seaborn to explore the data!

Try recreating the plots shown below!

Create a histogram of the Age


In [11]:
ad_data['Age'].plot(kind='hist', bins=40)


Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a2aa011d0>

Create a jointplot showing Area Income versus Age.


In [15]:
sns.jointplot(x='Age', y='Area Income', data=ad_data)


Out[15]:
<seaborn.axisgrid.JointGrid at 0x1a2af2a390>

Create a jointplot showing the kde distributions of Daily Time spent on site vs. Age.


In [16]:
sns.jointplot(x='Age', y='Daily Time Spent on Site', data=ad_data, kind='kde', color='red')


Out[16]:
<seaborn.axisgrid.JointGrid at 0x1a2b583198>

Create a jointplot of 'Daily Time Spent on Site' vs. 'Daily Internet Usage'


In [18]:
sns.jointplot(x='Daily Time Spent on Site', y='Daily Internet Usage', data=ad_data, color='green')


Out[18]:
<seaborn.axisgrid.JointGrid at 0x1a2bb55a90>

Finally, create a pairplot with the hue defined by the 'Clicked on Ad' column feature.


In [19]:
sns.pairplot(data = ad_data, hue='Clicked on Ad')


/Users/atma6951/anaconda3/envs/pychakras/lib/python3.6/site-packages/statsmodels/nonparametric/kde.py:488: RuntimeWarning: invalid value encountered in true_divide
  binned = fast_linbin(X, a, b, gridsize) / (delta * nobs)
/Users/atma6951/anaconda3/envs/pychakras/lib/python3.6/site-packages/statsmodels/nonparametric/kdetools.py:34: RuntimeWarning: invalid value encountered in double_scalars
  FAC1 = 2*(np.pi*bw/RANGE)**2
Out[19]:
<seaborn.axisgrid.PairGrid at 0x1a2bdb6908>

Logistic Regression

Now 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!

Split the data into training set and testing set using train_test_split


In [20]:
from sklearn.model_selection import train_test_split

In [22]:
ad_data.info()


<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 10 columns):
Daily Time Spent on Site    1000 non-null float64
Age                         1000 non-null int64
Area Income                 1000 non-null float64
Daily Internet Usage        1000 non-null float64
Ad Topic Line               1000 non-null object
City                        1000 non-null object
Male                        1000 non-null int64
Country                     1000 non-null object
Timestamp                   1000 non-null object
Clicked on Ad               1000 non-null int64
dtypes: float64(3), int64(3), object(4)
memory usage: 78.2+ KB

In [36]:
X = ad_data[['Daily Time Spent on Site','Age','Area Income','Daily Internet Usage','Male']]
y = ad_data['Clicked on Ad']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=33)

In [37]:
(X_train.shape, X_test.shape)


Out[37]:
((670, 5), (330, 5))

Train and fit a logistic regression model on the training set.


In [38]:
from sklearn.linear_model import LogisticRegression
logmodel = LogisticRegression()

In [39]:
logmodel.fit(X_train, y_train)


/Users/atma6951/anaconda3/envs/pychakras/lib/python3.6/site-packages/sklearn/linear_model/logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
Out[39]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='warn',
          n_jobs=None, penalty='l2', random_state=None, solver='warn',
          tol=0.0001, verbose=0, warm_start=False)

In [40]:
logmodel.intercept_


Out[40]:
array([0.00951899])

In [41]:
logmodel.intercept_scaling


Out[41]:
1

In [42]:
logmodel.coef_


Out[42]:
array([[-6.10521776e-02,  2.57579631e-01, -1.84014933e-05,
        -2.32040373e-02,  1.74159864e-03]])

In [34]:
logmodel.coef_.round(4)


Out[34]:
array([[-0.0524,  0.2534, -0.    , -0.0265,  0.0218]])

In [43]:
# coefficients after shuffling the train test dataset
logmodel.coef_.round(4)


Out[43]:
array([[-0.0611,  0.2576, -0.    , -0.0232,  0.0017]])

Predictions and Evaluations

Now predict values for the testing data.


In [47]:
train_predictions = logmodel.predict(X_train)
test_predictions = logmodel.predict(X_test)

Create a classification report for the model.


In [46]:
from sklearn.metrics import classification_report

In [55]:
print("Training errors")
print(classification_report(y_true=y_train, 
                      y_pred=train_predictions, 
                      labels=[0,1], 
                      target_names=['Not clicked on ad','Clicked on ad']))


Training errors
                   precision    recall  f1-score   support

Not clicked on ad       0.88      0.93      0.91       333
    Clicked on ad       0.93      0.88      0.90       337

        micro avg       0.90      0.90      0.90       670
        macro avg       0.90      0.90      0.90       670
     weighted avg       0.90      0.90      0.90       670


In [56]:
print("Test errors")
print(classification_report(y_true=y_test, 
                      y_pred=test_predictions, 
                      labels=[0,1], 
                      target_names=['Not clicked on ad','Clicked on ad']))


Test errors
                   precision    recall  f1-score   support

Not clicked on ad       0.84      0.94      0.88       167
    Clicked on ad       0.93      0.81      0.87       163

        micro avg       0.88      0.88      0.88       330
        macro avg       0.88      0.87      0.88       330
     weighted avg       0.88      0.88      0.88       330


In [ ]: