TypeError: object of type 'numpy.int64' has no len() TypeError: object of type 'int' has no len() 在使用scikit-learn中的classification_report时发生错误。

5
有时在使用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))
1个回答

4
这种情况发生在使用的 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))

HuggingFace中使用metric.load('bertscore')的解决方案相同。 - Alaa M.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接