I'm using neural network machine learning and would like to see the result of my confusion matrix for my model. However, there is an error that I've got and don't know how to solve it.

from sklearn.metrics import confusion_matrix
cm=confusion_matrix(testY, testPredict)

then it give an error stated: Found input variables with inconsistent numbers of samples: [30, 24]

actually I've check the shape of the test and the prediction value and it shows the different shape. How can I make the shape of the test and prediction become same ?

the shape of test and prediction value


1 Answer 1


I had the same problem, here is how I sloved it:

testX is a tf.data.Dataset, I guess. In that case try the following:

# make the prdictions from your test set:
testPredictRaw = self.model.predict(testX)  
testPredict = np.argmax(testPredictRaw, axis=1)

# Then, take all the y values from the prefetch dataset (thus changing the shape):
trueClasses = tf.concat([y for x, y in testY], axis=0)

# Calculate the confusion matrix using sklearn.metrics
cm = metrics.confusion_matrix(trueClasses, testPredict)

I used these answered questions to make it work for my case:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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