【问题标题】:How to convert back the model output for single input to one of the prediction classes?如何将单个输入的模型输出转换回预测类之一?
【发布时间】:2020-08-02 11:05:18
【问题描述】:

我一直在使用 LabelEncoder 进行分类输出

from keras.utils import np_utils
from sklearn.preprocessing import LabelEncoder
label = LabelEncoder()
y_train = np_utils.to_categorical(label.fit_transform(y_train))
y_test = np_utils.to_categorical(label.fit_transform(y_test))

输出有 4 个类, 编码后,标签看起来像这样

模型看起来像这样:

model0 = Sequential()
model0.add(Conv1D(32, kernel_size=(3), input_shape=input_shape))
model0.add(Conv1D(64, kernel_size=(3)))
model0.add(Conv1D(128, kernel_size=(3)))
model0.add(Conv1D(64, kernel_size=(3)))
model0.add(Conv1D(32, kernel_size=(3)))
model0.add(Flatten())
model0.add(Dense(128, activation='relu'))
model0.add(Dense(12, activation='relu'))
model0.add(Dense(4, activation='softmax'))

model0.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

训练后,同时预测如何将输出作为类之一?我尝试使用:

model0.predict_classes(feature.reshape(1,num_features,1))
array([3], dtype=int64)

我如何知道输出属于哪个类?

【问题讨论】:

    标签: python machine-learning keras neural-network label-encoding


    【解决方案1】:

    我找到了解决办法:我们可以使用LabelEncoder类中的inverse_transform()方法,

    代码如下:

    result = model0.predict_classes(feature.reshape(1,num_features,1))
    print(label.inverse_transform(result))
    

    【讨论】:

    • 请接受您自己的答案 - 这没有错。这不是您失败的证明,而是鼓励您开始学习(并掌握)调试艺术并能够帮助自己。这是一项非常珍贵的技能,尤其是在现实生活中......
    • 网站不允许我在 48 小时之前接受我自己的答案,所以我必须再等 18 小时才能接受这个答案:)
    猜你喜欢
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多