Sklearn-绘图分类报告提供的输出与基本平均值不同? [英] Sklearn - plotting classification report gives a different output than basic avg?

查看:115
本文介绍了Sklearn-绘图分类报告提供的输出与基本平均值不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用此答案如何绘制scikit学习分类报告? / a>将sklearn分类报告转换为热图。

I wanted to leverage this answer How to plot scikit learn classification report? turning an sklearn classification report into a heatmap.

它们都与示例报告一起使用,但是我的分类报告看起来略有不同,因此使功能更加混乱。

It's all working with their sample report, however my classification report looks slightly different and is thus screwing up the functions.

他们的报告(注意平均/总计):

Their report (notice the avg / total):

sampleClassificationReport =             
                   precision    recall  f1-score   support

          Acacia        0.62      1.00      0.76        66
          Blossom       0.93      0.93      0.93        40
          Camellia      0.59      0.97      0.73        67
          Daisy         0.47      0.92      0.62       272
          Echium        1.00      0.16      0.28       413

        avg / total     0.77      0.57      0.49       858

我的报告具有 metrics.classification_report(valid_y,y_pred)

              precision    recall  f1-score   support

           0       1.00      0.18      0.31        11
           1       0.00      0.00      0.00        14
           2       0.00      0.00      0.00        19
           3       0.50      0.77      0.61        66
           4       0.39      0.64      0.49        47
           5       0.00      0.00      0.00        23

    accuracy                           0.46       180
   macro avg       0.32      0.27      0.23       180
weighted avg       0.35      0.46      0.37       180

问题来自于热图链接中的选定答案,

The issue, from the selected answer in the heatmap link, is here:

for line in lines[2 : (len(lines) - 2)]:
    t = line.strip().split()
    if len(t) < 2: continue
    classes.append(t[0])
    v = [float(x) for x in t[1: len(t) - 1]]
    support.append(int(t[-1]))
    class_names.append(t[0])
    print(v)
    plotMat.append(v)

因为出现错误:


ValueError:无法将字符串转换为float:'avg'

ValueError: could not convert string to float: 'avg'

所以真正的问题是我的分类报告是如何输出的。我可以在此处更改以匹配示例吗?

So the problem truly is how my classification report is being outputted. What can I change here to match the sample?

编辑:我尝试过的操作:

what Ive tried:

df = pd.DataFrame(metrics.classification_report(valid_y, y_pred)).T

df['support'] = df.support.apply(int)

df.style.background_gradient(cmap='viridis',
                             subset=pd.IndexSlice['0':'9', :'f1-score'])

错误:


ValueError:DataFrame构造函数不正确

ValueError: DataFrame constructor not properly called!


推荐答案

随着 output_dict classification_report ,解析报表没有麻烦。您可以直接使用分类报告的输出读取为 pd.DataFrame 。然后,您可以使用 pd。样式 选项以渲染热图。

With the advent of output_dict param in classification_report, there is no hassle for parsing the report. You can directly use the output of classification report to be read as pd.DataFrame. Then, you could use the pd.Style option to render the heat map.

示例:

from sklearn.metrics import classification_report
import numpy as np
import pandas as pd

from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split, GridSearchCV


X, y = make_classification(n_samples=1000, n_features=30,
                           n_informative=12,
                           n_clusters_per_class=1, n_classes=10,
                           class_sep=2.0, random_state=42)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, stratify=y)


clf = LogisticRegression(max_iter=1000, random_state=42).fit(X_train, y_train)



df = pd.DataFrame(classification_report(clf.predict(X_test), 
                                        y_test, digits=2,
                                        output_dict=True)).T

df['support'] = df.support.apply(int)

df.style.background_gradient(cmap='viridis',
                             subset=pd.IndexSlice['0':'9', :'f1-score'])

这篇关于Sklearn-绘图分类报告提供的输出与基本平均值不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