[英]how to initialize y_true and y_pred for confusion_matrix and classification_report
如何為 confusion_matrix 和 classification_report 初始化 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' object has no attribute 'classes'。
我從很久以前就開始嘗試了。 我得到了 flow_from_directory 的結果,但沒有得到 flow_from_dataframe 的結果。
請指導我。
試試下面的代碼。注意在 val_set = val_datagen.flow_from_dataframe(...) set parameter 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.