-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheart_uci.py
74 lines (55 loc) · 1.98 KB
/
heart_uci.py
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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#These two lines deal with a bug on some mac computers.
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
#Read in the data.
data = pd.read_csv('heart.csv')
#Get all non-target columns
x = data[[c for c in data.columns if c != 'target']]
#Get JUST the target column
y = data['target']
#Establish input shape for network (needs to be a tuple - syntax thing.)
input_shape = (x.shape[1],)
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
#Make the middle section of hidden layers absurdly big:
layer_dims = [128]*20 #Set the 20 to 1 or 2 for a more appropriately-sized model
#Initialize the model.
model = Sequential()
#Add layers.
model.add(Dense(64, input_shape=input_shape, activation = 'relu'))
#Layers can be added via loops!
for l in layer_dims:
model.add(Dense(l, activation = 'relu'))
model.add(Dense(64, activation = 'relu'))
#Final layer should be 1 output node;
#Sigmoid forces predictions to be between 0 and 1
model.add(Dense(1, activation = 'sigmoid'))
#Create the optimizer:
adam = Adam(lr=0.0001)
#Compile the model. Binary Crossentropy is the best loss for binary classification.
model.compile(loss = 'binary_crossentropy', optimizer = 'adam')
#PS: For more classes, you'd use 'categorical-crossentropy' - look it up!
#Get a summary of the compiled model!
#Commented out for .py version.
#model.summary()
#Finally, fit the model to the data - and store the metrics in a history variable
history = model.fit(x, y, batch_size = 64, epochs = 100, shuffle = True)
#Save the model!
model.save('heart_uci_siri')
#View loss over time - should be decreasing.
plt.plot(history.history['loss'])
plt.title('Training Loss over Time')
plt.show()
model = keras.models.load_model('heart_uci_siri')
y_pred = model.predict(x)
plt.plot(y_pred, 'o')
plt.title('Unrounded Predictions')
plt.show()
plt.plot(np.round(y_pred), 'o')
plt.title('Predictions w/ Round')
plt.show()