【问题标题】:how to fix the values displayed in a confusion matrix in exponential form to normal form如何以指数形式将混淆矩阵中显示的值修复为正常形式
【发布时间】:2020-05-12 09:38:48
【问题描述】:

在处理我的项目时,我从测试数据中获得了一个混淆矩阵:

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cm

输出为:

array([[1102,   88],
   [  85,  725]], dtype=int64)

使用 seaborn 和 matplotlib,我使用代码对其进行了可视化:

import seaborn as sns
import matplotlib.pyplot as plt     

ax= plt.subplot();
sns.heatmap(cm, annot=True,cmap='Blues',ax=ax);
# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels'); 
ax.set_ylim(2.0, 0)
ax.set_title('Confusion Matrix');
ax.xaxis.set_ticklabels(['Fake','Real']); 
ax.yaxis.set_ticklabels(['Fake','Real']);

得到的输出是:

Confusion matrix

问题是 3 位数字(这里 1102 显示为 11e+03)或以上的值以指数形式显示。

有没有办法以正常形式显示它?

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    您可以使用"fmt" 选项:

    cm = np.array([[1102,   88],[85,  725]])
    
    import seaborn as sns
    import matplotlib.pyplot as plt     
    
    sns.heatmap(cm, annot=True,fmt="d",cmap='Blues')
    

    【讨论】:

      【解决方案2】:
      import matplotlib.pyplot as plt
      import seaborn as sns
      
      cm = confusion_matrix(y_test, y_pred)
      cm_df = pd.DataFrame(cm,
                       index = [0, 1], 
                       columns = [0, 1])
      plt.figure(figsize=(10,5))
      sns.heatmap(cm_df, annot=True)
      plt.title('Confusion Matrix')
      plt.ylabel('Actal Values')
      plt.xlabel('Predicted Values')
      plt.show()
      

      【讨论】:

        【解决方案3】:

        通过添加 fmt="d" 参数起作用。

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 2021-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-21
        相关资源
        最近更新 更多