Jul-27-2019, 05:32 PM
Hi all,
I was wondering if somebody could help me correct two lines in a function i have made to generate a confusion matrix. The lines in question are about reordering columns and indexes as this is required sometimes. In some instances the order of the columns in the confusion matrix need to change. Herein lies the problem: the function defined is as follows: the lines
df_cm = df_cm[[reorderCols]]
df_cm = df_cm.reindex(index=[reorderRows])
is where i get an error when i try and reorder the columns within the function itself.
order=["1","2","3","4","5","6"]
and then pass this in to the reorderCols and Rows argument.
When I run this, every argument works fine apart from reorderCols and reorderRows
The error that I receive when having as an argument reorderRows or columns is :
KeyError: "[('1', '2', '3', '4', '5', '6')] not in index"
I can't understand why this isn't working as when I do everything separately (away from a function as follows):
any help would be much appreciated!! many thanks!
I was wondering if somebody could help me correct two lines in a function i have made to generate a confusion matrix. The lines in question are about reordering columns and indexes as this is required sometimes. In some instances the order of the columns in the confusion matrix need to change. Herein lies the problem: the function defined is as follows: the lines
df_cm = df_cm[[reorderCols]]
df_cm = df_cm.reindex(index=[reorderRows])
is where i get an error when i try and reorder the columns within the function itself.
def kerasConfMat (classLabelPDindex,classLabelPDCols,reorderCols,reorderRows, actual, pred,hmcolor, xlab, ylab, fontsize, titlelab, xAxislab,yAxislab,titleFontSize, axisLabFontSize, colorbarlegend, annotatePlot): cm = confusion_matrix(actual.argmax(axis=1), pred.argmax(axis=1)) cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] df_cm = pd.DataFrame( cm, index=classLabelPDindex, columns=classLabelPDCols) df_cm = df_cm[[reorderCols]] df_cm = df_cm.reindex(index=[reorderRows]) ax = plt.axes() plot=seaborn.heatmap(df_cm, cmap=hmcolor, xticklabels=xlab, yticklabels=ylab, ax=ax, annot=annotatePlot, cbar=colorbarlegend) ax.set_title(titlelab, fontsize=titleFontSize) plt.xlabel(xAxislab, fontsize=axisLabFontSize) plt.ylabel(yAxislab, fontsize=axisLabFontSize) plot.yaxis.set_ticklabels(plot.yaxis.get_ticklabels(), rotation=0, ha='right', fontsize=fontsize) plot.xaxis.set_ticklabels(plot.xaxis.get_ticklabels(), rotation=45, ha='right', fontsize=fontsize) return plot #Dependencies import seaborn from sklearn.metrics import confusion_matrixin order to re-order the columns and index, I make an order (where the order is currently 3,2,1,5,6,4:
order=["1","2","3","4","5","6"]
and then pass this in to the reorderCols and Rows argument.
When I run this, every argument works fine apart from reorderCols and reorderRows
The error that I receive when having as an argument reorderRows or columns is :
KeyError: "[('1', '2', '3', '4', '5', '6')] not in index"
I can't understand why this isn't working as when I do everything separately (away from a function as follows):
cm = confusion_matrix(y_train[:,0:6].argmax(axis=1), trainpred[:,0:6].argmax(axis=1)) cm = pd.DataFrame( cm, index=order, columns=order) cm=cm[['1', '2', '3', '4', '5', '6']] cm=cm.reindex(index=['1', '2', '3', '4', '5', '6']) ax = plt.axes() cm=seaborn.heatmap(cm, cmap="Greens", xticklabels=ageLabelOrder, yticklabels=ageLabelOrder, annot=True, ax=ax)this works without a hitch and plots correctly with cols and rowd reordered appropriately. Is there a reason i am getting this error when it is embedded in to a custom function?
any help would be much appreciated!! many thanks!