我正在关注here和here的例子,我对Keras来说是全新的 . 它看起来很神奇 - 但我不明白 .
我有一个8级分类问题 . 我的训练集有5120行和62列,最后一列是目标变量 .
我的目标变量当前被编码为浮点数,因此我将它们转换为整数,然后使用to_categorical将其转换为模型的虚拟矩阵 . 结果是numpy.ndarray的形状(num_samples,num_classes 1) . 谁知道为什么?
这是代码:
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 - 我将它放在单热矢量中的'1'位置,如下所示:0 1 0 0 0 0 0 0 0.它与'2'相同:0 0 1 0 0 0 0 0 0,最多8个 . 这就是为什么有一个额外的列:处理'0'的情况,这不是' t存在于映射中 . 从技术上讲,接受的答案解释说,这只是提供了更多细节 .
1 回答
因为
to_categorical
将类向量(整数 from 0 转换为nb_classes)转换为二进制类矩阵,以便与here中记录的categorical_crossentropy一起使用 .