简体   繁体   English

将数字 output 标签转换为 keras 中的分类向量

[英]converting numerical output labels to categorical vectors in keras

I'm creating a Deep learning multi-classification model in Keras and I have converted my outputlabel training set y_train from numerical values ranging from 1 to 14 to output vectors looking like this [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0] => representing the number 2 .我正在 Keras 中创建一个深度学习多分类 model,我已经将我的输出标签训练集 y_train 从 1 到 14 的数值转换为 output 向量,看起来像这样[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0] => representing the number 2 This is the code I used in python (keras):这是我在 python (keras) 中使用的代码:

from keras.utils import to_categorical
y_train = to_categorical(y_train)

However, because it converts these outputlabels into a vector of length 15 instead of 14 because it adds zero as a potential output as well.但是,因为它将这些输出标签转换为长度为 15 而不是 14 的向量,因为它也将零添加为潜在的 output。 My original numpy array y_train looked like this: [1,8,9,7,2,2,8...] and it should be converted to a vector of length 14 instead of 15 to avoid extra loss when training the model. Is there a simpel way to avoid using zero as a potential output class?我原来的 numpy 数组 y_train 看起来像这样: [1,8,9,7,2,2,8...]应该将其转换为长度为 14 而不是 15 的向量,以避免在训练 model 时造成额外损失。有没有一种简单的方法可以避免使用零作为潜在的 output class?

( num_classes = 14 as a parameter of to_categorical gives an error message) num_classes = 14作为 to_categorical 的参数给出错误信息)

if your labels are from 1 to 14, try this simple trick:如果你的标签是从 1 到 14,试试这个简单的技巧:

y_train = to_categorical(np.asarray(y_train)-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Pandas 中将数值转换为分类 - Converting Numerical Values to Categorical in Pandas 在熊猫中将字符串/数字数据转换为分类格式 - Converting string/numerical data to categorical format in pandas Keras model.predict错误的分类标签 - Keras model.predict error for categorical labels 将数值和分类数据混合到具有密集层的 keras 序列模型中 - Mixing numerical and categorical data into keras sequential model with Dense layers 用于数值和分类变量的自定义损失函数 keras - Custom loss function keras for both numerical and categorical variables 为什么 keras model 如果与 one-hot 标签和 categorical_crossentropy 和 softmax output 一起使用,则将所有预测为 1 - Why does keras model predicts all as ones if used with one-hot labels and categorical_crossentropy amnd softmax output 是否必须将分类数据转换为数值数据才能使用解释(Microsoft 包)? - Is it obligatory converting categorical data to numerical data to use interpret (Microsoft package)? Python - 加速将分类变量转换为数字索引 - Python - Speed up for converting a categorical variable to it's numerical index 将带有 % 符号的分类变量转换为数值变量 Python Pandas - Converting Categorical Variable with % Sign to Numerical Variable Python Pandas Keras to_categorical 输出的类别比实际标签多 - Keras to_categorical outputs more categories than actual labels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM