Health care: Binary classification using simple neural network

Author: Iqbal Hossain

(dated: 12th November, 2019)
Data source: diabetic_data.csv
In [50]:
import numpy as np

data = np.loadtxt('diabetic_data.csv', delimiter=',')

training, test = train_test_split(data, test_size=0.1, random_state=42)

train_x = training[:,0:8]  #select 0-7 column
train_y = training[:,8]  #select last coloumn

test_x = test[:,0:8]  #select 0-7 column
test_y = test[:,8]  #select last column
In [57]:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from sklearn.model_selection import train_test_split

# define the keras model
model = Sequential()
model.add(Dense(10, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# fit the keras model on the dataset
epoch_data = model.fit(train_x, train_y, epochs=100, batch_size=10, verbose=1, validation_data=(test_x, test_y))

score = model.evaluate(test_x, test_y, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

model.save('binary_clas_diabetic_data_model.h5')
Train on 691 samples, validate on 77 samples
Epoch 1/100
691/691 [==============================] - 0s 452us/step - loss: 4.8591 - accuracy: 0.5774 - val_loss: 2.5522 - val_accuracy: 0.4675
Epoch 2/100
691/691 [==============================] - 0s 165us/step - loss: 1.3557 - accuracy: 0.5818 - val_loss: 1.0939 - val_accuracy: 0.6494
Epoch 3/100
691/691 [==============================] - 0s 186us/step - loss: 1.0163 - accuracy: 0.6151 - val_loss: 0.9559 - val_accuracy: 0.6623
Epoch 4/100
691/691 [==============================] - 0s 222us/step - loss: 0.9121 - accuracy: 0.6281 - val_loss: 0.9153 - val_accuracy: 0.6883
Epoch 5/100
691/691 [==============================] - 0s 151us/step - loss: 0.8399 - accuracy: 0.6353 - val_loss: 0.8376 - val_accuracy: 0.6364
Epoch 6/100
691/691 [==============================] - 0s 153us/step - loss: 0.8000 - accuracy: 0.6483 - val_loss: 0.8032 - val_accuracy: 0.6364
Epoch 7/100
691/691 [==============================] - 0s 165us/step - loss: 0.7655 - accuracy: 0.6643 - val_loss: 0.7829 - val_accuracy: 0.6883
Epoch 8/100
691/691 [==============================] - 0s 161us/step - loss: 0.7436 - accuracy: 0.6411 - val_loss: 0.7545 - val_accuracy: 0.6753
Epoch 9/100
691/691 [==============================] - 0s 166us/step - loss: 0.7314 - accuracy: 0.6570 - val_loss: 0.7335 - val_accuracy: 0.6753
Epoch 10/100
691/691 [==============================] - 0s 154us/step - loss: 0.7061 - accuracy: 0.6671 - val_loss: 0.7490 - val_accuracy: 0.7273
Epoch 11/100
691/691 [==============================] - 0s 181us/step - loss: 0.6982 - accuracy: 0.6643 - val_loss: 0.6961 - val_accuracy: 0.6883
Epoch 12/100
691/691 [==============================] - 0s 184us/step - loss: 0.6779 - accuracy: 0.6512 - val_loss: 0.6982 - val_accuracy: 0.6623
Epoch 13/100
691/691 [==============================] - 0s 204us/step - loss: 0.6712 - accuracy: 0.6657 - val_loss: 0.6941 - val_accuracy: 0.6623
Epoch 14/100
691/691 [==============================] - 0s 150us/step - loss: 0.6659 - accuracy: 0.6397 - val_loss: 0.6737 - val_accuracy: 0.6623
Epoch 15/100
691/691 [==============================] - 0s 141us/step - loss: 0.6646 - accuracy: 0.6599 - val_loss: 0.6581 - val_accuracy: 0.7532
Epoch 16/100
691/691 [==============================] - 0s 132us/step - loss: 0.6437 - accuracy: 0.6599 - val_loss: 0.6440 - val_accuracy: 0.7143
Epoch 17/100
691/691 [==============================] - 0s 174us/step - loss: 0.6551 - accuracy: 0.6585 - val_loss: 0.6445 - val_accuracy: 0.7143
Epoch 18/100
691/691 [==============================] - 0s 152us/step - loss: 0.6423 - accuracy: 0.6512 - val_loss: 0.6427 - val_accuracy: 0.7143
Epoch 19/100
691/691 [==============================] - 0s 131us/step - loss: 0.6894 - accuracy: 0.6425 - val_loss: 0.6463 - val_accuracy: 0.6883
Epoch 20/100
691/691 [==============================] - 0s 131us/step - loss: 0.6260 - accuracy: 0.6700 - val_loss: 0.6427 - val_accuracy: 0.6623
Epoch 21/100
691/691 [==============================] - 0s 155us/step - loss: 0.6224 - accuracy: 0.6643 - val_loss: 0.6437 - val_accuracy: 0.7273
Epoch 22/100
691/691 [==============================] - 0s 130us/step - loss: 0.6181 - accuracy: 0.6860 - val_loss: 0.6360 - val_accuracy: 0.7013
Epoch 23/100
691/691 [==============================] - 0s 188us/step - loss: 0.6113 - accuracy: 0.6845 - val_loss: 0.6318 - val_accuracy: 0.7013
Epoch 24/100
691/691 [==============================] - 0s 145us/step - loss: 0.6105 - accuracy: 0.6787 - val_loss: 0.6331 - val_accuracy: 0.7143
Epoch 25/100
691/691 [==============================] - 0s 147us/step - loss: 0.6208 - accuracy: 0.6715 - val_loss: 0.6308 - val_accuracy: 0.7143
Epoch 26/100
691/691 [==============================] - 0s 139us/step - loss: 0.6086 - accuracy: 0.6758 - val_loss: 0.6400 - val_accuracy: 0.7273
Epoch 27/100
691/691 [==============================] - 0s 158us/step - loss: 0.6072 - accuracy: 0.6889 - val_loss: 0.6331 - val_accuracy: 0.7792
Epoch 28/100
691/691 [==============================] - 0s 131us/step - loss: 0.6093 - accuracy: 0.6802 - val_loss: 0.6330 - val_accuracy: 0.7273
Epoch 29/100
691/691 [==============================] - 0s 137us/step - loss: 0.6298 - accuracy: 0.6845 - val_loss: 0.6127 - val_accuracy: 0.7403
Epoch 30/100
691/691 [==============================] - 0s 143us/step - loss: 0.6055 - accuracy: 0.6932 - val_loss: 0.6190 - val_accuracy: 0.7143
Epoch 31/100
691/691 [==============================] - 0s 178us/step - loss: 0.6073 - accuracy: 0.6903 - val_loss: 0.6527 - val_accuracy: 0.6883
Epoch 32/100
691/691 [==============================] - 0s 148us/step - loss: 0.6008 - accuracy: 0.6961 - val_loss: 0.6083 - val_accuracy: 0.7403
Epoch 33/100
691/691 [==============================] - 0s 129us/step - loss: 0.5935 - accuracy: 0.6874 - val_loss: 0.6245 - val_accuracy: 0.7403
Epoch 34/100
691/691 [==============================] - 0s 164us/step - loss: 0.5899 - accuracy: 0.7004 - val_loss: 0.6120 - val_accuracy: 0.7403
Epoch 35/100
691/691 [==============================] - 0s 133us/step - loss: 0.5884 - accuracy: 0.6946 - val_loss: 0.6083 - val_accuracy: 0.7273
Epoch 36/100
691/691 [==============================] - 0s 134us/step - loss: 0.5966 - accuracy: 0.6975 - val_loss: 0.6104 - val_accuracy: 0.7013
Epoch 37/100
691/691 [==============================] - 0s 131us/step - loss: 0.5919 - accuracy: 0.6961 - val_loss: 0.6021 - val_accuracy: 0.7792
Epoch 38/100
691/691 [==============================] - 0s 161us/step - loss: 0.5895 - accuracy: 0.6831 - val_loss: 0.6119 - val_accuracy: 0.7532
Epoch 39/100
691/691 [==============================] - 0s 153us/step - loss: 0.5766 - accuracy: 0.7091 - val_loss: 0.6014 - val_accuracy: 0.7013
Epoch 40/100
691/691 [==============================] - 0s 131us/step - loss: 0.5801 - accuracy: 0.7004 - val_loss: 0.6002 - val_accuracy: 0.7403
Epoch 41/100
691/691 [==============================] - 0s 133us/step - loss: 0.5857 - accuracy: 0.7091 - val_loss: 0.5981 - val_accuracy: 0.7403
Epoch 42/100
691/691 [==============================] - 0s 156us/step - loss: 0.5790 - accuracy: 0.6946 - val_loss: 0.6058 - val_accuracy: 0.7143
Epoch 43/100
691/691 [==============================] - 0s 135us/step - loss: 0.5782 - accuracy: 0.7091 - val_loss: 0.6149 - val_accuracy: 0.7143
Epoch 44/100
691/691 [==============================] - 0s 137us/step - loss: 0.5808 - accuracy: 0.7019 - val_loss: 0.6244 - val_accuracy: 0.7922
Epoch 45/100
691/691 [==============================] - 0s 129us/step - loss: 0.5814 - accuracy: 0.7048 - val_loss: 0.5937 - val_accuracy: 0.7532
Epoch 46/100
691/691 [==============================] - 0s 170us/step - loss: 0.5684 - accuracy: 0.7120 - val_loss: 0.5968 - val_accuracy: 0.7532
Epoch 47/100
691/691 [==============================] - 0s 140us/step - loss: 0.5730 - accuracy: 0.7091 - val_loss: 0.6056 - val_accuracy: 0.7662
Epoch 48/100
691/691 [==============================] - 0s 129us/step - loss: 0.5774 - accuracy: 0.7077 - val_loss: 0.6173 - val_accuracy: 0.7403
Epoch 49/100
691/691 [==============================] - 0s 137us/step - loss: 0.5653 - accuracy: 0.7164 - val_loss: 0.6061 - val_accuracy: 0.7922
Epoch 50/100
691/691 [==============================] - 0s 165us/step - loss: 0.5777 - accuracy: 0.6889 - val_loss: 0.6003 - val_accuracy: 0.7662
Epoch 51/100
691/691 [==============================] - 0s 166us/step - loss: 0.5683 - accuracy: 0.7091 - val_loss: 0.6731 - val_accuracy: 0.7143
Epoch 52/100
691/691 [==============================] - 0s 169us/step - loss: 0.5800 - accuracy: 0.6990 - val_loss: 0.5892 - val_accuracy: 0.7792
Epoch 53/100
691/691 [==============================] - 0s 188us/step - loss: 0.5648 - accuracy: 0.7048 - val_loss: 0.5872 - val_accuracy: 0.7662
Epoch 54/100
691/691 [==============================] - 0s 177us/step - loss: 0.5613 - accuracy: 0.7106 - val_loss: 0.5855 - val_accuracy: 0.7403
Epoch 55/100
691/691 [==============================] - 0s 188us/step - loss: 0.5658 - accuracy: 0.7106 - val_loss: 0.5993 - val_accuracy: 0.7143
Epoch 56/100
691/691 [==============================] - 0s 164us/step - loss: 0.5668 - accuracy: 0.7120 - val_loss: 0.6213 - val_accuracy: 0.7143
Epoch 57/100
691/691 [==============================] - 0s 154us/step - loss: 0.5698 - accuracy: 0.7091 - val_loss: 0.5842 - val_accuracy: 0.7662
Epoch 58/100
691/691 [==============================] - 0s 134us/step - loss: 0.5618 - accuracy: 0.7178 - val_loss: 0.5912 - val_accuracy: 0.7273
Epoch 59/100
691/691 [==============================] - 0s 142us/step - loss: 0.5591 - accuracy: 0.7062 - val_loss: 0.6304 - val_accuracy: 0.7013
Epoch 60/100
691/691 [==============================] - 0s 134us/step - loss: 0.5616 - accuracy: 0.7164 - val_loss: 0.5814 - val_accuracy: 0.7532
Epoch 61/100
691/691 [==============================] - 0s 132us/step - loss: 0.5546 - accuracy: 0.7120 - val_loss: 0.5764 - val_accuracy: 0.7273
Epoch 62/100
691/691 [==============================] - 0s 168us/step - loss: 0.5529 - accuracy: 0.7192 - val_loss: 0.5827 - val_accuracy: 0.7792
Epoch 63/100
691/691 [==============================] - 0s 137us/step - loss: 0.5518 - accuracy: 0.7294 - val_loss: 0.6195 - val_accuracy: 0.6753
Epoch 64/100
691/691 [==============================] - 0s 149us/step - loss: 0.5682 - accuracy: 0.7149 - val_loss: 0.6313 - val_accuracy: 0.7273
Epoch 65/100
691/691 [==============================] - 0s 137us/step - loss: 0.5620 - accuracy: 0.7091 - val_loss: 0.6196 - val_accuracy: 0.7143
Epoch 66/100
691/691 [==============================] - 0s 132us/step - loss: 0.5527 - accuracy: 0.7164 - val_loss: 0.6100 - val_accuracy: 0.7013
Epoch 67/100
691/691 [==============================] - 0s 132us/step - loss: 0.5651 - accuracy: 0.7178 - val_loss: 0.5930 - val_accuracy: 0.7792
Epoch 68/100
691/691 [==============================] - 0s 164us/step - loss: 0.5495 - accuracy: 0.7294 - val_loss: 0.5881 - val_accuracy: 0.7532
Epoch 69/100
691/691 [==============================] - 0s 155us/step - loss: 0.5485 - accuracy: 0.7221 - val_loss: 0.5748 - val_accuracy: 0.7532
Epoch 70/100
691/691 [==============================] - 0s 146us/step - loss: 0.5471 - accuracy: 0.7236 - val_loss: 0.5795 - val_accuracy: 0.7792
Epoch 71/100
691/691 [==============================] - 0s 132us/step - loss: 0.5551 - accuracy: 0.7236 - val_loss: 0.5735 - val_accuracy: 0.7273
Epoch 72/100
691/691 [==============================] - 0s 166us/step - loss: 0.5531 - accuracy: 0.7120 - val_loss: 0.6194 - val_accuracy: 0.7143
Epoch 73/100
691/691 [==============================] - 0s 136us/step - loss: 0.5617 - accuracy: 0.7221 - val_loss: 0.5996 - val_accuracy: 0.7532
Epoch 74/100
691/691 [==============================] - 0s 152us/step - loss: 0.5517 - accuracy: 0.7192 - val_loss: 0.5760 - val_accuracy: 0.7662
Epoch 75/100
691/691 [==============================] - 0s 135us/step - loss: 0.5450 - accuracy: 0.7279 - val_loss: 0.5779 - val_accuracy: 0.7662
Epoch 76/100
691/691 [==============================] - 0s 134us/step - loss: 0.5436 - accuracy: 0.7192 - val_loss: 0.5795 - val_accuracy: 0.7403
Epoch 77/100
691/691 [==============================] - 0s 150us/step - loss: 0.5467 - accuracy: 0.7308 - val_loss: 0.5861 - val_accuracy: 0.7273
Epoch 78/100
691/691 [==============================] - 0s 138us/step - loss: 0.5509 - accuracy: 0.7192 - val_loss: 0.6141 - val_accuracy: 0.7143
Epoch 79/100
691/691 [==============================] - 0s 156us/step - loss: 0.5511 - accuracy: 0.7178 - val_loss: 0.6825 - val_accuracy: 0.7013
Epoch 80/100
691/691 [==============================] - 0s 166us/step - loss: 0.5475 - accuracy: 0.7135 - val_loss: 0.5723 - val_accuracy: 0.7403
Epoch 81/100
691/691 [==============================] - 0s 134us/step - loss: 0.5489 - accuracy: 0.7178 - val_loss: 0.5725 - val_accuracy: 0.7532
Epoch 82/100
691/691 [==============================] - 0s 141us/step - loss: 0.5425 - accuracy: 0.7323 - val_loss: 0.5859 - val_accuracy: 0.7792
Epoch 83/100
691/691 [==============================] - 0s 135us/step - loss: 0.5331 - accuracy: 0.7424 - val_loss: 0.5731 - val_accuracy: 0.7792
Epoch 84/100
691/691 [==============================] - 0s 166us/step - loss: 0.5505 - accuracy: 0.7337 - val_loss: 0.5916 - val_accuracy: 0.7532
Epoch 85/100
691/691 [==============================] - 0s 152us/step - loss: 0.5414 - accuracy: 0.7366 - val_loss: 0.5806 - val_accuracy: 0.7792
Epoch 86/100
691/691 [==============================] - 0s 132us/step - loss: 0.5437 - accuracy: 0.7207 - val_loss: 0.5749 - val_accuracy: 0.7273
Epoch 87/100
691/691 [==============================] - 0s 153us/step - loss: 0.5411 - accuracy: 0.7381 - val_loss: 0.5786 - val_accuracy: 0.7403
Epoch 88/100
691/691 [==============================] - 0s 145us/step - loss: 0.5435 - accuracy: 0.7294 - val_loss: 0.5730 - val_accuracy: 0.7532
Epoch 89/100
691/691 [==============================] - 0s 146us/step - loss: 0.5280 - accuracy: 0.7381 - val_loss: 0.6085 - val_accuracy: 0.7143
Epoch 90/100
691/691 [==============================] - 0s 177us/step - loss: 0.5357 - accuracy: 0.7337 - val_loss: 0.5769 - val_accuracy: 0.7532
Epoch 91/100
691/691 [==============================] - 0s 206us/step - loss: 0.5373 - accuracy: 0.7279 - val_loss: 0.5753 - val_accuracy: 0.7662
Epoch 92/100
691/691 [==============================] - 0s 134us/step - loss: 0.5369 - accuracy: 0.7337 - val_loss: 0.5743 - val_accuracy: 0.7662
Epoch 93/100
691/691 [==============================] - 0s 139us/step - loss: 0.5374 - accuracy: 0.7453 - val_loss: 0.5829 - val_accuracy: 0.7662
Epoch 94/100
691/691 [==============================] - 0s 140us/step - loss: 0.5261 - accuracy: 0.7482 - val_loss: 0.6098 - val_accuracy: 0.7403
Epoch 95/100
691/691 [==============================] - 0s 146us/step - loss: 0.5601 - accuracy: 0.7337 - val_loss: 0.6018 - val_accuracy: 0.7403
Epoch 96/100
691/691 [==============================] - 0s 142us/step - loss: 0.5428 - accuracy: 0.7294 - val_loss: 0.5644 - val_accuracy: 0.7662
Epoch 97/100
691/691 [==============================] - 0s 139us/step - loss: 0.5265 - accuracy: 0.7352 - val_loss: 0.5589 - val_accuracy: 0.7922
Epoch 98/100
691/691 [==============================] - 0s 163us/step - loss: 0.5331 - accuracy: 0.7265 - val_loss: 0.5676 - val_accuracy: 0.7662
Epoch 99/100
691/691 [==============================] - 0s 134us/step - loss: 0.5235 - accuracy: 0.7410 - val_loss: 0.5782 - val_accuracy: 0.7792
Epoch 100/100
691/691 [==============================] - 0s 136us/step - loss: 0.5276 - accuracy: 0.7641 - val_loss: 0.5707 - val_accuracy: 0.7662
Test loss: 0.5707184231126463
Test accuracy: 0.7662337422370911
In [58]:
import matplotlib.pyplot as plt
plt.plot(epoch_data.history['accuracy'])
plt.plot(epoch_data.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
In [ ]: