Created
July 6, 2018 05:19
-
-
Save spicyramen/7ff0081666c7ac55a90904ddde7c60dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MLP for Pima Indians Dataset with 10-fold cross validation | |
from keras.models import Sequential | |
from keras.layers import Dense | |
from sklearn.model_selection import StratifiedKFold | |
import numpy | |
# fix random seed for reproducibility | |
seed = 7 | |
numpy.random.seed(seed) | |
# load pima indians dataset | |
FILENAME = '../data/pima-indians-diabetes.csv' | |
dataset = numpy.loadtxt(FILENAME, delimiter=',') | |
# split into input (X) and output (Y) variables | |
X = dataset[:, 0:8] | |
Y = dataset[:, 8] | |
# define 10-fold cross validation test harness | |
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed) | |
cvscores = [] | |
for train, test in kfold.split(X, Y): | |
# create model | |
model = Sequential() | |
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu')) | |
model.add(Dense(8, kernel_initializer='uniform', activation='relu')) | |
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid')) | |
# Compile model | |
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model | |
model.fit(X[train], Y[train], nb_epoch=150, batch_size=10, verbose=0) | |
# evaluate the model | |
scores = model.evaluate(X[test], Y[test], verbose=0) | |
print("%s: %.2f%%" % (model.metrics_names[1], scores[1] * 100)) | |
cvscores.append(scores[1] * 100) | |
print("%.2f%% (+/- %.2f%%)" % (numpy.mean(cvscores), numpy.std(cvscores))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment