-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
67 lines (54 loc) · 1.71 KB
/
Copy pathutils.py
File metadata and controls
67 lines (54 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import matplotlib.pyplot as plt
import seaborn as sns
from typing import Any, Dict
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score,
confusion_matrix,
classification_report,
)
def print_evaluation_metrics(
y_true: list, y_pred: list, model, name: str
) -> Dict[str, Any]:
"""
Prints and plots evaluation metrics for a given model's predictions.
Parameters
---------
- y_true (list): The true labels.
- y_pred (list): The predicted labels by the model.
- model: The model used for prediction.
- name (str): The name of the model.
Returns
-------
- Dict[str, Any]: A dictionary containing the model's name, the model object, accuracy, precision, recall, F1 score, and confusion matrix.
"""
accuracy = accuracy_score(y_true, y_pred)
precision = precision_score(y_true, y_pred, average="macro")
recall = recall_score(y_true, y_pred, average="macro")
f1 = f1_score(y_true, y_pred, average="macro")
cm = confusion_matrix(y_true, y_pred)
print(f"Model: {name}")
print(model)
if hasattr(model, "oob_score_"):
print(f"OOB Score: {model.oob_score_}")
print(f"Accuracy: {accuracy}")
print(f"Precision: {precision}")
print(f"Recall: {recall}")
print(f"F1: {f1}")
print(f"Classification Report: \n{classification_report(y_true, y_pred)}")
plt.figure(figsize=(10, 7))
sns.heatmap(cm, annot=True, fmt="d")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()
return {
"name": name,
"model": model,
"accuracy": accuracy,
"precision": precision,
"recall": recall,
"f1": f1,
"confusion_matrix": cm,
}