keras 如何为混淆矩阵和分类报告初始化y_true和y_pred

f45qwnt8  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(190)

如何初始化混淆矩阵和分类报告的y_true和y_pred?我已经使用了flow_from_dataframe。
我的代码如下:

train_set = train_datagen.flow_from_dataframe(
    train,
    path,
    x_col="image_name",
    y_col="level",
    class_mode="raw",
    color_mode="rgb",
    batch_size=32,
    target_size=(64, 64))

val_set = val_datagen.flow_from_dataframe(
    val,
    path,
    x_col="image_name",
    y_col="level",
    class_mode="raw",
    color_mode="rgb",
    batch_size=32,
    target_size=(64, 64))

from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
Y_pred = model.predict(val_set)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')

print(confusion_matrix(val_set.classes, y_pred))
print('Classification Report')

class_labels = list(val_set.class_indices.keys()) 

print(classification_report(val_set.classes, y_pred, target_names=class_labels))

我得到的错误为AttributeError:'DataFrameIterator'对象没有'classes'属性。
我已经尝试了很长时间了。我得到了flow_from_directory的结果,但没有得到flow_from_dataframe的结果。
请指点。

v64noz0r

v64noz0r1#

请尝试下面的代码。注意在瓦尔_set = val_datagen.flow_from_dataframe(...)中设置参数shuffle=False

errors=0
y_pred=[]
y_true=val_set.labels # make sure shuffle=False in generator
classes=list(val_set.class_indices.keys()
class_count=len(classes)
preds=model.predict(val_set, verbose=1)
for i, p in enumerate(preds):        
        pred_index=np.argmax(p)         
        true_index=test_gen.labels[i]  # labels are integer values        
        if pred_index != true_index: # a misclassification has occurred                                           
            errors=errors + 1
        y_pred.append(pred_index)
tests=len(preds)
acc=( 1-errors/tests) * 100
msg=f'there were {errors} errors in {tests} tests for an accuracy of {acc:6.2f}'
print(msg)
ypred=np.array(y_pred)
ytrue=np.array(y_true)
cm = confusion_matrix(ytrue, ypred )
plt.figure(figsize=(12, 8))
sns.heatmap(cm, annot=True, vmin=0, fmt='g', cmap='Blues', cbar=False)       
plt.xticks(np.arange(class_count)+.5, classes, rotation=90)
plt.yticks(np.arange(class_count)+.5, classes, rotation=0)
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.show()
clr = classification_report(y_true, y_pred, target_names=classes, digits= 4) # create classification report
print("Classification Report:\n----------------------\n", clr)

相关问题