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 radius | mean texture | mean perimeter | mean area | mean smoothness | mean compactness | mean concavity | mean concave points | mean symmetry | mean fractal dimension | ... | worst radius | worst texture | worst perimeter | worst area | worst smoothness | worst compactness | worst concavity | worst concave points | worst symmetry | worst fractal dimension | |
| 0 | 17.99 | 10.38 | 122.80 | 1001.0 | 0.11840 | 0.27760 | 0.3001 | 0.14710 | 0.2419 | 0.07871 | ... | 25.38 | 17.33 | 184.60 | 2019.0 | 0.1622 | 0.6656 | 0.7119 | 0.2654 | 0.4601 | 0.11890 |
| 1 | 20.57 | 17.77 | 132.90 | 1326.0 | 0.08474 | 0.07864 | 0.0869 | 0.07017 | 0.1812 | 0.05667 | ... | 24.99 | 23.41 | 158.80 | 1956.0 | 0.1238 | 0.1866 | 0.2416 | 0.1860 | 0.2750 | 0.08902 |
| 2 | 19.69 | 21.25 | 130.00 | 1203.0 | 0.10960 | 0.15990 | 0.1974 | 0.12790 | 0.2069 | 0.05999 | ... | 23.57 | 25.53 | 152.50 | 1709.0 | 0.1444 | 0.4245 | 0.4504 | 0.2430 | 0.3613 | 0.08758 |
| 3 | 11.42 | 20.38 | 77.58 | 386.1 | 0.14250 | 0.28390 | 0.2414 | 0.10520 | 0.2597 | 0.09744 | ... | 14.91 | 26.50 | 98.87 | 567.7 | 0.2098 | 0.8663 | 0.6869 | 0.2575 | 0.6638 | 0.17300 |
| 4 | 20.29 | 14.34 | 135.10 | 1297.0 | 0.10030 | 0.13280 | 0.1980 | 0.10430 | 0.1809 | 0.05883 | ... | 22.54 | 16.67 | 152.20 | 1575.0 | 0.1374 | 0.2050 | 0.4000 | 0.1625 | 0.2364 | 0.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




