# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/prachi-lad17/Python-Case-Studies/blob/main/Case_Study_2%3A%20Figuring_out_which_customer_may_leave.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="H_4EUYVVmLE_"
# # **Figuring out which customer may leave**

# + id="CzupW10UmRPP"


# + [markdown] id="99qBqi2Oy85D"
# # Figuring Our Which Customers May Leave - Churn Analysis
#
# ### About our Dataset
# Source - https://www.kaggle.com/blastchar/telco-customer-churn
# 1. We have customer information for a Telecommunications company
# 2. We've got customer IDs, general customer info, the servies they've subscribed too, type of contract and monthly charges.
# 3. This is a historic customer information so we have a field stating whether that customer has **churnded** 
# **Field Descriptions**
# - customerID - Customer ID
# - gender - Whether the customer is a male or a female
# - SeniorCitizen - Whether the customer is a senior citizen or not (1, 0)
# - Partner - Whether the customer has a partner or not (Yes, No)
# - Dependents - Whether the customer has dependents or not (Yes, No)
# - tenure - Number of months the customer has stayed with the company
# - PhoneService - Whether the customer has a phone service or not (Yes, No)
# - MultipleLines - Whether the customer has multiple lines or not (Yes, No, No phone service)
# - InternetService - Customer’s internet service provider (DSL, Fiber optic, No)
# - OnlineSecurity - Whether the customer has online security or not (Yes, No, No internet service)
# - OnlineBackup - Whether the customer has online backup or not (Yes, No, No internet service)
# - DeviceProtection - Whether the customer has device protection or not (Yes, No, No internet service)
# - TechSupport - Whether the customer has tech support or not (Yes, No, No internet service)
# - StreamingTV - Whether the customer has streaming TV or not (Yes, No, No internet service)
# - StreamingMovies - Whether the customer has streaming movies or not (Yes, No, No internet service)
# - Contract - The contract term of the customer (Month-to-month, One year, Two year)
# - PaperlessBilling - Whether the customer has paperless billing or not (Yes, No)
# - PaymentMethod - The customer’s payment method (Electronic check, Mailed check Bank transfer (automatic), Credit card (automatic))
# - MonthlyCharges - The amount charged to the customer monthly
# - TotalCharges - The total amount charged to the customer
# - Churn - Whether the customer churned or not (Yes or No)
#
# ***Customer Churn*** - churn is when an existing customer, user, player, subscriber or any kind of return client stops doing business or ends the relationship with a company.
#
# **Aim -** is to figure our which customers may likely churn in future

# + id="u20yT_I8y9y9"
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline
import seaborn as sns 

# + id="joEiBbg42Rzv" colab={"base_uri": "https://localhost:8080/", "height": 301} outputId="932a0fb5-9502-4b52-d766-692af322db5f"
## Loading files

file_name = "https://raw.githubusercontent.com/rajeevratan84/datascienceforbusiness/master/WA_Fn-UseC_-Telco-Customer-Churn.csv"
churn_df = pd.read_csv(file_name)

# Using .head() function to check if file is uploaded. It will print first 5 records.
churn_df.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 301} id="5EIAc8hrFBfI" outputId="70761233-33c8-4869-b6f2-fa58d206b650"
## To check last 5 records, we use .tail() funtion

churn_df.tail()

# + colab={"base_uri": "https://localhost:8080/", "height": 287} id="xTuPxhWWEnKh" outputId="aace9754-539c-426f-8f60-13172f7aabcf"
## To get summary on numeric columns

churn_df.describe()

# + colab={"base_uri": "https://localhost:8080/", "height": 414} id="eW1dP4UGFhBl" outputId="562cea0a-0287-4077-adf6-a8c3986539b8"
## To get summary on each column

churn_df.describe(include="all")

# + colab={"base_uri": "https://localhost:8080/"} id="Ue5BlRaPFnHF" outputId="6b28804a-7250-4287-e81e-1de63a436656"
## TO check categorical variables in dataset

churn_df.select_dtypes(exclude=['int64','float']).columns

