如何将sklearn的分类报告用于keras模型? [英] How to use classification report from sklearn for keras models?

查看:92
本文介绍了如何将sklearn的分类报告用于keras模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我编写了一个网络,该网络由以下各项组成,用于多类分类:-y_labels用to_categorical转换-最后一层使用具有3个神经元的S形函数作为我的课程模型编译使用categorical_crossentropy作为损失函数所以我用

So i have written a network which consists of the followings for multi-class classification : -y_labels transformed with to_categorical -last layer uses a sigmoid function with 3 neurons as my classes -model compile uses categorical_crossentropy as loss function So i used

 model.predict_classes(x_test)

然后我将其用作

   classification_report(y_test,pred)

y_test的格式为to_categorical而且我收到以下错误:

y_test has the form to_categorical And i am getting the following error :

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

我的问题是如何将其转换回原样以使用它?

My question is how can i transform it back in order to use it as such?

推荐答案

该错误只是表明 y_test pred 是不同类型.在type_of_target "noreferrer"> multiclass.py .如此处所示, y 之一是类的指示符,另一个是类向量的指示符.您可以通过打印形状 y_test.shape,pred.shape 来推断哪一个是什么.

The error simply indicates that y_test and pred are of different types. Check function type_of_target in multiclass.py. As indicated here one of the y is indicator of classes and another of class vector. You can infer which one is what just by printing the shape, y_test.shape , pred.shape.

更多信息,因为您使用的是 model.predict_classes 而不是 model.predict ,因此 model.predict_classes 的输出将只是类,而不是类向量.

More over since you are using model.predict_classes instead of model.predict your output of model.predict_classes will be just classes and not class vector.

所以您需要通过以下方法之一来转换:

So either you need to convert one of them by:

# 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)

这篇关于如何将sklearn的分类报告用于keras模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