How to switch the position of values in a numpy array

Viewed 28

I need some help in switching the position of the confusion matrix as shown in the image below.enter image description here.

The code below is the current version whereby the true positive is at the bottom right. Appreciate the help. Thanks in advance.

from sklearn.metrics import confusion_matrix

#data_swn['Truth'] = data_swn['Truth'].astype(str)
cf_matrix3 = confusion_matrix(data_swn['SWN_Sentiment'], data_swn['Truth'])

cf_matrix3

Ouput:enter image description here

labels = ['True Neg','False Pos','False Neg','True Pos']
Group_percentages = ['{0:.2%}'.format(value) for value in cf_matrix1.flatten() / np.sum(cf_matrix1)]

labels = [f'{v1}\n{v2}' for v1, v2 in zip(labels,Group_percentages)]
labels = np.asarray(labels).reshape(2,2)

ax = sns.heatmap(cf_matrix1, annot=labels, fmt='', cmap='Blues')

ax.set_title('Seaborn Confusion Matrix with labels\n\n');
ax.set_xlabel('\nPredicted Values')
ax.set_ylabel('Actual Values ');

## Ticket labels - List must be in alphabetical order
ax.xaxis.set_ticklabels(['False','True'])
ax.yaxis.set_ticklabels(['False','True'])

## Display the visualization of the Confusion Matrix.
plt.show()

Ouput:enter image description here

1 Answers

Seems like the best way is to reverse your data (in both dimensions, to match the desired order) before you plot it:

cf_matrix1 = np.array([[836,1651], [219,3411]])
cf_matrix1 = cf_matrix1[::-1,::-1]

Then you also need to change your label order:

labels = ['True Pos', 'False Pos', 'False Neg', 'True Neg']

Rest of your code is the same, although you probably want to change the x- and y-labels.

Related