# + colab={"base_uri": "https://localhost:8080/"} id="QDJa_HSITeRJ" outputId="b177f153-5a37-42f1-a96c-9f51b4c6baae"
## To check numerical variables

churn_df.select_dtypes(exclude=['object']).columns

# + colab={"base_uri": "https://localhost:8080/"} id="vJrjcDhZV629" outputId="e899ebf2-e8a1-4d58-98e7-ea86170da41b"
## To check the unique values 

churn_df.SeniorCitizen.unique()

# + colab={"base_uri": "https://localhost:8080/"} id="2LvNWOYxWSEW" outputId="bdd32683-63d1-4804-ca42-85f803bc7ce3"
## To check unique levels of tenure

churn_df.tenure.unique()

# + colab={"base_uri": "https://localhost:8080/"} id="Il6ZRcNIWbY2" outputId="b028b653-2b75-4f22-c9e9-234992e1a956"
## Printing unique levels of Churn variable

churn_df.Churn.unique()

# + colab={"base_uri": "https://localhost:8080/"} id="GB5UQvRvWlOv" outputId="6db7f83f-f6d3-408f-8858-89a2d6c56d85"
## How many unique values are there in MonthlyChaerges variable

len(churn_df.MonthlyCharges.unique())

# + colab={"base_uri": "https://localhost:8080/"} id="n0N9iv9IaDfI" outputId="21da339f-5189-4df1-be45-82d9cd992e46"
## How many unique values are there in Churn variable

len(churn_df.Churn.unique())

# + colab={"base_uri": "https://localhost:8080/"} id="Be52eqDNaNCp" outputId="d0548f53-ba3e-4268-a6c4-195ee83d62c3"
## Another way of showing information of data in a single output

print("No_of_Rows: ", churn_df.shape[0])
print()
print("No_of_Columns: ", churn_df.shape[1])
print()
print("Features: ", churn_df.columns.to_list)
print("\nMissing_Values: ", churn_df.isnull().sum().values.sum())
print("\nMissing_Values: ", churn_df.isnull().sum())
print("\nUnique_Values: \n", churn_df.nunique())


# + colab={"base_uri": "https://localhost:8080/"} id="dmdhA8Rnbd_F" outputId="2284ccb9-787c-4a1b-9f58-4fb676ceb69a"
## Print how many churn and not churn 

churn_df['Churn'].value_counts(sort = False)

# + id="M5LomPmNcXKi"


# + [markdown] id="QrHG4v-IcyHG"
# ## **Exporatory Data Analysis**

# + id="oPDkaLOGc2ot"
## It is a best practice to keep a copy, in case we need to check at original dataset in future

churn_df_copy = churn_df.copy()

# + colab={"base_uri": "https://localhost:8080/", "height": 284} id="5pOx1cqufJln" outputId="f309bc92-bdd7-497a-8e59-d8ee7567be98"
## Dropping the columns which are not necessary for the plots we are gonna do.

churn_df_copy.drop(['customerID','MonthlyCharges', 'TotalCharges', 'tenure'], axis=1, inplace=True)
churn_df_copy.head()

# + [markdown] id="VR9QC7LBhx3y"
# ### **pd.crosstab() == if we want to work upon more variable at a time then we use pd.crosstab()**
# * The pandas crosstab function builds a cross-tabulation table that can show the frequency with which certain groups of data appear.
# * The crosstab function can operate on numpy arrays, series or columns in a dataframe. 
# * Pandas does that work behind the scenes to count how many occurrences there are of each combination.
# * The pandas crosstab function is a useful tool for summarizing data. The functionality overlaps with some of the other pandas tools but it occupies a useful place in your data analysis toolbox. 

# + colab={"base_uri": "https://localhost:8080/"} id="yOZA0EtJhzLG" outputId="88eca0a9-5028-460c-b442-091b7812b7a2"
## By using this code we can apply crosstab function for each column

summary = pd.concat([pd.crosstab(churn_df_copy[x], churn_df_copy.Churn) for x in churn_df_copy.columns[:-1]], keys=churn_df_copy.columns[:-1])
summary

