Spaces:
Sleeping
Sleeping
File size: 6,058 Bytes
edf3081 | 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | """
evaluate/evaluate_all.py
βββββββββββββββββββββββββ
Aggregates all results JSON files from results/ and produces:
1. A summary table printed to stdout
2. results/comparison_bar.png β bar chart (Accuracy, F1, AUC)
3. results/latency_bar.png β inference latency comparison
4. results/summary_table.csv β machine-readable results
Run AFTER all model training scripts have completed:
python evaluate/evaluate_all.py
"""
import json
import pathlib
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
# ββ Paths βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ROOT = pathlib.Path(__file__).parents[1]
RESULTS = ROOT / "results"
# ββ Model display names (key = JSON filename stem) ββββββββββββββββββββ
MODEL_LABELS = {
"classical_results": None, # handled specially (nested)
"bilstm_results": "BiLSTM",
"textcnn_results": "TextCNN",
"distilbert_base_uncased_results": "DistilBERT",
"bert_base_uncased_results": "BERT-base",
"roberta_base_results": "RoBERTa-base",
}
TIER_COLORS = {
"Logistic Regression": "#4C72B0",
"Linear SVM": "#4C72B0",
"Naive Bayes": "#4C72B0",
"BiLSTM": "#DD8452",
"TextCNN": "#DD8452",
"DistilBERT": "#55A868",
"BERT-base": "#55A868",
"RoBERTa-base": "#55A868",
}
TIER_HATCHES = {
"Logistic Regression": "",
"Linear SVM": "//",
"Naive Bayes": "xx",
"BiLSTM": "",
"TextCNN": "//",
"DistilBERT": "",
"BERT-base": "//",
"RoBERTa-base": "xx",
}
def load_all_results() -> list[dict]:
rows = []
for json_path in sorted(RESULTS.glob("*.json")):
stem = json_path.stem
# Classical results are nested dicts
if stem == "classical_results":
data = json.loads(json_path.read_text())
for model_name, metrics in data.items():
rows.append({"model": model_name, **metrics})
continue
label = MODEL_LABELS.get(stem)
if label is None:
continue
data = json.loads(json_path.read_text())
rows.append({"model": label, **data})
return rows
def main():
rows = load_all_results()
if not rows:
print("β No result files found in results/. Run all training scripts first.")
return
df = pd.DataFrame(rows)
df = df[["model", "accuracy", "macro_f1", "roc_auc", "infer_ms_per_sample"]]
df = df.sort_values("macro_f1", ascending=False).reset_index(drop=True)
print("\n" + "="*70)
print(" COMPARATIVE SENTIMENT ANALYSIS RESULTS β amazon_polarity")
print("="*70)
print(df.to_string(index=False, float_format="{:.4f}".format))
print("="*70 + "\n")
df.to_csv(RESULTS / "summary_table.csv", index=False)
print(f"π Table saved β {RESULTS / 'summary_table.csv'}")
models = df["model"].tolist()
colors = [TIER_COLORS.get(m, "#999999") for m in models]
hatches = [TIER_HATCHES.get(m, "") for m in models]
x = np.arange(len(models))
# ββ Bar chart: Accuracy / F1 / AUC βββββββββββββββββββββββββββββββ
fig, axes = plt.subplots(1, 3, figsize=(16, 5))
fig.suptitle("Comparative Sentiment Analysis β amazon_polarity", fontsize=13, weight="bold")
for ax, metric, ylabel in zip(
axes,
["accuracy", "macro_f1", "roc_auc"],
["Accuracy", "Macro F1", "ROC-AUC"],
):
bars = ax.bar(x, df[metric], color=colors, edgecolor="white", linewidth=0.8)
for bar, hatch in zip(bars, hatches):
bar.set_hatch(hatch)
ax.set_xticks(x)
ax.set_xticklabels(models, rotation=35, ha="right", fontsize=9)
ax.set_ylabel(ylabel)
ax.set_ylim(max(0, df[metric].min() - 0.05), min(1.0, df[metric].max() + 0.05))
ax.yaxis.set_major_formatter(mticker.FormatStrFormatter("%.3f"))
ax.set_title(ylabel)
ax.grid(axis="y", alpha=0.3)
for bar, val in zip(bars, df[metric]):
ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 0.002,
f"{val:.3f}", ha="center", va="bottom", fontsize=7.5)
# Legend: tiers
from matplotlib.patches import Patch
legend_elements = [
Patch(facecolor="#4C72B0", label="Classical ML"),
Patch(facecolor="#DD8452", label="Deep Learning"),
Patch(facecolor="#55A868", label="Transformers"),
]
fig.legend(handles=legend_elements, loc="upper right", fontsize=9)
plt.tight_layout()
bar_path = RESULTS / "comparison_bar.png"
plt.savefig(bar_path, dpi=150)
print(f"π Bar chart saved β {bar_path}")
# ββ Latency chart βββββββββββββββββββββββββββββββββββββββββββββββββ
fig2, ax2 = plt.subplots(figsize=(10, 4))
lat_bars = ax2.barh(models[::-1], df["infer_ms_per_sample"][::-1],
color=[colors[i] for i in range(len(models)-1, -1, -1)])
ax2.set_xlabel("Inference latency (ms / sample)")
ax2.set_title("Inference Speed Comparison β amazon_polarity", weight="bold")
ax2.grid(axis="x", alpha=0.3)
for bar, val in zip(lat_bars, df["infer_ms_per_sample"][::-1]):
ax2.text(bar.get_width() + 0.01, bar.get_y() + bar.get_height() / 2,
f"{val:.3f} ms", va="center", fontsize=8)
plt.tight_layout()
lat_path = RESULTS / "latency_bar.png"
plt.savefig(lat_path, dpi=150)
print(f"π Latency chart saved β {lat_path}")
plt.show()
if __name__ == "__main__":
main()
|