Chapter 5: Improving Model Accuracy
Activity 8: Weight Regularization on a Diabetes Diagnosis Classifier
Solution:
- Load the dataset and split the dataset into a training set and a test set:
# Load The dataset
import numpy
data = numpy.loadtxt(“./data/pima-indians-diabetes.csv”, delimiter=”,”)
X = data[:,0:8]
y = data[:,8]
# Split the dataset into training set and test set with a 0.7-0.3 ratio
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
- Define a Keras model with two hidden layers of size eight to perform the classification. Train the model and plot the trends in training error and test error:
# define a seed for random number generator so the result will be reproducible
import numpy
seed = 1
numpy.random.seed(seed)
# define the keras model
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(8, input_dim=8, activation=’...