# + colab={"base_uri": "https://localhost:8080/"} id="hFr1MwRCmnLx" outputId="ada21740-3a9e-4f4d-efac-9f595454fe96"
## Printing churn rate by gender

pd.crosstab(churn_df_copy['Churn'],churn_df_copy['gender'])

# + colab={"base_uri": "https://localhost:8080/"} id="sfSmcBfMnHbL" outputId="7831c900-7027-4022-f0d1-0e5c3cce2083"
## Printing churn rate by gender with margins

pd.crosstab(churn_df_copy['Churn'],churn_df_copy['gender'],margins=True,margins_name="Total",normalize=True)

# + colab={"base_uri": "https://localhost:8080/"} id="7lm82EcCnvBa" outputId="71f33e45-08e2-4c6f-81f0-d6289074f3c4"
## Checking margins

pd.concat([pd.crosstab(churn_df_copy[x], churn_df_copy.Churn,margins=True,margins_name="Total",normalize=True) for x in churn_df_copy.columns[:-1]], keys=churn_df_copy.columns[:-1])


# + id="FHbXL4n1oevj"
"""Making a % column for summary"""

summary['Churn_%'] = summary['Yes'] / summary['No'] + summary['Yes']


# + colab={"base_uri": "https://localhost:8080/"} id="2ZdfI-hGpDZM" outputId="442bd90e-360c-4a90-e2f7-7f04078e18c7"
summary

# + id="gnNCu0topGPY"


# + [markdown] id="_7XgN9OFpRtZ"
# ## **Visualization and EDA**

# + colab={"base_uri": "https://localhost:8080/", "height": 426} id="cVY9W205pW5A" outputId="7f573cca-ae98-4ffc-8a86-1b8b1752ed68"
import matplotlib.pyplot as plt # this is used for the plot the graph 
import seaborn as sns # used for plot interactive graph.
from pylab import rcParams # Customize Matplotlib plots using rcParams

# Data to plot
labels = churn_df['Churn'].value_counts(sort = True).index
sizes = churn_df['Churn'].value_counts(sort = True)

colors = ["pink","lightblue"]
explode = (0.05,0)  # explode 1st slice
 
rcParams['figure.figsize'] = 7,7
# Plot
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90,)

plt.title('Customer Churn Breakdown')
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 632} id="xWmafmDRpiMO" outputId="e7273f80-d6d7-4512-a80a-00846c7bd9d3"
# Correlation plot doesn't end up being too informative
import matplotlib.pyplot as plt

def plot_corr(df,size=10):
    '''Function plots a graphical correlation matrix for each pair of columns in the dataframe.

    Input:
        df: pandas DataFrame
        size: vertical and horizontal size of the plot'''

    corr = df.corr()
    fig, ax = plt.subplots(figsize=(size, size))
    ax.legend()
    cax = ax.matshow(corr)
    fig.colorbar(cax)
    plt.xticks(range(len(corr.columns)), corr.columns, rotation='vertical')
    plt.yticks(range(len(corr.columns)), corr.columns)
    
plot_corr(churn_df)

# + colab={"base_uri": "https://localhost:8080/", "height": 438} id="S6Xk3v2BpwJO" outputId="ea81993a-088d-4cbc-b87f-eb1c57a182b9"
# Create a Violin Plot showing how monthy charges relate to Churn
# We an see that Churned customers tend to be higher paying customers
g = sns.factorplot(x="Churn", y = "MonthlyCharges",data = churn_df, kind="violin", palette = "Pastel1")

# + colab={"base_uri": "https://localhost:8080/", "height": 438} id="4b1h0U1pp0_z" outputId="a5e8a3f6-9a18-40fc-b7d5-1893c227d724"
# Let's look at Tenure
g = sns.factorplot(x="Churn", y = "tenure",data = churn_df, kind="violin", palette = "Pastel1")

