python中函数中两行的问题
我想知道是否有人能帮我纠正一个生成混淆矩阵的函数中的两行。有问题的行是关于对列和索引重新排序的,因为这有时是必需的。在某些情况下,混淆矩阵中列的顺序需要更改。问题就在这里:定义的函数如下:行python中函数中两行的问题,python,Python,我想知道是否有人能帮我纠正一个生成混淆矩阵的函数中的两行。有问题的行是关于对列和索引重新排序的,因为这有时是必需的。在某些情况下,混淆矩阵中列的顺序需要更改。问题就在这里:定义的函数如下:行 df_cm = df_cm[[reorderCols]] df_cm = df_cm.reindex(index=[reorderRows]) 当我尝试对函数本身中的列重新排序时,会出现错误 def kerasConfMat (classLabelPDindex,classLabelPDCols,reor
df_cm = df_cm[[reorderCols]]
df_cm = df_cm.reindex(index=[reorderRows])
当我尝试对函数本身中的列重新排序时,会出现错误
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
为了对列和索引重新排序,我制定了一个顺序(当前的顺序是3,2,1,5,6,4:
order=["1","2","3","4","5","6"]
然后将其传递给reorderCols
和行
参数。
当我运行它时,除了reorderCols
和reordrows
将ReordRows或columns作为参数时,我收到的错误是:
KeyError:“[('1','2','3','4','5','6')]不在索引中”
我不明白为什么这不能像我单独做每件事时那样工作(远离下面的函数):
这可以顺利地工作,并正确地打印,cols和rowd被适当地重新排序。当它嵌入到自定义函数中时,是否有原因导致此错误
任何帮助都将不胜感激!!非常感谢!检查您的第一个代码示例中的reorderRows是什么 只需在该行之前插入一个print(reordrows)
df_cm = df_cm.reindex(index=[reorderRows])
看看第二个代码示例
cm=cm.reindex(index=['1', '2', '3', '4', '5', '6'])
reorderRows必须与“1”、“2”、“3”、“4”、“5”、“6”相同,但如果您这样分配它
reorderRows = '1', '2', '3', '4', '5', '6'
reorderRows的类型为tuple('1','2','3','4','5','6')
而index=['1','2','3','4','5','6']与index=[('1','2','3','4','5','6')不同]
我假设您按照建议使用print(reordrows)打印出来,因为您得到的错误
KeyError:“[('1','2','3','4','5','6')]不在索引中”
正是这样说的
reorderRows = '1', '2', '3', '4', '5', '6'