import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

"""
Explanation: <h1>Table of Contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#Import-Libraries" data-toc-modified-id="Import-Libraries-1">Import Libraries</a></span></li><li><span><a href="#The-Data" data-toc-modified-id="The-Data-2">The Data</a></span></li><li><span><a href="#Missing-Data" data-toc-modified-id="Missing-Data-3">Missing Data</a></span><ul class="toc-item"><li><span><a href="#Cufflinks-for-plots" data-toc-modified-id="Cufflinks-for-plots-3.1">Cufflinks for plots</a></span></li></ul></li><li><span><a href="#Data-Cleaning" data-toc-modified-id="Data-Cleaning-4">Data Cleaning</a></span></li><li><span><a href="#Converting-Categorical-Features" data-toc-modified-id="Converting-Categorical-Features-5">Converting Categorical Features</a></span></li><li><span><a href="#Train-Test-Split" data-toc-modified-id="Train-Test-Split-6">Train Test Split</a></span></li><li><span><a href="#Training-and-Predicting" data-toc-modified-id="Training-and-Predicting-7">Training and Predicting</a></span></li><li><span><a href="#Evaluation" data-toc-modified-id="Evaluation-8">Evaluation</a></span></li><li><span><a href="#Great-Job!" data-toc-modified-id="Great-Job!-9">Great Job!</a></span></li><li><span><a href="#Validate-against-test-dataset" data-toc-modified-id="Validate-against-test-dataset-10">Validate against test dataset</a></span></li></ul></div>


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

Logistic Regression with Python
For this lecture we will be working with the Titanic Data Set from Kaggle. This is a very famous data set and very often is a student's first step in machine learning! 
We'll be trying to predict a classification- survival or deceased.
Let's begin our understanding of implementing Logistic Regression in Python for classification.
We'll use a "semi-cleaned" version of the titanic data set, if you use the data set hosted directly on Kaggle, you may need to do some additional cleaning not shown in this lecture notebook.
Import Libraries
Let's import some libraries to get started!
End of explanation
"""


train = pd.read_csv('titanic_train.csv')

train.head(25)

"""
Explanation: The Data
Let's start by reading in the titanic_train.csv file into a pandas dataframe.
End of explanation
"""


sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap='viridis')

"""
Explanation: Exploratory Data Analysis
Let's begin some exploratory data analysis! We'll start by checking out missing data!
Missing Data
We can use seaborn to create a simple heatmap to see where we are missing data!
End of explanation
"""


sns.set_style('whitegrid')
sns.countplot(x='Survived',data=train,palette='RdBu_r')

# sns.set_style('whitegrid')
sns.countplot(x='Survived',hue='Sex',data=train,palette='RdBu_r')

# sns.set_style('whitegrid')
sns.countplot(x='Survived',hue='Pclass',data=train,palette='rainbow')

sns.distplot(train['Age'].dropna(),kde=False,color='darkred',bins=30)

train['Age'].hist(bins=30,color='darkred',alpha=0.7)

sns.countplot(x='SibSp',data=train)

train['Fare'].hist(color='green',bins=40,figsize=(8,4))

"""
Explanation: Roughly 20 percent of the Age data is missing. The proportion of Age missing is likely small enough for reasonable replacement with some form of imputation. Looking at the Cabin column, it looks like we are just missing too much of that data to do something useful with at a basic level. We'll probably drop this later, or change it to another feature like "Cabin Known: 1 or 0"
Let's continue on by visualizing some more of the data! Check out the video for full explanations over these plots, this code is just to serve as reference.
End of explanation
"""


import plotly_express as pex

pex.histogram(data_frame=train, x='Fare', nbins=30)

"""
Explanation: Cufflinks for plots

Let's take a quick moment to show an example of cufflinks!
End of explanation
"""


plt.figure(figsize=(12, 7))
sns.boxplot(x='Pclass',y='Age',data=train,palette='winter')

"""
Explanation: Data Cleaning
We want to fill in missing age data instead of just dropping the missing age data rows. One way to do this is by filling in the mean age of all the passengers (imputation).
However we can be smarter about this and check the average age by passenger class. For example:
End of explanation
"""


def impute_age(cols):
    Age = cols[0]
    Pclass = cols[1]
    
    if pd.isnull(Age):

        if Pclass == 1:
            return 37

        elif Pclass == 2:
            return 29

        else:
            return 24

    else:
        return Age

"""
Explanation: We can see the wealthier passengers in the higher classes tend to be older, which makes sense. We'll use these average age values to impute based on Pclass for Age.
End of explanation
"""


train['Age'] = train[['Age','Pclass']].apply(impute_age,axis=1)

"""
Explanation: Now apply that function!
End of explanation
"""


sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap='viridis')

"""
Explanation: Now let's check that heat map again!
End of explanation
"""


train.drop('Cabin',axis=1,inplace=True)

train.head(50)

train.shape

train.dropna(inplace=True)

train.shape

"""
Explanation: Great! Let's go ahead and drop the Cabin column and the row in Embarked that is NaN.
End of explanation
"""


train.info()

sex = pd.get_dummies(train['Sex'],drop_first=True)
embark = pd.get_dummies(train['Embarked'],drop_first=True)

embark.head()

sex.head()

train.drop(['Sex','Embarked','Name','Ticket'],axis=1,inplace=True)

train = pd.concat([train,sex,embark],axis=1)

train.head()

"""
Explanation: Converting Categorical Features
We'll need to convert categorical features to dummy variables using pandas! Otherwise our machine learning algorithm won't be able to directly take in those features as inputs.
End of explanation
"""


from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(train.drop('Survived',axis=1), 
                                                    train['Survived'], test_size=0.30, 
                                                    random_state=101)

"""
Explanation: Great! Our data is ready for our model!
Building a Logistic Regression model
Let's start by splitting our data into a training set and test set (there is another test.csv file that you can play around with in case you want to use all this data for training).
Train Test Split
End of explanation
"""


from sklearn.linear_model import LogisticRegression

logmodel = LogisticRegression()
logmodel.verbose = 1

logmodel.fit(X_train,y_train)

logmodel.coef_

logmodel.intercept_

predictions = logmodel.predict(X_test)

"""
Explanation: Training and Predicting
End of explanation
"""


from sklearn.metrics import classification_report

print(classification_report(y_test,predictions))

"""
Explanation: Let's move on to evaluate our model!
Evaluation
We can check precision,recall,f1-score using classification report!
End of explanation
"""


test_df = pd.read_csv('titanic_test.csv')
test_df.head()

test_df.shape

test_df.iloc[0]

"""
Explanation: Not so bad! You might want to explore other feature engineering and the other titanic_text.csv file, some suggestions for feature engineering:

Try grabbing the Title (Dr.,Mr.,Mrs,etc..) from the name as a feature
Maybe the Cabin letter could be a feature
Is there any info you can get from the ticket?

Great Job!
Validate against test dataset
End of explanation
"""


