2
$\begingroup$

I intend to display a confusion matrix using Keras while K-fold of scikit-learn. My code using Keras is:

import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense

seed = 7
numpy.random.seed(seed)

# load dataset
dataframe = pandas.read_csv("BolMov.csv", header=None)
dataset = dataframe.values
X = dataset[:,0:24].astype(float)
Y = dataset[:,24]

model = Sequential()
model.add(Dense(16, activation='relu'))
model.add(Dense(7, activation='softmax')
model.compile('adam', 'categorical_crossentropy', metrics=['accuracy'])

y_cat = to_categorical(Y)
result = model.fit(X, y_cat, verbose=0, epochs=50)

plot_loss_accuracy(result)

y_pred = model.predict_classes(X, verbose=0)

print(classification_report(y, y_pred))
plot_confusion_matrix(model, X, y)

How should I use kfold in this code? Here the author is calling a function. I believe that if I do that in my code, the model.fit() will be executed twice - once for my Keras code and another time (internally) for the KerasClassifier(). I would like model.fit() to only execute once. Help from anyone is appreciated.

$\endgroup$
2
  • $\begingroup$ don't call model.fit() as you currently do in your code but instead wrap your model in a KerasClassifier() and apply KFold() to it $\endgroup$
    – pcko1
    Commented Feb 16, 2019 at 23:58
  • $\begingroup$ @pcko1 Can I write like this: result = KerasClassifier(build_fn=baseline_model, epochs=200, batch_size=5, verbose=0) and then plot_loss_accuracy(result) so that result can be used for kfold validation of scikit-learn as well as confusion matrix display of Keras? $\endgroup$
    – PS Nayak
    Commented Feb 18, 2019 at 14:08

1 Answer 1

1
$\begingroup$

KFold() is meant for cross-validation purpose where multiple models are created over the subsets of the entire dataset and discarded after the validation procedure is over. So the model.fit() should be called explicitly to create the model for purpose. Both the tasks can be easily done through the wrapper named KerasClassifier() by packaging all the details of the model design.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.