Creating a simple Neural Network using Keras for a binary classification task

Kaustubh Atey
Analytics Vidhya
Published in
3 min readJan 27, 2020

--

Keras logo.

In this article, I will show how to implement a basic Neural network using Keras.

Keras is a very user-friendly Deep learning library that allows for easy and fast prototyping. It offers consistent and simple APIs and minimizes the number of user actions required for common use cases, and it provides clear and actionable feedback upon user error.

For the Binary classification task, I will use the ‘Pima Indians Diabetes Dataset’. The objective of the dataset is to diagnostically predict whether or not a patient has diabetes, based on certain diagnostic measurements included in the dataset. There are 768 observations with 8 input variables and 1 output variable.

We will first import the dataset from the .txt file and converting it into a NumPy array. This is done in the following way:

Importing the ‘Pima Indians diabetes dataset.’

After importing the dataset, we must do some data preprocessing before running it through a model. The first eight columns are stored as X_data, and the last column is stored as Y_data.

X_data contains the eight features for different samples, and the Y_data contains the target variable. These variables are further split into X_train, X_test, y_train, y_test using train_test_split function from a sci-kit-learn library.

Data preprocessing.

After preprocessing, the data is ready.

Now, we will build a simple neural network using Keras. Here I have used the Sequential model.

Representation for the neural network.

The first layers of the model contain 16 neurons that take the input from the data and applies the sigmoid activation. The second layer contains a single neuron that takes the input from the preceding layer, applies a hard sigmoid activation and gives the classification output as 0 or 1.

Creating a Sequential model.

Now the model is ready; we will compile it. Here, I have used ‘binary cross-entropy loss’ and SGD (Stochastic gradient descent) optimizer for compilation. The SGD has a learning rate of 0.5 and a momentum of 0.9.

Compiling the model.

Now, we use X_train and y_train for training the model and run it for 100 epochs. After the training is done, the model is evaluated on X_test and y_test.

Training and Testing the model.

When the model is evaluated, we obtain a loss = 0.57 and accuracy = 0.73

The baseline performance of predicting the most prevalent class is a classification accuracy of approximately 65%. Top results achieve a classification accuracy of approximately 77%.

We have achieved a relatively better efficiency with a simple neural network when compared to the average results for this dataset. Better accuracy can be obtained with a deeper network.

Github link for the notebook: Intro_to_Keras_Basic.ipynb

Made in Google Colab by Kaustubh Atey (kaustubh.atey@students.iiserpune.ac.in)

--

--