Not able to print anything in row 2 of confusion matrix

42 Views Asked by At

Can anyone please help! I tried printing confusion matrix, but not able to get the values in row 2. I am not able to print anything in row 2 for the KNN Model trained.

import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

def plot_confusion_matrix(y_test, y_pred):
    cm = confusion_matrix(y_test, y_pred)
    sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
    plt.xlabel("Predicted")
    plt.ylabel("Actual")
    plt.title("Confusion Matrix")
    plt.xticks([0, 1], ["Edible", "Poisonous"])
    plt.yticks([0, 1], ["Edible", "Poisonous"])
    plt.show()

target_names = ["Edible", "Poisonous"]

knn_classifier = KNeighborsClassifier(n_neighbors=5)
knn_classifier.fit(X_train, y_train)
y_pred = knn_classifier.predict(X_test)

print(classification_report(y_test, y_pred, target_names=target_names))

conf_matrix = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=target_names, yticklabels=target_names)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)```

1

There are 1 best solutions below

0
TheHungryCub On

It seems like you’re not importing the sns module for Seaborn visualization. Make sure you import it at the beginning of your script.

Add import seaborn as sns after your import statements. This should resolve the issue with not being able to print anything in row 2 for the KNN model.