np.argamx不返回整数

2024-07-07 08:41:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些名为testoutput的onehot编码数据,它具有形状(1000,14)

我想对它进行解码,所以根据我在网上找到的一些建议,我使用了以下代码:

# go from onehot encoding to integer
def decode(datum):
    return np.argmax(datum)

predictedhits=np.empty((1000))
for i in range(testoutput.shape[0]):
    datum = testoutput[i]
    predictedhits[i] = decode(datum)
    print('Event',i,'predicted number of hits: %s' % predictedhits[i])

问题是我希望并且期望np.argmax输出一个整数,但它却输出一个numpy.float64。 请问有人能告诉我为什么会发生这种情况,我们该怎么办?简单地执行predictedhits[i]=int(解码(数据))不会改变任何事情

提前谢谢


Tags: 代码fromgonp解码建议encoding形状
1条回答
网友
1楼 · 发布于 2024-07-07 08:41:37

你把问题误诊了numpy.argmax未返回numpy.float64的实例。相反,predictedhits具有float64数据类型。将任何值存储到该数组中会将其存储为64位浮点,从该数组中检索predictedhits[i]将生成一个numpy.float64对象

不要一次迭代一行testoutput并将值逐个存储到空数组中,只需沿所需的轴调用argmax

predictedhits = np.argmax(testoutput, axis=1)

这将保存代码,保存运行时,并生成正确数据类型的数组

相关问题 更多 >