# + id="mtWy031Gp6MZ" colab={"base_uri": "https://localhost:8080/", "height": 678} outputId="fc9c8155-c4d7-43b9-ab89-9ea174974dd2"
plt.figure(figsize= (10,10))

sns.countplot(churn_df['Churn'])

# + [markdown] id="LSq-dksKuic2"
# ## **Preparing our dataset for Machine Learning**

# + colab={"base_uri": "https://localhost:8080/"} id="E6rUvIWkuqQD" outputId="7de34f14-864f-438e-dfbb-ff300f66fa24"
# Check for empty fields, Note, " " is not Null but a spaced character
len(churn_df[churn_df['TotalCharges'] == " "])

# + id="s2C1PnAZxCuq"
## Drop missing data
churn_df = churn_df[churn_df['TotalCharges'] != " "]

# + colab={"base_uri": "https://localhost:8080/"} id="NFpB6Zzix0GQ" outputId="578798a0-335a-4732-9993-ac6f64358930"
len(churn_df[churn_df['TotalCharges'] == " "])

# + colab={"base_uri": "https://localhost:8080/", "height": 423} id="iswexGFW1vMS" outputId="e55c6e54-204b-45ee-fb55-4ff27652dd0d"
## Here we are making diff col - id_col, target_col, 
## Next we are writing a code to check the unique levels in each categorical variable and if it is <6 then it is applying label encoding.
## Label Encoding takes binary column and changes the values to 0 and 1. We do label encoding because our can only understand 0 and 1 language.
## cat_col - will store categorical variables which are having less than 6 unique levels
## id_col - stores customerID column
## target_col - stores Churn column
## num_cols - stores all the numerical columns except id_cols, target_cols, cat_col
## bin_cols - stores the binary variables
## multi_cols - stores the categorical columns which are not binary
## Then next we do label encoding for binary columns 
## And duplicating columns for multi value columns


from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler

#customer id col
id_col = ['customerID']

#Target columns
target_col = ["Churn"]

#categorical columns
cat_cols = churn_df.nunique()[churn_df.nunique() < 6].keys().tolist()
cat_cols = [x for x in cat_cols if x not in target_col]

#numerical columns
num_cols = [x for x in churn_df.columns if x not in cat_cols + target_col + id_col]

#Binary columns with 2 values
bin_cols = churn_df.nunique()[churn_df.nunique() == 2].keys().tolist()

#Columns more than 2 values
multi_cols = [i for i in cat_cols if i not in bin_cols]

#Label encoding Binary columns
le = LabelEncoder()
for i in bin_cols :
    churn_df[i] = le.fit_transform(churn_df[i])
    
#Duplicating columns for multi value columns
churn_df = pd.get_dummies(data = churn_df, columns = multi_cols )
churn_df.head()

# + id="3c74-9Pq15a1" colab={"base_uri": "https://localhost:8080/"} outputId="a86a1575-da26-4d5f-eadf-32345446fc57"
len(churn_df.columns)

# + colab={"base_uri": "https://localhost:8080/"} id="qG5NkJNEF4fG" outputId="d77d6d81-82aa-413c-bb48-4beb810bdfb4"
num_cols

# + colab={"base_uri": "https://localhost:8080/"} id="EvEEWhEHF_Eg" outputId="5588853b-0113-4fd7-9502-4fdd3a0ef6c5"
id_col

# + colab={"base_uri": "https://localhost:8080/"} id="Hc9chOfUGAf0" outputId="8a01265b-a330-4d65-981b-ccd30291ba64"
cat_cols

# + colab={"base_uri": "https://localhost:8080/", "height": 334} id="nGqGckh4GCWI" outputId="9c93b924-1232-4b67-da0e-5621752b3283"
## Scaling Numerical columns
std = StandardScaler()

## Scale data
scaled = std.fit_transform(churn_df[num_cols])
scaled = pd.DataFrame(scaled,columns = num_cols)

## Dropping original values merging scaled values for numerical columns
df_telcom_og = churn_df.copy()
churn_df = churn_df.drop(columns = num_cols,axis = 1)
churn_df = churn_df.merge(scaled, left_index = True, right_index = True, how = "left")

