-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTutorial_1_Medical_data.py
More file actions
112 lines (90 loc) · 2.56 KB
/
Tutorial_1_Medical_data.py
File metadata and controls
112 lines (90 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import sys; print('Python %s on %s' % (sys.version, sys.platform))
# make sure to connect via 'xterm -ls -xrm 'XTerm*selectToClipboard: true'&
import os
# set the environment variable
os.environ["KERAS_BACKEND"] = "tensorflow"
# make sure we were successful
print(os.environ.get("KERAS_BACKEND"))
# set preloading
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
# Create first network with Keras
from keras.models import Sequential
from keras.layers import Dense
import numpy
import urllib
from sklearn.metrics import confusion_matrix
from os.path import expanduser
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima dataset
nr_epochs = 200
dataset = numpy.loadtxt('pima-indians-diabetes.csv', delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# Fit the model
history = model.fit(
X,
Y,
epochs=nr_epochs,
batch_size=10,
verbose=2)
# Evaluate the network
loss, accuracy = model.evaluate(X, Y)
print("\nLoss: %.2f, Accuracy: %.2f%%" % (loss, accuracy*100))
# calculate predictions
probabilities = model.predict(X)
predictions = [float(x>0.5) for x in probabilities]
## visualizations WORK ONLY VIA 'ssh -XC' and 'xterm python' or locally on a laptop
# loss function
import gnuplotlib as gpl
loss = numpy.array(history.history['loss'])
epochs = numpy.array(range(1, len(loss) + 1))
gpl.plot((epochs, loss),
_with='dots',
title='Training loss',
legend='Training loss',
xlabel='Epochs',
ylabel='Loss',
unset='grid',
terminal='dumb 120,30')
# accuracy
acc = numpy.array(history.history['acc'])
gpl.plot((epochs, acc),
_with='dots',
title='Training accuracy',
legend='Training acc',
xlabel='Epochs',
ylabel='Accuracy',
unset='grid',
terminal='dumb 120,30')
#
cm = confusion_matrix(Y, predictions)
tp = float(cm[1,1])
fp = float(cm[0,1])
tn = float(cm[0,0])
fn = float(cm[1,0])
print ("True positives: %.0f" % tp)
print ("False positives: %.0f" % fp)
print ("True negatives: %.0f" % tn)
print ("False negatives: %.0f" % fn)
prec = tp/(tp+fp)
rec = tp/(tp+fn)
f1 = (2*prec*rec)/(prec+rec)
print ("Precision: %.3f" % prec)
print ("Recall: %.3f" % rec)
print ("F1: %.3f" % f1)