【问题标题】:How to use classification report from sklearn for keras models?如何将 sklearn 的分类报告用于 keras 模型?
【发布时间】:2017-05-25 06:33:26
【问题描述】:

所以我编写了一个由以下组成的网络用于多类分类: -y_labels 用 to_categorical 转换 -last 层使用 3 个神经元的 sigmoid 函数作为我的类 -model compile 使用 categorical_crossentropy 作为损失函数 所以我用了

 model.predict_classes(x_test)

然后我把它当作

   classification_report(y_test,pred)

y_test 的形式为 to_categorical 我收到以下错误:

ValueError: Mix type of y not allowed, got types set(['binary', 'multilabel-indicator'])

我的问题是我怎样才能将它转换回来以便这样使用它?

【问题讨论】:

    标签: scikit-learn keras


    【解决方案1】:

    错误只是表明y_test 和pred 属于不同类型。在multiclass.py 中检查函数type_of_target。如此处所示,y 之一是类的指示符,另一个是类向量的指示符。您可以通过打印形状y_test.shape , pred.shape 来推断哪个是什么。

    此外,由于您使用的是model.predict_classes 而不是model.predict,因此model.predict_classes 的输出将只是类而不是类向量。

    因此,您需要通过以下方式转换其中之一:

    # class --> class vector
    from keras.utils import np_utils
    x_vec = np_utils.to_categorical(x, nb_classes)
    
    # class vector --> class 
    x = x_vec.argmax(axis=-1)
    

    【讨论】:

    • 非常感谢您的评论。这只是一个简单的解决方案,尽管我花了一些时间。再次感谢
    • 没问题。如果有帮助,你能接受答案吗?
    • @indraforyou 如果你有机会可以看看这个问题stackoverflow.com/questions/66386561/…
    猜你喜欢
    • 2017-03-29
    • 2017-09-21
    • 2019-01-09
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2021-02-09
    相关资源
    最近更新 更多