## Churn_df.info()
churn_df.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 251} id="4dtV-IhKIw6_" outputId="fec8a9ee-2152-4812-83fa-3bbc9c0c7530"
churn_df.drop(['customerID'], axis=1, inplace=True)
churn_df.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 431} id="oXTH7QUWI7Sh" outputId="3cfb7c6a-76f2-41f3-ba72-0267b133285d"
churn_df[churn_df.isnull().any(axis=1)]

# + id="w6lYUyfbI9-d" colab={"base_uri": "https://localhost:8080/"} outputId="99d372ce-4751-4aff-db80-1d697c01cfbd"
print(len(churn_df.isnull().sum()))

## Since there are only 11 NA values, we drop them.
churn_df = churn_df.dropna()

# + colab={"base_uri": "https://localhost:8080/", "height": 101} id="m9ILshnxJHvE" outputId="3fde5ff6-26c1-45ac-b47e-c2750f19c5c3"
# Double check that nulls have been removed
churn_df[churn_df.isnull().any(axis=1)]

# + [markdown] id="j7cEtngZ_ZX4"
# # **Splitting into training and testing**

# + id="h8zQD6ap_hHe"
from sklearn.model_selection import train_test_split

# We remove our label values from train data
X = churn_df.drop(['Churn'],axis=1).values

# We assigned our label variable to test data
Y = churn_df['Churn'].values

# + id="8_lQX7-MAvcW"
# Split it to a 70:30 Ratio Train:Test

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.3)

# + colab={"base_uri": "https://localhost:8080/"} id="1xQjKvVPAzcU" outputId="f7bcf753-fa39-4fb9-895f-b0d4ed0803e0"
type(x_train)

# + colab={"base_uri": "https://localhost:8080/", "height": 217} id="Q09QyWEUA6AL" outputId="739fb576-38f7-4b3b-8f76-4990ae6c8528"
df_train = pd.DataFrame(x_train)
df_train.head()

# + colab={"base_uri": "https://localhost:8080/"} id="2uWugCLpBDlZ" outputId="318300e9-309f-4445-868a-9f81baf2c0f0"
print(len(churn_df.columns))
churn_df.columns

# + colab={"base_uri": "https://localhost:8080/", "height": 251} id="ffhgYoomBG8f" outputId="396c6556-3add-4588-c1aa-d76719727173"
churn_df.head()

# + id="KIMtbfcIBKGz"


# + [markdown] id="R8NmVIM1BbXU"
# # **Training LOGISTIC REGRESSION model**

# + id="5pZHxanrBfkC" colab={"base_uri": "https://localhost:8080/"} outputId="430fcba1-d4d2-4f16-<PASSWORD>-<PASSWORD>"
from sklearn.linear_model import LogisticRegression

### creating a model
classifier_model = LogisticRegression()

### passing training data to model
classifier_model.fit(x_train,y_train)

### predicting values x_test using model and storing the values in y_pred
y_pred = classifier_model.predict(x_test)

### interception and coefficient of model
print(classifier_model.intercept_)
print(classifier_model.coef_)
print()

### printing values for better understanding
print(list(zip(y_test, y_pred)))

# + colab={"base_uri": "https://localhost:8080/"} id="ecWrSYQDLQlj" outputId="c8b0b8da-0ba1-4c7b-d3e9-a470d362528f"
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report

### creating and printing confusion matrix
conf_matrix = confusion_matrix(y_test,y_pred)
print(conf_matrix)

### Creating and printing classification report
print("Classification Report: ")
print(classification_report(y_test,y_pred))


### Creating and printing accuracy score
acc = accuracy_score(y_test,y_pred)
print("Accuracy {0:.2f}%".format(100*accuracy_score(y_pred, y_test)))

# + id="snquThPILbFr"


# + [markdown] id="27j7soUlLgPn"
# ## **Feature Importance using Logistic Regression**

