# -*- coding: utf-8 -*-
"""Logistic Regression Project(advertising data set).ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1uFJdpLlQagAzxcGw0pDkex6mPXAoIV1C

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!)**
"""

# Commented out IPython magic to ensure Python compatibility.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline

"""## Get the Data
**Read in the advertising.csv file and set it to a data frame called ad_data.**
"""

from google.colab import files

uploaded = files.upload()

ad_data=pd.read_csv("advertising.csv")

"""**Check the head of ad_data**"""

ad_data.head()

"""** Use info and describe() on ad_data**"""

ad_data.describe()

ad_data.info()



"""## Exploratory Data Analysis

Let's use seaborn to explore the data!

Try recreating the plots shown below!

** Create a histogram of the Age**
"""

ad_data["Age"].plot.hist(bins=40,figsize=(10,4))

"""**Create a jointplot showing Area Income versus Age.**"""

sns.jointplot(x="Age",y="Area Income",data=ad_data)

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

sns.jointplot(x="Age",y="Daily Time Spent on Site",data=ad_data,kind="kde",color="red")

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

sns.jointplot(x="Daily Time Spent on Site",y="Daily Internet Usage",data=ad_data,color="green")

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

sns.pairplot(ad_data,hue="Clicked on Ad",palette="coolwarm",diag_kind ="hist")

"""# 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**
"""

from sklearn.model_selection import train_test_split

ad_data.columns



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.30, 
                                                    random_state=101)

from sklearn.linear_model import LogisticRegression
lg= LogisticRegression()

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

lg.fit(X_train,y_train)

"""## Predictions and Evaluations
** Now predict values for the testing data.**
"""

predictions = lg.predict(X_test)

"""** Create a classification report for the model.**"""

from sklearn.metrics import classification_report
print(classification_report(y_test,predictions))

from sklearn.metrics import  confusion_matrix
print(confusion_matrix(y_test,predictions))
08a3292836356835ef5e0e79ef6f533b5050399d
<mask token>

<mask token>
ad_data.head()
<mask token>
ad_data.describe()
ad_data.info()
<mask token>
ad_data['Age'].plot.hist(bins=40, figsize=(10, 4))
<mask token>
sns.jointplot(x='Age', y='Area Income', data=ad_data)
<mask token>
sns.jointplot(x='Age', y='Daily Time Spent on Site', data=ad_data, kind=
    'kde', color='red')
<mask token>
sns.jointplot(x='Daily Time Spent on Site', y='Daily Internet Usage', data=
    ad_data, color='green')
<mask token>
sns.pairplot(ad_data, hue='Clicked on Ad', palette='coolwarm', diag_kind='hist'
    )
<mask token>
ad_data.columns
<mask token>
lg.fit(X_train, y_train)
<mask token>
print(classification_report(y_test, predictions))
<mask token>
print(confusion_matrix(y_test, predictions))

<mask token>
uploaded = files.upload()
ad_data = pd.read_csv('advertising.csv')
<mask token>
ad_data.head()
<mask token>
ad_data.describe()
ad_data.info()
<mask token>
ad_data['Age'].plot.hist(bins=40, figsize=(10, 4))
<mask token>
sns.jointplot(x='Age', y='Area Income', data=ad_data)
<mask token>
sns.jointplot(x='Age', y='Daily Time Spent on Site', data=ad_data, kind=
    'kde', color='red')
<mask token>
sns.jointplot(x='Daily Time Spent on Site', y='Daily Internet Usage', data=
    ad_data, color='green')
<mask token>
sns.pairplot(ad_data, hue='Clicked on Ad', palette='coolwarm', diag_kind='hist'
    )
<mask token>
ad_data.columns
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.3,
    random_state=101)
<mask token>
lg = LogisticRegression()
<mask token>
lg.fit(X_train, y_train)
<mask token>
predictions = lg.predict(X_test)
<mask token>
print(classification_report(y_test, predictions))
<mask token>
print(confusion_matrix(y_test, predictions))

<mask token>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
<mask token>
from google.colab import files
uploaded = files.upload()
ad_data = pd.read_csv('advertising.csv')
<mask token>
ad_data.head()
<mask token>
ad_data.describe()
ad_data.info()
<mask token>
ad_data['Age'].plot.hist(bins=40, figsize=(10, 4))
<mask token>
sns.jointplot(x='Age', y='Area Income', data=ad_data)
<mask token>
sns.jointplot(x='Age', y='Daily Time Spent on Site', data=ad_data, kind=
    'kde', color='red')
<mask token>
sns.jointplot(x='Daily Time Spent on Site', y='Daily Internet Usage', data=
    ad_data, color='green')
<mask token>
sns.pairplot(ad_data, hue='Clicked on Ad', palette='coolwarm', diag_kind='hist'
    )
<mask token>
from sklearn.model_selection import train_test_split
ad_data.columns
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.3,
    random_state=101)
from sklearn.linear_model import LogisticRegression
lg = LogisticRegression()
<mask token>
lg.fit(X_train, y_train)
<mask token>
predictions = lg.predict(X_test)
<mask token>
from sklearn.metrics import classification_report
print(classification_report(y_test, predictions))
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_test, predictions))

# -*- coding: utf-8 -*-
"""Logistic Regression Project(advertising data set).ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1uFJdpLlQagAzxcGw0pDkex6mPXAoIV1C

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!)**
"""

# Commented out IPython magic to ensure Python compatibility.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline

"""## Get the Data
**Read in the advertising.csv file and set it to a data frame called ad_data.**
"""

from google.colab import files

uploaded = files.upload()

ad_data=pd.read_csv("advertising.csv")

"""**Check the head of ad_data**"""

ad_data.head()

"""** Use info and describe() on ad_data**"""

ad_data.describe()

ad_data.info()



"""## Exploratory Data Analysis

Let's use seaborn to explore the data!

Try recreating the plots shown below!

** Create a histogram of the Age**
"""

ad_data["Age"].plot.hist(bins=40,figsize=(10,4))

"""**Create a jointplot showing Area Income versus Age.**"""

sns.jointplot(x="Age",y="Area Income",data=ad_data)

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

sns.jointplot(x="Age",y="Daily Time Spent on Site",data=ad_data,kind="kde",color="red")

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

sns.jointplot(x="Daily Time Spent on Site",y="Daily Internet Usage",data=ad_data,color="green")

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

sns.pairplot(ad_data,hue="Clicked on Ad",palette="coolwarm",diag_kind ="hist")

"""# 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**
"""

from sklearn.model_selection import train_test_split

ad_data.columns



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.30, 
                                                    random_state=101)

from sklearn.linear_model import LogisticRegression
lg= LogisticRegression()

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

lg.fit(X_train,y_train)

"""## Predictions and Evaluations
** Now predict values for the testing data.**
"""

predictions = lg.predict(X_test)

"""** Create a classification report for the model.**"""

from sklearn.metrics import classification_report
print(classification_report(y_test,predictions))

from sklearn.metrics import  confusion_matrix
print(confusion_matrix(y_test,predictions))