Enterprise Java

Binary classification with Tensorflow 2

This post uses Tensorflow with Keras API for a classification problem of predicting diabetes based on a feed-forward neural network also known as multilayer perceptron and uses Pima Indians Diabetes Database from Kaggle. A Google colab notebook with code is available on GitHub.

Exploratory data analysis

The dataset consists of 8 numeric features each of which does not have any missing values. The database contains 768 records from which 500 correspond to negative outcomes and 268 to positive.

There are no features that strongly correlate to each other.

Building a model

We split the dataset into the training part which constitutes 80% of the whole data and the test part of 20%. A
sequential model consisting of 6 layers. The first one is a normalization layer that is a kind of experimental
preprocessing layer used to coerce it inputs to have distributions with the mean of zero and standard deviation of one.

normalizer = preprocessing.Normalization(axis=-1)

The model contains two three fully-connected layers, two with five units and ReLu activation and one output layer with sigmoid activation function. In addition, there are two dropout layers to prevent overfitting. The layers with ReLu activation use He normal weight initialization and the output layer uses normal Glorot normal weight initialization.

He normal weight initialization

model = Sequential()
model.add(Dense(5, input_shape=(X_train.shape[1],),
                activation='relu', kernel_initializer='he_normal'))
model.add(Dense(5, activation="relu",
model.add(Dense(1, activation="sigmoid",

The model uses Adam optimizer, binary cross-entropy loss function and binary accuracy as a metric.



The learning rate of 1e-5 was picked to ensure the decrease of both training and validation loss. Two dropout layers were added to prevent overfitting.

The accuracy of over 70% was achieved. The confusion matrix is depicted below.






Published on Java Code Geeks with permission by Dmitry Noranovich, partner at our JCG program. See the original article here: Binary classification with Tensorflow 2

Opinions expressed by Java Code Geeks contributors are their own.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Dmitry Noranovich

Dmitry teaches physics and moonlights as a Java developer. He is experienced in building Web applications using a full-profile application server as well as in Java SE. Also he is fond of teaching on-line programming courses
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button