Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
97 views
in Artificial Intelligence (AI) by (119k points)
Could you provide an example code snippet to calculate a confusion matrix in Python?

Please log in or register to answer this question.

1 Answer

0 votes
by (119k points)

Certainly! Here's an example code snippet using scikit-learn to calculate a confusion matrix:

from sklearn.metrics import confusion_matrix

# Ground truth labels
actual_labels = [1, 0, 0, 1, 1, 0, 1, 0, 1, 0]

# Predicted labels from the model
predicted_labels = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]

# Calculate confusion matrix
cm = confusion_matrix(actual_labels, predicted_labels)

print(cm)
 

Output:

[[3 2]
 [3 2]]
 

In this example, we have two classes: 0 and 1. The confusion matrix shows that there are 3 true positives (class 1 predicted as class 1), 2 false positives (class 0 predicted as class 1), 3 false negatives (class 1 predicted as class 0), and 2 true negatives (class 0 predicted as class 0).

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...