# + colab={"base_uri": "https://localhost:8080/"} id="fcsbCDE2LlK6" outputId="3a0be5a6-4190-45ac-e907-338057c6af09"
# Let's see what features mattered most i.e. Feature Importance
# We sort on the co-efficients with the largest weights as those impact the resulting output the most 
coef = classifier_model.coef_[0]
coef = [abs(number) for number in coef]
print(coef)

# + colab={"base_uri": "https://localhost:8080/"} id="YJj6i_7dLzYd" outputId="0a64e9ef-2517-4f46-c41f-3499f45a8982"
# Finding and deleting the label column
cols = list(churn_df.columns)
cols.index('Churn')

# + colab={"base_uri": "https://localhost:8080/"} id="xI_5ToTgMBsc" outputId="16ad4aae-dc38-44de-d956-af2275cc29f6"
del cols[6]
cols

# + colab={"base_uri": "https://localhost:8080/"} id="XdltJMhLMIay" outputId="00d8b07e-bc4c-4e69-f11a-7da8feb9adcf"
# Sorting on Feature Importance
sorted_index = sorted(range(len(coef)), key = lambda k: coef[k], reverse = True)
for idx in sorted_index:
    print(cols[idx])

# + id="oP3xA5b0MOAt"


# + [markdown] id="kVAI6RQ4MSyU"
# ## **Try Random Forests**

# + id="P0LhzSm4MVeB"
from sklearn.ensemble import RandomForestClassifier

random_forest_model = RandomForestClassifier(n_estimators=100,random_state=10) ## it will built 100 DT in background

#fit the model on the data and predict the values

random_forest_model.fit(x_train,y_train)

y_pred_rf = random_forest_model.predict(x_test)

# + colab={"base_uri": "https://localhost:8080/"} id="WghBHSQQMeUU" outputId="809546b1-c3bc-4538-f482-414a369db55d"
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report

### creating and printing confusion matrix
conf_matrix_rf = confusion_matrix(y_test,y_pred_rf)
print(conf_matrix_rf)

### Creating and printing classification report
print("Classification Report: ")
print(classification_report(y_test,y_pred_rf))


### Creating and printing accuracy score
acc = accuracy_score(y_test,y_pred_rf)
print("Accuracy {0:.2f}%".format(100*accuracy_score(y_pred_rf, y_test)))

# + id="SVAoYdBBNtFi"


# + [markdown] id="GeQXdqj1Nt01"
# # **Saving a model**

# + id="5m3ZOu7KMoJM"
import pickle

# save
with open('model.pkl','wb') as f:
    pickle.dump(random_forest_model, f)

# load
with open('model.pkl', 'rb') as f:
    loaded_model_rf = pickle.load(f)

# + id="XV_TqDnQNz9A"
predictions = loaded_model_rf.predict(x_test)

# + colab={"base_uri": "https://localhost:8080/"} id="JzLIANfvjfgs" outputId="666abcd1-b7a6-4ff6-8fba-05170a669936"
predictions

# + id="e_a2c0PUjhpy"


# + [markdown] id="8fMDaH7mjkFZ"
# # **Deep Learning Model**

# + id="YxSc2NlTjt9L"
## Using the newest version of Tensorflow 2.0
# %tensorflow_version 2.x


# + colab={"base_uri": "https://localhost:8080/", "height": 35} id="AEt2tGr_kL9M" outputId="762be354-984d-43da-90dc-8a3f582b1dd8"
## Checking to ensure we are using our GPU
import tensorflow as tf

tf.test.gpu_device_name()

# + id="dDNdk-BykeUq"
# Create a simple model
import tensorflow.keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential()

model.add(Dense(20, kernel_initializer = "uniform",activation = "relu", input_dim=40))
model.add(Dense(1, kernel_initializer = "uniform",activation = "sigmoid"))

model.compile(optimizer= "adam",loss = "binary_crossentropy",metrics = ["accuracy"])

