sakeesh
sakeesh

Reputation: 1039

TypeError: object of type 'numpy.int64' has no len() / TypeError: object of type 'int' has no len()/ while using classification_report in scikitlearn

Sometimes when using sklearn.metrics.classification_report we get below error

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

Sometime depending on the datatype we get error like below too

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

Sample code when we might get this error

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))

Upvotes: 5

Views: 2900

Answers (1)

sakeesh
sakeesh

Reputation: 1039

This happens when target_names used are not string; to solve did a conversion like below on the target_names variable

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))

Upvotes: 4

Related Questions