Thoa Shook
4 min readSep 14, 2019

Building Neural Networks using Keras for Classification

Neural Network Model for binary classification

What is Keras
*
Keras is a high -level neural networks API, a free open source -user friendly Python library that runs on top of TensorFlow
* Allows you to define and train neural network models
* Supports both CNN (Convolution Neural Networks) and RNN (Recurrent Neural Networks)

Applications
*
Use for prediction, feature extraction, image classification, and fine-tuning
* In this research, with all the predictors present, predict whether patients have onset of coronary heart disease within 10 years (TenYearCHD)

Steps to build neural network using Keras
*
1. Load the required packages
* 2. Load the data
* 3. Define input features (X) and target variable (y)
* 4. Standardize the inputs/ split data into train and test sets
* 5. Define the model
* 6. Compile the model
* 7. Fit the model
* 8. Evaluate the model
* 9. Make predictions

Load the required packages

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from keras import models
from keras import layers
from keras import optimizers
Import seaborn as sns

Load data

df = pd.read_csv(‘framingham_heart_disease.csv’)

Note:This is the data info after cleaning
* In this research, with all the predictors present, predict whether patients have onset of coronary heart disease within 10 years(TenYearCHD)
* This is basically a binary classification problem. Having CHD classified as 1 and not having CHD is classified as 0.
* Data was obtained at kaggle.com/datasets

Define X ( input variables) and y(target variable)

X = df[df.columns[:-1]]
y = df[‘TenYearCHD’]`

Standardize input variables
Input variables are at different scales. It is best to standardize them

from sklearn.preprocessing import StandardScale
sc = StandardScale()
X = sc.fit_transform(X)

Split data into train and test sets

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.3)

Create the model

model = Sequential()
model.add(Dense(8, input_dim=15, activation=’relu’))
model.add(Dense(8, activation=’relu’))
model.add(Dense(1, activation=’sigmoid’))

Compile the model

model.compile(loss='binary_crossentropy',optimizer='Adam',metrics=['accuracy'])

Fit the model


baseline = model.fit(X, y, epochs=100, batch_size=10)

this is the results after running the baseline model
Evaluate the model
* 1. Plot the learning curve (loss vs epochs)

history_dict = baseline.history
history_dict.keys()
history_dict = baseline.history
loss_values= history_dict['loss']
epochs = range(1,len(loss_values)+1)
plt.plot(epochs, loss_values,'g', label = 'Training loss')
plt.title('Training loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

Note: Training loss gradually decreases and at its minimum when epochs reaches 100.
* 2. Plot the learning curve: training accuracy vs epochs

acc_values = history_dict['acc']
plt.plot(epochs, acc_values, 'r', label='Training acc')
plt.title('Training accuracy')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

Note: Training accuracy gradually increase and its maximum is ~86% when epochs approaches 100.
From both graphs training loss and accuracy, one can see that with epochs = 100 will yield minimum loss and maximum accuracy.

Note: Our model perform very well for both training and test sets, loss and accuracy for both training and test sets yield almost the same results.
Make class predictions with the model

The results is in an array with 4238 elements of 0 and 1. Each element represents a patient with a prediction of 0 ( no CHD) or 1 (onset of CHD). Using the ‘for loop’ to loop through the array, we can count how many patients are going to have CHD.

count_1 = 0
for p in predictions:
if p == 1:
count_1 += 1
print(count_1)

Number of patients will have onset of CHD in 10 years = 72
From a medical record of 4238 patients, we can identify 72 patients that are going to have CHD. Early identification and prevention such as life style change will save money and lives.

Note: This is a simple neural network model using keras for classification. For better and consistent results, the architecture of this Neural Networks should be modified and implemented.