Skip to content

Commit 428bd62

Browse files
authored
Update confusion-matrix.md
1 parent a656809 commit 428bd62

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

contrib/machine-learning/confusion-matrix.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Confusion Matrix - A confusion matrix is a fundamental performance evaluation tool used in machine learning to assess the accuracy of a classification model. It is an N x N matrix, where N represents the number of target classes.
1+
## Confusion Matrix
2+
3+
A confusion matrix is a fundamental performance evaluation tool used in machine learning to assess the accuracy of a classification model. It is an N x N matrix, where N represents the number of target classes.
24

35
For binary classification, it results in a 2 x 2 matrix that outlines four key parameters:
46
1. True Positive (TP) - The predicted value matches the actual value, or the predicted class matches the actual class.
@@ -20,15 +22,18 @@ To implement the confusion matrix in Python, we can use the confusion_matrix() f
2022
The function returns a 2D array that represents the confusion matrix.
2123
We can also visualize the confusion matrix using a heatmap.
2224

25+
```python
2326
# Import necessary libraries
2427
import numpy as np
25-
from sklearn.metrics import confusion_matrix,classification_report
28+
from sklearn.metrics import confusion_matrix, classification_report
2629
import seaborn as sns
2730
import matplotlib.pyplot as plt
2831

2932
# Create the NumPy array for actual and predicted labels
30-
actual = np.array(['Apple', 'Apple', 'Apple', 'Not Apple', 'Apple', 'Not Apple', 'Apple', 'Apple', 'Not Apple', 'Not Apple'])
31-
predicted = np.array(['Apple', 'Not Apple', 'Apple', 'Not Apple', 'Apple', 'Apple', 'Apple', 'Apple', 'Not Apple', 'Not Apple'])
33+
actual = np.array(['Apple', 'Apple', 'Apple', 'Not Apple', 'Apple',
34+
'Not Apple', 'Apple', 'Apple', 'Not Apple', 'Not Apple'])
35+
predicted = np.array(['Apple', 'Not Apple', 'Apple', 'Not Apple', 'Apple',
36+
'Apple', 'Apple', 'Apple', 'Not Apple', 'Not Apple'])
3237

3338
# Compute the confusion matrix
3439
cm = confusion_matrix(actual,predicted)
@@ -46,8 +51,11 @@ plt.show()
4651

4752
# Classifications Report based on Confusion Metrics
4853
print(classification_report(actual, predicted))
54+
```
55+
56+
### Results
4957

50-
# Results
58+
```
5159
1. Confusion Matrix:
5260
[[5 1]
5361
[1 3]]
@@ -59,3 +67,4 @@ Not Apple 0.75 0.75 0.75 4
5967
accuracy 0.80 10
6068
macro avg 0.79 0.79 0.79 10
6169
weighted avg 0.80 0.80 0.80 10
70+
```

0 commit comments

Comments
 (0)