# + colab={"base_uri": "https://localhost:8080/"} id="rdATaKQvk9ze" outputId="4cf65f21-9173-4c58-c536-fc5fb8b74a18"
# Display Model Summary and Show Parameters
model.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="gHt155h5lAHv" outputId="9441e6a0-db18-4338-e49a-cb3c360caa1d"
# Start Training Our Classifier 

batch_size = 64
epochs = 25

history = model.fit(x_train,
                    y_train,
                    batch_size = batch_size,
                    epochs = epochs,
                    verbose = 1,
                    validation_data = (x_test, y_test))

score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

# + colab={"base_uri": "https://localhost:8080/"} id="DN6d4RAVlcLw" outputId="90492694-326b-4881-ec75-dc24b1c6e4a1"
predictions = model.predict(x_test)
predictions = (predictions > 0.5)

print(confusion_matrix(y_test, predictions))
print(classification_report(y_test, predictions))

# + id="_dg0Uw3zlnPy"


# + [markdown] id="L_Jkgea-lzx0"
# # **Saving Model**

# + id="CQDuS4qbl4Zz"
## Simple cnn = simple convolutional neural network 
## You mean a HDF5/H5 file, which is a file format to store structured data, its not a model by itself. 
## Keras saves models in this format as it can easily store the weights and model configuration in a single file.

model.save("simple_cnn_25_epochs.h5")

# + id="mmNpwxu8mstz"
## Loading our model

from tensorflow.keras.models import load_model

classifier_DL_simple_cnn = load_model('simple_cnn_25_epochs.h5')

# + id="zvTqq4wDnJm3"


# + [markdown] id="6nq_MmkEnMyK"
# # **Trying deeper models, checkpoints and stopping early.**

# + colab={"base_uri": "https://localhost:8080/"} id="udMztx4InVdW" outputId="207cf4f7-5aba-4c64-ea3e-320bff74ec2b"
from tensorflow.keras.regularizers import l2
from tensorflow.keras.layers import Dropout
from tensorflow.keras.callbacks import ModelCheckpoint

model2 = Sequential()

# Hidden Layer 1
model2.add(Dense(2000, activation='relu', input_dim=40, kernel_regularizer=l2(0.01)))
model2.add(Dropout(0.3, noise_shape=None, seed=None))

# Hidden Layer 2
model2.add(Dense(1000, activation='relu', input_dim=18, kernel_regularizer=l2(0.01)))
model2.add(Dropout(0.3, noise_shape=None, seed=None))

# Hidden Layer 3
model2.add(Dense(500, activation = 'relu', kernel_regularizer=l2(0.01)))
model2.add(Dropout(0.3, noise_shape=None, seed=None))

model2.add(Dense(1, activation='sigmoid'))

model2.summary()

# Create our checkpoint so that we save model after each epoch
checkpoint = ModelCheckpoint("deep_model_checkpoint.h5",
                             monitor="val_loss",
                             mode="min",
                             save_best_only = True,
                             verbose=1)

# + id="zlZyai-vn7lj"
model2.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# + id="NYtmlhyVoEh_"
# Defining our early stoppping criteria
from tensorflow.keras.callbacks import EarlyStopping 

earlystop = EarlyStopping(monitor = 'val_loss', # value being monitored for improvement
                          min_delta = 0, #Abs value and is the min change required before we stop
                          patience = 2, #Number of epochs we wait before stopping 
                          verbose = 1,
                          restore_best_weights = True) #keeps the best weigths once stopped

# we put our call backs into a callback list
callbacks = [earlystop, checkpoint]

# + colab={"base_uri": "https://localhost:8080/"} id="zbWYb_1IoRbh" outputId="5a623c83-a9e8-49b7-da5d-424f6d95972e"
batch_size = 32
epochs = 10

history = model2.fit(x_train,
                    y_train,
                    batch_size = batch_size,
                    epochs = epochs,
                    verbose = 1,
                    # NOTE We are adding our callbacks here
                    callbacks = callbacks,
                    validation_data = (x_test, y_test))

score = model2.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

# + id="QpeH3PKkoahD"


StarcoderdataJupyterScriptsDedupFiltered