Python Forum
Help with two lines in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with two lines in a function
#1
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.


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_matrix
in 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!
Reply


Messages In This Thread
Help with two lines in a function - by amjass12 - Jul-27-2019, 05:32 PM
RE: Help with two lines in a function - by ThomasL - Jul-27-2019, 08:01 PM
RE: Help with two lines in a function - by amjass12 - Jul-28-2019, 08:30 AM
RE: Help with two lines in a function - by ThomasL - Jul-28-2019, 08:50 AM
RE: Help with two lines in a function - by amjass12 - Jul-28-2019, 09:33 AM
RE: Help with two lines in a function - by ThomasL - Jul-28-2019, 09:47 AM
RE: Help with two lines in a function - by amjass12 - Jul-28-2019, 12:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Importing a function from another file runs the old lines also dedesssse 6 2,598 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,922 Aug-10-2020, 11:01 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020