Spaces:
Running
Running
File size: 5,428 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 | """
models/classical/train_classical.py
ββββββββββββββββββββββββββββββββββββ
Trains three classical ML classifiers on TF-IDF features:
1. Logistic Regression
2. Linear SVM (SGDClassifier)
3. Multinomial Naive Bayes
Saves per-model metrics to results/classical_results.json and
a confusion-matrix figure to results/classical_cm.png.
Usage:
python models/classical/train_classical.py
"""
import json
import time
import pathlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.metrics import (
accuracy_score, f1_score, roc_auc_score, confusion_matrix, classification_report
)
# ββ Paths βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ROOT = pathlib.Path(__file__).parents[2]
DATA_DIR = ROOT / "data" / "splits"
RESULTS = ROOT / "results"
RESULTS.mkdir(parents=True, exist_ok=True)
# ββ Load data βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print("Loading data β¦")
train_df = pd.read_parquet(DATA_DIR / "train.parquet")
test_df = pd.read_parquet(DATA_DIR / "test.parquet")
X_train, y_train = train_df["text"].tolist(), train_df["label"].tolist()
X_test, y_test = test_df["text"].tolist(), test_df["label"].tolist()
print(f"Train: {len(X_train):,} Test: {len(X_test):,}")
# ββ TF-IDF config βββββββββββββββββββββββββββββββββββββββββββββββββββββ
TFIDF = TfidfVectorizer(
ngram_range=(1, 2),
max_features=100_000,
sublinear_tf=True,
min_df=2,
)
# ββ Model definitions βββββββββββββββββββββββββββββββββββββββββββββββββ
MODELS = {
"Logistic Regression": Pipeline([
("tfidf", TFIDF),
("clf", LogisticRegression(max_iter=1000, C=1.0, solver="lbfgs")),
]),
"Linear SVM": Pipeline([
("tfidf", TfidfVectorizer(ngram_range=(1, 2), max_features=100_000,
sublinear_tf=True, min_df=2)),
("clf", SGDClassifier(loss="hinge", max_iter=100, random_state=42)),
]),
"Naive Bayes": Pipeline([
("tfidf", TfidfVectorizer(ngram_range=(1, 2), max_features=100_000,
sublinear_tf=True, min_df=2)),
("clf", MultinomialNB(alpha=0.1)),
]),
}
# ββ Training & evaluation βββββββββββββββββββββββββββββββββββββββββββββ
all_results = {}
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
fig.suptitle("Classical ML β Confusion Matrices (amazon_polarity)", fontsize=13)
for idx, (name, pipeline) in enumerate(MODELS.items()):
print(f"\n{'β'*50}")
print(f"Training: {name}")
# Train
t0 = time.perf_counter()
pipeline.fit(X_train, y_train)
train_time = time.perf_counter() - t0
# Predict
t1 = time.perf_counter()
y_pred = pipeline.predict(X_test)
infer_time_ms = (time.perf_counter() - t1) / len(X_test) * 1000
# Probabilities for ROC-AUC (where available)
if hasattr(pipeline.named_steps["clf"], "predict_proba"):
y_prob = pipeline.predict_proba(X_test)[:, 1]
elif hasattr(pipeline.named_steps["clf"], "decision_function"):
y_prob = pipeline.decision_function(X_test)
y_prob = (y_prob - y_prob.min()) / (y_prob.max() - y_prob.min())
else:
y_prob = y_pred.astype(float)
acc = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred, average="macro")
roc_auc = roc_auc_score(y_test, y_prob)
print(f" Accuracy : {acc:.4f}")
print(f" Macro F1 : {f1:.4f}")
print(f" ROC-AUC : {roc_auc:.4f}")
print(f" Train time : {train_time:.1f}s")
print(f" Infer (ms/sample): {infer_time_ms:.3f}")
print(classification_report(y_test, y_pred, target_names=["Negative", "Positive"]))
all_results[name] = {
"accuracy": round(acc, 4),
"macro_f1": round(f1, 4),
"roc_auc": round(roc_auc, 4),
"train_time_s": round(train_time, 2),
"infer_ms_per_sample": round(infer_time_ms, 4),
}
# Confusion matrix subplot
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", ax=axes[idx],
xticklabels=["Neg", "Pos"], yticklabels=["Neg", "Pos"])
axes[idx].set_title(name)
axes[idx].set_xlabel("Predicted")
axes[idx].set_ylabel("Actual")
plt.tight_layout()
cm_path = RESULTS / "classical_cm.png"
plt.savefig(cm_path, dpi=150)
print(f"\nπ Confusion matrix saved β {cm_path}")
# Save JSON results
out_path = RESULTS / "classical_results.json"
with open(out_path, "w") as f:
json.dump(all_results, f, indent=2)
print(f"π Metrics saved β {out_path}")
|