Learning Keras for the 1st time with a simple model on Google Colab.

Scott Dallman
CodeX
Published in
3 min readDec 6, 2022

Understanding deep learning using a churn classifier

Photo by Christin Hume on Unsplash

As I am to get deeper into machine learning it was now time to dig into Tensorflow. Instead of trying to build out an intense model with transformations and scaling, I thought it might be best to build a simple model with no large changes to the data and to see how the process works. I decided to use the most popular customer churn dataset from Kaggle the “Telco Customer Churn” which can be found at this link https://www.kaggle.com/datasets/blastchar/telco-customer-churn.

So let’s get started with the model by importing all of our dependencies. We’ll want to bring in pandas to load the data and the library to split our dataset for testing and validation. Directly after that, we’ll import the libraries for TensorFlow with our focus on using Keras to make this model easy to implement

import pandas as pd
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense
from sklearn.metrics import accuracy_score

Next, let’s go ahead and load up the data. I am using Google’s Colab to build the model so I stored the CSV file in my drive to access it.

df = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/Intro Tensorflow/WA_Fn-UseC_-Telco-Customer-Churn.csv')

Once the data is loaded let’s go ahead and drop the columns we don’t need for X and assign y only the labels we are looking for. We’ll also use get_dummies to give us an easy way to convert categorical data into numeric data. This is a really fast way of doing one-hot encoding. Finally, let’s split the data in order to get a training set, I use an 80/20 split.

X = pd.get_dummies(df.drop(['Churn', 'customerID'], axis=1))
y = df['Churn'].apply(lambda x: 1 if x=='Yes' else 0)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)

As you can see below we are really quick at building and compiling a model. I’m not going to dig into the activations functions as that can lead to a lot more reading but will focus on the inputs and outputs. For the inputs we what to take will be the exact number of inputs as the number of columns in the data frame, so we’ll just pull the length of the columns. For the output, we only want one node using the sigmoid activation in order for us to predict did the customer stayed or did they churn.

model = Sequential()
model.add(Dense(units=32, activation='relu', input_dim=len(X_train.columns)))
model.add(Dense(units=64, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='sgd', metrics='accuracy')

On to training the model by using the fit function. I have this set to train with 300 epochs with a batch size of 32. We could go longer but we don’t want to overfit our model.

This step will take some time, so go grab a drink!

model.fit(X_train, y_train, epochs=300, batch_size=32)

Now that our model is done with training it’s time to evaluate how well it did. We will complete a prediction on our test data set. Once the prediction is completed we will map our results to either a 0 or 1 in order to validate against the correct answer in the y_test dataset. Let’s run the accuracy_score and see how well the model is.

Without changing much of the data or feature engineering the model still has around an 80% accuracy score, which I don’t consider that bad considering the amount of time we spent on this.


y_pred = model.predict(X_test)
y_pred = [0 if val < 0.5 else 1 for val in y_hat]
accuracy_score(y_test, y_pred)

If we are planning to move this to production, we’ll just need to save the model to use for later. I added the command if you are looking to load the model and added a link to how you could deploy this into fastAPI.

model.save('tfmodel')
model = load_model('tfmodel')

Stop wasting time, deploy an ML model using fastAPI

We are all done and now you have your first model with Keras in just a few minutes. It’s not as hard as I would have thought and the results worked out quite well. I will have to add that I have no expectations that every model created in this fashion will work out as well as this one. Most models I would assume would need more feature engineering and data modification.

If you like the content I would appreciate a follow!

--

--

Scott Dallman
CodeX
Writer for

Writing about technology and tech trends as a husband, father, all around technology guy, bad golfer and Googler