0

I am trying to create the confusion matrix but I don't understand what the problem is. Is it possible that I input incompatible things?

score = model.evaluate(X_test, y_test, verbose=1)

print("Test Score:", score[0])
print("Test Accuracy:", score[1])
# %%

y_pred = model.predict(X_test) > 0.4
cm = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
print(cm)
#plt.show(cm)
cm_df = pd.DataFrame(cm, index = ['aid_related','other_aid','request','weather_related','direct_report','infrastructure_related','medical','primary_needs'], columns = ['primary_needs','medical','infrastructure_related','direct_report','weather_related','request','other_aid','aid_related'])
plt.figure(figsize=(5,4))
sns.heatmap(cm_df, annot=True)
plt.title('Confusion Matrix')
plt.ylabel('Actal Values')
plt.xlabel('Predicted Values')
plt.show()
#print(preds)
#result = pd.DataFrame(preds, columns=[["aid_related","other_aid","request","weather_related","direct_report","infrastructure_related","medical","primary_needs"]])
#result.head(50)

Il messaggio di errore completo è il seguente

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
c:\Users\kaka0\Desktop\distaster_messages\Untitled-1 (1).py in <module>
      156 cm = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
      157 print(cm)
----> 158 plt.show(cm)
      159 '''
      160 cm_df = pd.DataFrame(cm, index = ['aid_related','other_aid','request','weather_related','direct_report','infrastructure_related','medical','primary_needs'], columns = ['primary_needs','medical','infrastructure_related','direct_report','weather_related','request','other_aid','aid_related'])

~\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pyplot.py in show(*args, **kwargs)
    376     """
    377     _warn_if_gui_out_of_main_thread()
--> 378     return _backend_mod.show(*args, **kwargs)
    379 
    380 

~\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib_inline\backend_inline.py in show(close, block)
     47         # only call close('all') if any to close
     48         # close triggers gc.collect, which can be slow
---> 49         if close and Gcf.get_all_fig_managers():
     50             matplotlib.pyplot.close('all')
     51 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


Why is it always giving me this error?

  • Please post the full error message - including the portion where it says which line is causing the error. – Mortz Oct 06 '21 at 09:21
  • Possible duplicate https://stackoverflow.com/questions/22175489/numpy-valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambi – abdou_dev Oct 06 '21 at 09:45

1 Answers1

0

The y_true are the true labels, the predicted labels are saved in the variable predictions. X-test are the input labels the model has to predict on. Next we import confusion_matrix from sklearn and give it the y_true and y_pred values. Last we put in the output of the confusion matrix into the heatmap, you can modify the heatmap yourself and choose if you want to save it or not.

Note in this case I have used male and female for my x and y labels, you have to adjust this yourself.

from sklearn.metrics import confusion_matrix 

predictions = model.predict(X_test)

y_pred = np.round(predictions, 0)
y_true = y_true

confusion = np.round(confusion_matrix(y_true, y_pred, normalize='true'),2)

import seaborn as sb
from matplotlib.pyplot import figure
x_axis_labels = ['Female', 'Male']
y_axis_labels = ['Female', 'Male']
figure(figsize = (5,5))

sb.heatmap(confusion, xticklabels = x_axis_labels, yticklabels = y_axis_labels,  cmap= "Blues", linecolor = 'black' , linewidth = .1 , annot = True, fmt='',)
plt.savefig('confusion_matrix_test.png')
plt.show()