python - OneHotEncoder 之前和之后的不同结果

标签 python numpy

我使用 OneHotEncoder 对 y 向量进行编码,运行我的神经网络并获得输出。 这里a代表我的y向量,b代表预测结果

a = np.array([[0., 0., 1., 0.],
       [0., 0., 1., 0.],
       [0., 0., 1., 0.],
       [0., 0., 1., 0.],
       [0., 0., 1., 0.],
       [0., 0., 1., 0.],
       [1., 0., 0., 0.],
       [1., 0., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 1., 0.]])
b=np.array([[0., 0., 0., 1.],
       [1., 0., 0., 0.],
       [0., 0., 1., 0.],
       [0., 1., 0., 0.],
       [1., 0., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 1., 0.],
       [1., 0., 0., 0.],
       [0., 0., 0., 1.],
       [0., 1., 0., 0.]])
print(np.mean(a!=b))
print(np.mean(np.argmax(a,1)!=np.argmax(b,1)))

但在恢复编码后(使用np.argmax(a,1)),我得到输出:

0.35
0.7

这些值不应该相等吗?

最佳答案

您的问题来自于 a!=b 返回一个数组数组,检查每个索引处的元素是否不同。第一个返回数组的数组,其中第一个值为 [False False True True],这意味着对于 ab 中的第一项,前两个条目相同,后两个条目不同。请尝试以下操作:

print(a!=b)
>>>[[False False  True  True], [ True False  True False] ...

第二个返回一个数组,根据需要比较索引:

print(np.argmax(a,1)!=np.argmax(b,1))
>>>[ True  True False  True  True False  True False  True  True]

关于python - OneHotEncoder 之前和之后的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52952067/

相关文章:

python - Numpy 高级索引 : same index used multiple times in +=

python - 在 OS X 上安装 python 2 的最佳方法是什么?

python - Tensorflow - 训练期间的 Nan 成本值 - 尝试了通常的修复但没有成功

Python:获取列表中出现次数最多的项目

python - Python 中的语法 (.T)

python - pandas在ubuntu上安装错误代码1 in/tmp/pip-build-FM0q5o/pandas/

arrays - Mpi4Py - 发送无副本的 numpy 子数组(非连续内存)

Python + List : selling prices calculated correctly only for first customer, 其余的都是 0

python - pywinauto 用 CTRL 滚动模拟缩放不起作用

c++ - 多个应用程序入口点