np.argamx 不是 return 整数
np.argamx doesn't return integer
我有一些名为 testoutput 的单热编码数据,其形状为 (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(decode(datum)) 不会改变任何东西。
提前致谢!
你误诊了问题。 numpy.argmax
没有返回 numpy.float64
的实例。相反,predictedhits
具有 float64 dtype。将任何值存储到该数组中会将其存储为 64 位浮点数,并从数组中检索 predictedhits[i]
会生成一个 numpy.float64
对象。
与其一次一行地遍历 testoutput
行并将值一一存储到空数组中,不如沿所需的轴调用 argmax
:
predictedhits = np.argmax(testoutput, axis=1)
这节省了代码,节省了运行时间,并生成了适当数据类型的数组。
我有一些名为 testoutput 的单热编码数据,其形状为 (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(decode(datum)) 不会改变任何东西。
提前致谢!
你误诊了问题。 numpy.argmax
没有返回 numpy.float64
的实例。相反,predictedhits
具有 float64 dtype。将任何值存储到该数组中会将其存储为 64 位浮点数,并从数组中检索 predictedhits[i]
会生成一个 numpy.float64
对象。
与其一次一行地遍历 testoutput
行并将值一一存储到空数组中,不如沿所需的轴调用 argmax
:
predictedhits = np.argmax(testoutput, axis=1)
这节省了代码,节省了运行时间,并生成了适当数据类型的数组。