【发布时间】:2017-02-15 21:51:23
【问题描述】:
我正在遵循 here 和 here 中的示例,并且对 Keras 完全陌生。看起来很神奇 - 但我遇到了一些我不明白的事情。
我有一个 8 类分类问题。我的训练集有 5120 行和 62 列,最后一列是目标变量。
我的目标变量目前被编码为浮点数,因此我将它们转换为整数,然后使用 to_categorical 转换为模型的虚拟矩阵。结果是一个形状为 (num_samples, num_classes+1) 的 numpy.ndarray。有人知道为什么吗?
代码如下:
import numpy as np
from keras.utils.np_utils import to_categorical
dataset = np.loadtxt("train_pl.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:61] #I have 5120 rows.
Y = (dataset[:,62]).astype(int) #class labels 1 to 8 inclusive
#print Y.shape #(5120,)
#print np.unique(Y) #1 2 3 4 5 6 7 8
y_binary = to_categorical(Y)
print y_binary.shape #(5120, 9) - why does this have 9 columns?
编辑
我不理解给出的答案的原因是我不明白 Keras 将类标签逐字解释为数字。例如,由于我的类被标记为 1 到 8,Keras 会查看标签“1”并说“这是一个 1 - 我将它放在 one-hot 向量中的“1”位置,如下所示:0 1 0 0 0 0 0 0 0。它与“2”相同:0 0 1 0 0 0 0 0 0,最多 8 个。这就是为什么有一个额外的列:处理“第 0”个情况,它没有t 存在于映射中。从技术上讲,公认的答案解释说,这只是提供了更多细节。
【问题讨论】: