-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelSummarizer.py
More file actions
121 lines (105 loc) · 4.09 KB
/
Copy pathModelSummarizer.py
File metadata and controls
121 lines (105 loc) · 4.09 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import numpy as np
from sklearn.metrics import accuracy_score, f1_score
import shutil
import os
import matplotlib.pyplot as plt
import pandas as pd
def evaluate(models: list[dict]):
"""
Evaluate each model in the list of models and add the evaluation metrics to the model dictionary.
Args:
models (list[dict]): A list of dictionaries where each dictionary contains details of a model.
[
{
"modelName": "model1",
"predArray": [1, 2, 3, 4, 5],
"trueArray": [1, 2, 3, 4, 5],
}
]
Returns:
list[dict]: The list of models with added evaluation metrics.
Author: Kelvin Mock
"""
for model in models:
model["accuracy"] = accuracy_score(model["trueArray"], model["predArray"])
model["macroF1"] = f1_score(model["trueArray"], model["predArray"], average="macro")
model["microF1"] = f1_score(model["trueArray"], model["predArray"], average="micro")
return models
def summarize(models: list[dict]):
"""
Summarize the best model from a list of models.
Parameters:
models (list[dict]): A list of dictionaries where each dictionary contains details of a model.
[
{
"modelName": "model1",
"predArray": [1, 2, 3, 4, 5],
"trueArray": [1, 2, 3, 4, 5],
"accuracy": 0.5,
"macroF1": 0.5,
"microF1": 0.5
}
]
Returns:
dict: The best model from the list of models.
Author: Kelvin Mock
"""
bestModel = max(models, key=lambda x: (x["accuracy"], x["macroF1"], x["microF1"]))
return bestModel
def plotComparison(models: list[dict]):
ROOT = os.path.dirname(os.path.abspath(__file__))
metrics = ["accuracy", "macroF1", "microF1"]
data = {metric: [model[metric] for model in models] for metric in metrics}
data["modelName"] = [model["modelName"] for model in models]
df = pd.DataFrame(data)
df.set_index("modelName", inplace=True)
plt.figure(figsize=(10, 6))
df.plot(kind="bar")
plt.title("Model Comparison")
plt.ylabel("Scores")
plt.xlabel("Models")
plt.xticks(rotation=0)
plt.gca().set_xticklabels([label.get_text().replace(" ", "\n") for label in plt.gca().get_xticklabels()])
plt.legend(loc="best")
plt.tight_layout()
plt.show()
plt.savefig(os.path.join(ROOT, "models_comparison.png"))
if __name__ == "__main__":
ROOT = os.path.dirname(os.path.abspath(__file__))
models = [
{
"modelName": "Baseline: Logistic Regression",
"predArray": np.load(os.path.join(ROOT, "predictions","BASELINE_y_dev_pred.npy")),
"trueArray": np.load(os.path.join(ROOT, "predictions", "BASELINE_y_dev.npy")),
},
{
"modelName": "Deep Learning: DistilBERT",
"predArray": np.load(os.path.join(ROOT, "predictions", "DISTILBERT_pred_dev.npy")),
"trueArray": np.load(os.path.join(ROOT, "predictions", "DISTILBERT_y_dev.npy")),
},
{
"modelName": "Deep Learning: LLM",
"predArray": np.load(os.path.join(ROOT, "predictions", "LLM_y_dev_pred.npy")),
"trueArray": np.load(os.path.join(ROOT, "predictions", "LLM_y_dev.npy")),
}
]
models = evaluate(models)
bestModel = summarize(models)
print("Best Model is: ", bestModel["modelName"])
# process model name
match (bestModel["modelName"]) :
case "Baseline - Logistic Regression":
best_model_name = "baseline"
case "Deep Learning - DistilBERT":
best_model_name = "distilBERT"
case "Deep Learning - LLM":
best_model_name = "llm"
case _:
best_model_name = "baseline"
# copy the best model's results to Result.jsonl as an output
source_file = os.path.join(ROOT, "content", f"Result_{best_model_name}.jsonl")
destination_file = os.path.join(ROOT, "content", "Results.jsonl")
shutil.copyfile(source_file, destination_file)
print(f"Copied {source_file} to {destination_file}")
# Plot Comparison
plotComparison(models)