-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathL4 PID w Keras Wrappers.py
More file actions
42 lines (35 loc) · 1.44 KB
/
L4 PID w Keras Wrappers.py
File metadata and controls
42 lines (35 loc) · 1.44 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
## using KerasClassifier to wrap deep learning models
#import libraries
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.cross_validation import StratifiedKFold
from sklearn.cross_validation import cross_val_score
import numpy
import pandas
#define a function which creates a model.
#KerasClassifier requires this function to be passed in as argument
def create_model():
#create model
model = Sequential()
# 8 input - 12 neurons hidden layer 1 - 8 neurons in hidden layer 2 - 1 output
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'])
return model
#fix seed
seed = 7
numpy.random.seed(seed)
#load pima indians dataset
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 = KerasClassifier(build_fn=create_model, nb_epoch = 150, batch_size = 10)
#evaluate using 10-fold cross validation
kfold = StratifiedKFold(y=Y, n_folds=10, shuffle=True, random_state=seed)
results = cross_val_score(model, X, Y ,cv = kfold)
print(results.mean())