微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 scikit learn 创建混淆矩阵时出错

如何解决使用 scikit learn 创建混淆矩阵时出错

我正在尝试使用预训练模型 benign 将癌症分为 malignantresnet50

这是模型的代码

data_path = '/content/drive/My Drive/data'
data_dir_list = os.listdir(data_path)

img_data_list = []    

for dataclass in data_dir_list:
    img_list = os.listdir(data_path+"/"+dataclass)
    for img in img_list:
        img_path = data_path+"/"+dataclass+"/"+img
        img=image.load_img(img_path,target_size=(224,224))
        x=image.img_to_array(img)
        x=np.expand_dims(x,axis=0)
        x=preprocess_input(x)
        img_data_list.append(x)
        img_data=np.array(img_data_list)

        img_data=np.rollaxis(img_data,1,0)
        img_data= img_data[0]

        num_classes=2
        num_of_sample=250
        labels=np.ones(250,dtype='int64')
        labels[0:100]=0
        labels[100:250]=1
        names=['benign','malignant']

        Y=np_utils.to_categorical(labels,num_classes)
        x,y = shuffle(img_data,Y,random_state=3)
        X_train,X_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=3)

        image_input = Input(shape=(224,224,3))

        model = resnet50(input_tensor=image_input,include_top='True',weights='imagenet')
        last_layer = model.get_layer('avg_pool').output
        x=Flatten(name='flattern')(last_layer)
        out=Dense(2,activation='softmax',name='output_layer')(x)

        custom_resnet_model = Model(inputs = image_input,outputs=out)

        for layer in custom_resnet_model.layers[:-1]:
            layer.trainable = False

            custom_resnet_model.layers[-1].trainable

            custom_resnet_model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])

            hist=custom_resnet_model.fit(X_train,batch_size=32,epochs=100,steps_per_epoch=7,validation_data=(X_test,y_test))

然后我尝试使用 scikit learn 为这个模型创建一个混淆矩阵。但是我遇到了错误

这是混淆矩阵的代码

prediction = model.predict_generator(test_set,steps=2 )

rounded_prediction = np.argmax(prediction,axis=-1)

cm = confusion_matrix(y_true=y_test,y_pred=rounded_prediction)

错误

ValueError                                Traceback (most recent call last)
<ipython-input-96-1965da59b466> in <module>()
----> 1 cm = confusion_matrix(y_true=y_test,y_pred=rounded_prediction)

1 frames
/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py in _check_targets(y_true,y_pred)
     88     if len(y_type) > 1:
     89         raise ValueError("Classification metrics can't handle a mix of {0} "
---> 90                          "and {1} targets".format(type_true,type_pred))
     91 
     92     # We can't have more than one value on y_type => The set is no more needed

ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets

因为我认为 y_truey_pred 存在问题。根据我的模型,此参数的值是多少。在这里,我使用 rounded_prediction 为每个样本选择最可能的预测。

数据类型:

type(y_test)
numpy.ndarray

type(rounded_prediction)
numpy.ndarray

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。