अब हम Deep Learning में सबसे ज़रूरी और ज़्यादा इस्तेमाल होने वाले Evaluation Metrics — विशेषकर Confusion Matrix, Precision, और Recall — को पूरी गहराई से समझेंगे।
🔷 1. What Are Evaluation Metrics?
Evaluation metrics यह मापने के लिए होते हैं कि trained model कितनी सही predictions कर पा रहा है — खासकर classification problems में।
🔶 2. Confusion Matrix (गड़बड़ी मैट्रिक्स)
Confusion matrix classification prediction को चार हिस्सों में तोड़ता है:
Predicted Positive | Predicted Negative | |
---|---|---|
Actual Positive (1) | ✅ TP (True Positive) | ❌ FN (False Negative) |
Actual Negative (0) | ❌ FP (False Positive) | ✅ TN (True Negative) |
🤖 ये matrix binary और multi-class दोनों में काम करता है।
✅ Python Code (PyTorch compatible)
from sklearn.metrics import confusion_matrix
y_true = [0, 1, 0, 1, 1, 0] # Ground truth
y_pred = [0, 1, 1, 1, 0, 0] # Model predictions
cm = confusion_matrix(y_true, y_pred)
print("Confusion Matrix:\n", cm)
Output:
[[2 1]
[1 2]]
यह कहता है:
- 2 True Negatives (class 0 सही predict किया)
- 1 False Positive
- 1 False Negative
- 2 True Positives
🔶 3. Precision (सटीकता)
Definition:
Model ने जितने positive predict किए, उनमें से कितने सच में positive थे?
Precision = TP / (TP + FP)
✅ Example:
Model ने 10 images को “cat” कहा, पर केवल 6 ही असली cat थीं →Precision = 6 / 10 = 0.60
✅ Code:
from sklearn.metrics import precision_score
precision = precision_score(y_true, y_pred)
print("Precision:", precision)
🔶 4. Recall (Sensitivity / True Positive Rate)
Definition:
Total actual positive samples में से कितनों को model ने सही predict किया?
Recall = TP / (TP + FN)
✅ Example:
10 में से 8 असली cats थीं, और model ने 6 को correctly पकड़ा →Recall = 6 / 8 = 0.75
✅ Code:
from sklearn.metrics import recall_score
recall = recall_score(y_true, y_pred)
print("Recall:", recall)
🔶 5. F1 Score (Precision + Recall का Balance)
Definition:
Precision और Recall का harmonic mean:
F1 = 2 * (Precision * Recall) / (Precision + Recall)
✅ Code:
from sklearn.metrics import f1_score
f1 = f1_score(y_true, y_pred)
print("F1 Score:", f1)
🔷 🔍 Visualization (Optional)
import seaborn as sns
import matplotlib.pyplot as plt
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.show()
🧠 कब क्या Use करें?
Metric | Use When… |
---|---|
Accuracy | Data balanced हो |
Precision | False Positive से बचना ज़रूरी हो (e.g. spam) |
Recall | False Negative से बचना ज़रूरी हो (e.g. cancer detection) |
F1 Score | Precision और Recall दोनों ज़रूरी हों |
📝 Objective Questions:
- Confusion matrix के 4 parts क्या होते हैं?
- Precision और Recall में अंतर क्या है?
- Precision कब ज़्यादा important होता है?
f1_score
metric कब use करते हैं?- Confusion matrix का visualization कैसे करें?
🔚 Summary
Metric | Formula | Meaning |
---|---|---|
Precision | TP / (TP + FP) | Predicted Positives में से सही कितने |
Recall | TP / (TP + FN) | Actual Positives में से सही कितने |
F1 Score | Harmonic mean of P & R | Balance of both metrics |
Confusion Matrix | TP, FP, FN, TN | Overall prediction structure |