# You can use any Python source file as a module by executing an import statement in some other Python source file.
# The import statement combines two operations; it searches for the named module, then it binds the
# results of that search to a name in the local scope.
import os
import pandas as pd
import numpy as np
# Import matplotlib to visualize the model
import matplotlib.pyplot as plt
# Seaborn is a Python data visualization library based on matplotlib
import seaborn as sns
%matplotlib inline

"""
Explanation: Introduction to Logistic Regression
Learning Objectives

Create Seaborn plots for Exploratory Data Analysis 
Train a Logistic Regression Model using Scikit-Learn

Introduction
This lab is an introduction to logistic regression using Python and Scikit-Learn.  This lab serves as a foundation for more complex algorithms and machine learning models that you will encounter in the course. In this lab, we will use a synthetic advertising data set, indicating whether or not a particular internet user clicked on an Advertisement on a company website. 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.  
Each learning objective will correspond to a #TODO in the student lab notebook -- try to complete that notebook first before reviewing this solution notebook. 
Import Libraries
End of explanation
"""


# TODO 1: Read in the advertising.csv file and set it to a data frame called ad_data.
ad_data = pd.read_csv('../advertising.csv')

"""
Explanation: Load the Dataset
We will use a synthetic advertising dataset.  This data set contains the following features:

'Daily Time Spent on Site': consumer time on site in minutes
'Age': customer 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
End of explanation
"""


# Get the first five rows of DataFrame ad_data.
ad_data.head()

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


# Get a concise summary of DataFrame ad_data.
ad_data.info()

# Get the statistical summary of the DataFrame ad_data.
ad_data.describe()

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


# The isnull() method is used to check and manage NULL values in a data frame.
ad_data.isnull().sum()

"""
Explanation: Let's check for any null values.
End of explanation
"""


# TODO 1
# Seaborn is a Python data visualization library based on matplotlib.
# Set the aesthetic style of the plots.
sns.set_style('whitegrid')
ad_data['Age'].hist(bins=30)
plt.xlabel('Age')

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


# TODO 1
# Seaborn’s jointplot displays a relationship between 2 variables (bivariate) as well as
# 1D profiles (univariate) in the margins.
sns.jointplot(x='Age',y='Area Income',data=ad_data)

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


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

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


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

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


# `train_test_split` is a function in Sklearn model selection for splitting data arrays into two subsets:
# for training data and for testing data.
# With this function, you don't need to divide the dataset manually.
# By default, Sklearn `train_test_split` will make random partitions for the two subsets.
# However, you can also specify a random state for the operation.
from sklearn.model_selection import train_test_split

"""
Explanation: Logistic Regression
Logistic regression is a supervised machine learning process.  It is similar to linear regression, but rather than predict a continuous value, we try to  estimate probabilities by using a logistic function.  Note that even though it has regression in the name, it is for classification.
While linear regression is acceptable for estimating values, logistic regression is best for predicting the class of an observation
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!
End of explanation
"""


X = ad_data[['Daily Time Spent on Site', 'Age', 'Area Income','Daily Internet Usage', 'Male']]
y = ad_data['Clicked on Ad']

"""
Explanation: Next, let's define the features and label.  Briefly, feature is input; label is output. This applies to both classification and regression problems.
End of explanation
"""


# TODO 2
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

"""
Explanation: TODO 2: Split the data into training set and testing set using train_test_split
End of explanation
"""


# Logistic Regression is one of the most simple and commonly used Machine Learning algorithms for two-class classification.
# It is easy to implement and can be used as the baseline for any binary classification problem.
from sklearn.linear_model import LogisticRegression

logmodel = LogisticRegression()
logmodel.fit(X_train,y_train)

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


# Use predict() function to predict values for the testing data.
predictions = logmodel.predict(X_test)

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


# The classification_report function builds a text report showing the main classification metrics.
from sklearn.metrics import classification_report

print(classification_report(y_test,predictions))

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


