There is a dataframe "df" containing data about texts in the following format: Category Message Length Words 0 ham Go until jurong point, crazy... Available only ... 111 [go, jurong, point, crazy, available, bugis, g... 1 ham Ok lar... Joking wif u oni... 29 [ok, lar, joking, wif, oni] The texts in it are in the df['Message'] variable. And the classification categories are in df['Category']. The programmer has written code which should contain all the source messages classified incorrectly (specifying the actual and predicted ones). However, this code does not work and generates an error: KeyError. Here is this code: misclassified = [] for index, label_predicted in enumerate(predicted): if label_predicted != y_test[index]: misclassified.append({'message': df.iloc[index]['Message'], 'actual': df.iloc[index]['Category'], 'predicted': label_predicted}) misclassification_df = pd.DataFrame(misclassified) Fix it!
def misclassified_messages(y_test, predicted): misclassified = [] for index, label_predicted in enumerate(predicted): if label_predicted != y_test[index]: misclassified.append({'message': df.iloc[index]['Message'], 'actual': df.iloc[index]['Category'], 'predicted': label_predicted}) misclassification_df = pd.DataFrame(misclassified) return misclassification_df