1

I get this error:

AttributeError: 'NumpyArrayIterator' object has no attribute 'classes'

I am trying to make a confusion matrix to evaluate the Neural Net I have trained. I am using ImageDatagenerator and datagen.flow functions for before the fit_generator function for training.

For predictions I use the predict_generator function on the test set. All is working fine so far. Issue arrises in the following:

test_generator.reset()
pred = model.predict_generator(test_generator, steps=len(test_generator), verbose=2)

from sklearn.metrics import classification_report, confusion_matrix, cohen_kappa_score

y_pred = np.argmax(pred, axis=1)

print('Confusion Matrix')
print(pd.DataFrame(confusion_matrix(test_generator.classes, y_pred)))

I should be seeing a confusion matrix but instead I see an error. I ran the same code with sample data before I ran on the actual dataset and that did show me the results.

2
  • If I understand well, you want to make confusion_matrix of test_generator classes and predicted (y_pred) classes? Commented Oct 30, 2019 at 13:12
  • Yes please. Confusion matrix of truth and predicted values. Commented Oct 30, 2019 at 22:40

1 Answer 1

1

First you need to extract labels from generator and then put them in confusion_matrix function.
To extract labels use x_gen,y_gen = test_generator.next(), just pay attention that labels are one hot encoded.

Example:

test_generator.reset()
pred = model.predict_generator(test_generator, steps=len(test_generator), verbose=2)

from sklearn.metrics import classification_report, confusion_matrix, cohen_kappa_score

y_pred = np.argmax(pred, axis=1)

x_gen,y_gen = test_generator.next()
y_gen = np.argmax(y_gen, axis=1)

print('Confusion Matrix')
print(pd.DataFrame(confusion_matrix(y_gen, y_pred)))
Sign up to request clarification or add additional context in comments.

1 Comment

That gives me the following error: ValueError: Found input variables with inconsistent numbers of samples: [32, 8871] y_gen should also be 8871.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.