【问题标题】:'numpy.int64' has no len()'numpy.int64' 没有 len()
【发布时间】: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


【解决方案1】:

您在这一行中遍历predicted:

return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)])

predicted 是一个列表,但p 只是该列表的一个元素。

在apk 的范围内,predicted 是 p(不是来自mapk 的predicted)——因此显然是int64。

【讨论】:

    【解决方案2】:

    如何调试你的代码:

    根据您提供的信息,问题是predicted 的类型是numpy.int64。而int 确实没有 len。我想predicted 应该是list ?

    尝试在出现错误的行之前打印predicted 的类型:

    def apk(actual, predicted, k=10):
        print(type(predicted)) # Add this code
        if len(predicted)>k: # here is the error
    

    错误出现在apk函数中。

    导致错误的行:

    @Daniel F 在他的回答中解释了为什么predicted 不是list 类型:

    因为您作为参数传递给apk 函数的名为predicted 的参数不是predicted 函数中的变量predicted。实际上,您将变量p 传递给apk,而不是名为predicted 的list。 而且,从错误消息中,我们推断出p 的类型为numpy.64。

    这是您将p 作为参数predicted 的参数传递给您的函数apk 的行:[apk(a,p,k) for a,p in zip(actual, predicted)]

    如果您将此代码更改为此代码(我并不是说您应该这样做,因为根据您提供的信息,我们只能做出假设):

    [apk(a,predicted,k) for a,p in zip(actual, predicted)]
    

    然后,您不会传递p,而是将predicted 作为名为@9​​87654348@ 的参数的参数传递给您的函数apk。

    详细解释发生了什么:

    我会深入,如果它看起来太夸张了我的借口。

    我将在这里解释[apk(a,predicted,k) for a,p in zip(actual, predicted)]这一行:

    以下是zip 工作原理的简单示例:

    a = [1,2,3]
    b = [4,5,6]
    c = list(zip(a,b))
    

    c 将是 list 的 tuples。

    每个tuple 将包含两个int 类型的变量。 c 的值:[(1, 4), (2, 5), (3, 6)]

    因此,当您遍历 c 时,您将获得:

    • (1, 4) 在第一次迭代中
    • (2, 5) 在第二次迭代中
    • 和 (3, 6) 在第三次迭代中。

    示例代码:

    for a_tuple in c:
        a,p = a_tuple
        print(a) # 1 at 1st iter, 2 at 2nd,and 3 at 3rd
        print(p) # 4 at 1st iter, 5 at 2nd,and 6 at 3rd
    

    我们写的时候:

    a,p = (1,2)
    

    我们会将tuple 的第一个元素分配给a,将第二个元素分配给p。 所以a == 1 和p == 2。

    如果我们写a,b,c = (1,2,3):a == 1、b == 2 和 c == 3。

    现在,我们将您的列表理解分解为一个 for 循环:

    如果你设置actual = [1,2,3]和predicted = [4,5,6],那么,写:

    [apk(a,p,k) for a,p in zip(actual, predicted)]
    

    同写:

    k = 2
    zipped = zip(actual, predicted)
    # list(zipped) == [(1, 4), (2, 5), (3, 6)]
    
    for a,p in zipped:
        apk(a,p,k) # p == 4 at 1st iter, 5 at 2nd,and 6 at 3rd. p is an element of a tuple of the zipped list
    

    总结:您必须确定actual 和predited 的类型,内部(在其范围内)名为apk 的函数。

    【讨论】:

    • 但是如果我输入 print(type(y)) 它是 &lt;class 'list'&gt; 。
    • 在mapk方法中也是&lt;class 'list'&gt;
    • 问题出在函数apk
    猜你喜欢
    • 2019-05-23
    • 2022-12-14
    • 2018-02-11
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    相关资源
    最近更新 更多