python create simple MLP in Keras

description: create simple MLP in Keras

import packages

1
2
3
from keras.models import Sequential
from keras.layers import Dense
import numpy

fix random seed for reproducibility

1
numpy.random.seed(7)

load pima indians dataset

1
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")

split into input (X) and output (Y) variables

1
2
X = dataset[:,0:8]
Y = dataset[:,8]

create model

1
2
3
4
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

Compile model

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

Fit the model

1
model.fit(X, Y, epochs=150, batch_size=10)

evaluate the model

1
2
scores = model.evaluate(X, Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))