【问题标题】:TypeError: object of type 'numpy.int64' has no len() / TypeError: object of type 'int' has no len()/ while using classification_report in scikitlearnTypeError: 'numpy.int64' 类型的对象没有 len() / TypeError: 'int' 类型的对象没有 len()/ 在 scikitlearn 中使用分类报告时
【发布时间】:2021-02-26 00:44:14
【问题描述】:

有时在使用 sklearn.metrics.classification_report 时会出现以下错误

TypeError: object of type 'int' has no len()

有时取决于数据类型,我们也会收到如下错误

TypeError: object of type 'numpy.int64' has no len()

可能出现此错误时的示例代码

t=pd.Series([1,2,3,4])
p=np.asarray([1,2,3,4])
target_names=[1,2,3,4]
print(classification_report(t, p, target_names=target_names))

【问题讨论】:

    标签: python-3.x scikit-learn metrics


    【解决方案1】:

    target_names 使用的不是字符串时会发生这种情况;解决在target_names 变量上进行如下转换

    t=pd.Series([1,2,3,4])
    p=np.asarray([1,2,3,4])
    target_names=[1,2,3,4]
    target_names=list(map(str,target_names))
    print(classification_report(t, p, target_names=target_names))
    

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 2022-12-14
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多