Jul-28-2019, 08:30 AM
(Jul-27-2019, 08:01 PM)ThomasL Wrote: check what reorderRows is in your first code example
just by inserting a print(reorderRows) before that line
df_cm = df_cm.reindex(index=[reorderRows])Looking at your second code example
cm=cm.reindex(index=['1', '2', '3', '4', '5', '6'])reorderRows must be the same as '1', '2', '3', '4', '5', '6' but if you assign it like this
reorderRows = '1', '2', '3', '4', '5', '6'reorderRows is of type tuple ('1', '2', '3', '4', '5', '6')
and index=['1', '2', '3', '4', '5', '6'] is not the same as index=[('1', '2', '3', '4', '5', '6')]
which i assume you get printed out using print(reorderRows) as suggested because the error you get
KeyError: "[('1', '2', '3', '4', '5', '6')] not in index"
says exactly this
Thank you so much!! I am very sorry but i feel like i am doing something so wrong! I get your answer but i continue to get an error.. firstly, print(reorderRows in the first column, does not print the output after execution (snippet of code):
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]] [b] print(reorderRows) [/b] df_cm = df_cm.reindex(index=reorderRows)After execution I simply get the same error but not output of what reorderRows is:
Traceback (most recent call last): File "/Users/jassim01/.virtualenvs/DeannaNN/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3296, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-128-ada69537581b>", line 5, in <module> titleFontSize=18, axisLabFontSize=16, colorbarlegend=False, annotatePlot=False) File "<ipython-input-126-98aff873c3b0>", line 10, in kerasConfMat df_cm = df_cm[[reorderCols]] File "/Users/jassim01/.virtualenvs/DeannaNN/lib/python3.7/site-packages/pandas/core/frame.py", line 2682, in __getitem__ return self._getitem_array(key) File "/Users/jassim01/.virtualenvs/DeannaNN/lib/python3.7/site-packages/pandas/core/frame.py", line 2726, in _getitem_array indexer = self.loc._convert_to_indexer(key, axis=1) File "/Users/jassim01/.virtualenvs/DeannaNN/lib/python3.7/site-packages/pandas/core/indexing.py", line 1327, in _convert_to_indexer .format(mask=objarr[mask])) KeyError: "[('p3', 'p15', 'p30', 'p60', 'p180', 'p365')] not in index"Secondly, thank you for the clarification that does indeed make perfect sense! I feel like the solution was as follows:
Remove [] from the function so that the argument becomes:
df_cm = df_cm[[reorderCols]] print(reorderRows) df_cm = df_cm.reindex(index=reorderRows)then define order=['1','2','3','4','5','6']
whose output =
Out[129]: ['1', '2', '3', '4', '5', '6']
This should make the format of of the function after inserting order in the argument:
df_cm = df_cm.reindex(index=['1', '2', '3', '4', '5', '6'])Which should be in the correct format, however, i continue to get the same error:
KeyError: "[('p3', 'p15', 'p30', 'p60', 'p180', 'p365')] not in index"
I am really confused as to the specific issue here!