【发布时间】:2020-11-22 15:02:28
【问题描述】:
错误:
TypeError: object of type 'numpy.int64' has no len()
代码:
import numpy as np
def apk(actual, predicted, k=10):
if len(predicted)>k: # here is the error
predicted = predicted[:k]
score = 0.0
num_hits = 0.0
for i,p in enumerate(predicted):
if p in actual and p not in predicted[:i]:
num_hits += 1.0
score += num_hits / (i+1.0)
if not actual:
return 1.0
return score / min(len(actual), k)
def mapk(actual, predicted, k=10):
print(type(predicted))
>>> <class 'list'> # here is predicted also from the value list
return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)])
x = list(df_new['id'].to_numpy())
y = list(re['id'].to_numpy())
print(type(y))
>>> <class 'list'>
# So y is a list and not like later 'numpy.int64'
mapk(x,y)
当我开始以下调用时,我收到以下错误,我该如何解决?
当我执行以下操作时,我得到长度 len(y)
我该如何解决这个错误?我非常期待答案。
【问题讨论】:
-
predicted总是 是一个列表吗?如果您只预测一个值,那么预测的内容是否仍然会产生一个只有一个值的列表,还是只返回该值?根据您在此处提供的信息,我们无法重现,因此除了做出这样的疯狂假设之外,我们也无能为力。
标签: python list numpy dataframe