Skip to main content

Command Palette

Search for a command to run...

Implementing neural network using keras and Tensorflow

Published
5 min read
Implementing neural network using keras and Tensorflow

In this article, we will use a basic neural network to predict breast cancer outcomes. We will import a dataset from the sklearn library. The breast cancer dataset is a classic and very easy binary classification dataset. The copy of UCI ML Breast Cancer Wisconsin (Diagnostic) dataset is downloaded from: https://archive.ics.uci.edu/dataset/17/breast+cancer+wisconsin+diagnostic

Importing all the libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn.datasets
from sklearn.model_selection import train_test_split
from tensorflow import keras
from tensorflow.keras import layers

Let's dive into our data by loading it into a Pandas DataFrame! After that, we'll take a sneak peek at the first five rows to see what insights we can uncover.

breast_cancer_dataset = sklearn.datasets.load_breast_cancer()
# loading the data to a data frame
data_frame = pd.DataFrame(breast_cancer_dataset.data, columns = breast_cancer_dataset.feature_names)

# print the first 5 rows of the dataframe
data_frame.head()
mean radiusmean texturemean perimetermean areamean smoothnessmean compactnessmean concavitymean concave pointsmean symmetrymean fractal dimension...worst radiusworst textureworst perimeterworst areaworst smoothnessworst compactnessworst concavityworst concave pointsworst symmetryworst fractal dimension
017.9910.38122.801001.00.118400.277600.30010.147100.24190.07871...25.3817.33184.602019.00.16220.66560.71190.26540.46010.11890
120.5717.77132.901326.00.084740.078640.08690.070170.18120.05667...24.9923.41158.801956.00.12380.18660.24160.18600.27500.08902
219.6921.25130.001203.00.109600.159900.19740.127900.20690.05999...23.5725.53152.501709.00.14440.42450.45040.24300.36130.08758
311.4220.3877.58386.10.142500.283900.24140.105200.25970.09744...14.9126.5098.87567.70.20980.86630.68690.25750.66380.17300
420.2914.34135.101297.00.100300.132800.19800.104300.18090.05883...22.5416.67152.201575.00.13740.20500.40000.16250.23640.07678
 # number of rows and columns in the dataset
data_frame.shape
(569, 31)

We will split the data into 20% for Test data and rest as Training sample data

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=2)
print(X.shape, X_train.shape, X_test.shape)

StandardScaler is a preprocessing method available in scikit-learn that is designed to standardize features in a dataset. This technique involves two key steps: first, it subtracts the mean of each feature, which effectively centers the data around zero. Second, it scales the features by dividing each one by its standard deviation, resulting in a standard deviation of 1 for all features. By standardizing the features in this way, StandardScaler facilitates a clearer interpretation of the coefficients or weights associated with each feature, making it a valuable tool in many machine learning workflows.

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()

X_train_std = scaler.fit_transform(X_train)

X_test_std = scaler.transform(X_test)

Now we create a Sequential TensorFlow model with two Dense layers.

def get_model():
    model = keras.Sequential([
        layers.Dense(20, activation="relu"),
        layers.Dense(1, activation="sigmoid")
    ])
    model.compile(optimizer=keras.optimizers.RMSprop(1e-2),
                  loss="binary_crossentropy",
                  metrics=["accuracy"])
    return model

First dense layer uses ReLU activation function and second layer uses sigmoid activation function to predict the probability of cancer being benign or malignant. Here we have used optimizer as RMSprop whose hypeparameter \(\beta\) is set to \(1e^{-2}\). Loss function being used is binary_crossentropy and we will collect accuracy metrics.

we call instantiate the model and call function fit() on model.

model = get_model()
history = model.fit(X_train_std, Y_train, validation_split=0.2, epochs=10)

We keep the validation data as 20%( validation_split=0.2 ) to measure the metrics for model performace and we will run 10 epochs.

Epoch 1/10
12/12 [==============================] - 0s 6ms/step - loss: 0.1880 - accuracy: 0.9313 - val_loss: 0.1112 - val_accuracy: 0.9451
Epoch 2/10
12/12 [==============================] - 0s 1ms/step - loss: 0.0895 - accuracy: 0.9725 - val_loss: 0.0811 - val_accuracy: 0.9670
Epoch 3/10
12/12 [==============================] - 0s 1ms/step - loss: 0.0646 - accuracy: 0.9835 - val_loss: 0.0719 - val_accuracy: 0.9670
Epoch 4/10
12/12 [==============================] - 0s 1ms/step - loss: 0.0556 - accuracy: 0.9863 - val_loss: 0.0749 - val_accuracy: 0.9560
Epoch 5/10
12/12 [==============================] - 0s 1ms/step - loss: 0.0471 - accuracy: 0.9863 - val_loss: 0.0660 - val_accuracy: 0.9670
Epoch 6/10
12/12 [==============================] - 0s 1ms/step - loss: 0.0431 - accuracy: 0.9890 - val_loss: 0.0576 - val_accuracy: 0.9780
Epoch 7/10
12/12 [==============================] - 0s 1ms/step - loss: 0.0365 - accuracy: 0.9863 - val_loss: 0.0465 - val_accuracy: 0.9780
Epoch 8/10
12/12 [==============================] - 0s 1ms/step - loss: 0.0362 - accuracy: 0.9890 - val_loss: 0.0718 - val_accuracy: 0.9670
Epoch 9/10
12/12 [==============================] - 0s 1ms/step - loss: 0.0296 - accuracy: 0.9890 - val_loss: 0.0565 - val_accuracy: 0.9890
Epoch 10/10
12/12 [==============================] - 0s 1ms/step - loss: 0.0255 - accuracy: 0.9918 - val_loss: 0.0560 - val_accuracy: 0.9890

Lets plot the Training and validation accuracy.

plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])

plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')

plt.legend(['training data', 'validation data'], loc = 'lower right')

Lets also plot the loss function of validation data and training data against each epoch.

Now lets do prediction on Test data

Y_pred = model.predict(X_test_std)
print(Y_pred.shape)
print(Y_pred[0])
(114, 1)
[0.92443144]

Lets call a function Which translates this prediction to benign or malignant based on probability predicted.

def predictTheModel(Y):

    for i in range(Y.shape[0]):
        if Y[i] > 0.5 :
            print("Benign")
        else:
            print("Malignant")

Sample of the prediction is shown below for the test data.

predictTheModel(Y_pred)
Benign
Benign
Benign
Malignant
Benign
Malignant
Benign

More from this blog

Path To Machine Learning

37 posts