Python 得到最坏的多类分类预测类

Python 得到最坏的多类分类预测类,python,scikit-learn,confusion-matrix,Python,Scikit Learn,Confusion Matrix,我正在研究一个多类分类问题,我有很多不同的类(50+) 问题是,我想突出显示预测最差的类(例如,在混淆矩阵或其他什么中),以便在分类器中进行进一步调整 我的预测和测试数据保存在列表中(来自sklearn的小示例): 如何从矩阵中获取本例中的类1?在那里,预测是完全错误的。有没有一种方法可以根据类的真实积极预测对类进行排序?您可以使用scikit learn,它将返回一个字典,其中包含精度、召回率和F分数。然后,您可以按排序方式打印字典,以便轻松查看最坏的预测类 #prints classific

我正在研究一个多类分类问题,我有很多不同的类(50+)

问题是,我想突出显示预测最差的类(例如,在混淆矩阵或其他什么中),以便在分类器中进行进一步调整

我的预测和测试数据保存在列表中(来自sklearn的小示例):

如何从矩阵中获取本例中的类1?在那里,预测是完全错误的。有没有一种方法可以根据类的真实积极预测对类进行排序?

您可以使用scikit learn,它将返回一个字典,其中包含精度、召回率和F分数。然后,您可以按排序方式打印字典,以便轻松查看最坏的预测类

#prints classification_report     
print(classification_report(y_true, y_pred)

#returns a dict, which you can easily sort by prediction
report = classification_report(y_true, y_pred, output_dict=True)

您可以为此使用一个简单的函数:

def print_class_accuracies(confusion_matrix):
    # get the number of occurrences for each class
    counts = {cl: y_true.count(cl) for cl in set(y_true)}
    # extract the diagonal values (true positives)
    tps = dict(enumerate(conf.diagonal()))
    # Get the accuracy for each class, preventing ZeroDivisionErrors
    pred_accuracy = {cl: tps[cl]/counts.get(cl, 1) for cl in tps}
    # Get a ranking, worst accuracies are first/lowest
    ranking = sorted([(acc,cl) for cl, acc in pred_accuracy.items()])
    # Pretty print it
    for acc, cl in ranking:
        print(f"Class {cl}: accuracy: {acc:.2f}")
def print_class_accuracies(confusion_matrix):
    # get the number of occurrences for each class
    counts = {cl: y_true.count(cl) for cl in set(y_true)}
    # extract the diagonal values (true positives)
    tps = dict(enumerate(conf.diagonal()))
    # Get the accuracy for each class, preventing ZeroDivisionErrors
    pred_accuracy = {cl: tps[cl]/counts.get(cl, 1) for cl in tps}
    # Get a ranking, worst accuracies are first/lowest
    ranking = sorted([(acc,cl) for cl, acc in pred_accuracy.items()])
    # Pretty print it
    for acc, cl in ranking:
        print(f"Class {cl}: accuracy: {acc:.2f}")