import pandas as pd
import sklearn
import seaborn as sns
import matplotlib.pyplot as plt

%matplotlib inline

"""
Explanation: <a href='http://www.pieriandata.com'> <img src='../Pierian_Data_Logo.png' /></a>

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!)
End of explanation
"""


ad_data = pd.read_csv('./advertising.csv')

"""
Explanation: Get the Data
Read in the advertising.csv file and set it to a data frame called ad_data.
End of explanation
"""


ad_data.head()

"""
Explanation: Check the head of ad_data
End of explanation
"""


ad_data.info()

ad_data.describe()

"""
Explanation: Use info and describe() on ad_data
End of explanation
"""


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

"""
Explanation: Exploratory Data Analysis
Let's use seaborn to explore the data!
Try recreating the plots shown below!
 Create a histogram of the Age
End of explanation
"""


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

"""
Explanation: Create a jointplot showing Area Income versus Age.
End of explanation
"""


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

"""
Explanation: Create a jointplot showing the kde distributions of Daily Time spent on site vs. Age.
End of explanation
"""


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

"""
Explanation: Create a jointplot of 'Daily Time Spent on Site' vs. 'Daily Internet Usage'
End of explanation
"""


sns.pairplot(data = ad_data, hue='Clicked on Ad')

"""
Explanation: Finally, create a pairplot with the hue defined by the 'Clicked on Ad' column feature.
End of explanation
"""


from sklearn.model_selection import train_test_split

ad_data.info()

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)

(X_train.shape, X_test.shape)

"""
Explanation: 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
End of explanation
"""


from sklearn.linear_model import LogisticRegression
logmodel = LogisticRegression()

logmodel.fit(X_train, y_train)

logmodel.intercept_

logmodel.intercept_scaling

logmodel.coef_

logmodel.coef_.round(4)

# coefficients after shuffling the train test dataset
logmodel.coef_.round(4)

"""
Explanation: Train and fit a logistic regression model on the training set.
End of explanation
"""


train_predictions = logmodel.predict(X_train)
test_predictions = logmodel.predict(X_test)

"""
Explanation: Predictions and Evaluations
 Now predict values for the testing data.
End of explanation
"""


from sklearn.metrics import classification_report

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']))

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']))

"""
Explanation: Create a classification report for the model.
End of explanation
"""


