-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
67 lines (51 loc) · 1.69 KB
/
train.py
File metadata and controls
67 lines (51 loc) · 1.69 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
from pathlib import Path
import pandas as pd
import pickle
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
from src.preprocessing import clean_text
ROOT = Path(__file__).resolve().parent
PROCESSED_DIR = ROOT / "data" / "processed"
MODELS_DIR = ROOT / "models"
balanced_path = PROCESSED_DIR / "data_balanced.csv"
fallback_path = PROCESSED_DIR / "data.csv"
# Load dataset
if balanced_path.exists():
df = pd.read_csv(balanced_path)
elif fallback_path.exists():
df = pd.read_csv(fallback_path)
else:
raise FileNotFoundError(
"No training dataset found. Expected one of: "
f"{balanced_path} or {fallback_path}."
)
# Clean text
df["message"] = df["message"].apply(clean_text)
# Features & labels
X = df["message"]
y = df["label"]
# TF-IDF with NGRAMS 🔥
vectorizer = TfidfVectorizer(
max_features=10000,
ngram_range=(1, 2)
)
X = vectorizer.fit_transform(X)
# Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Model (improved)
model = LogisticRegression(max_iter=2000)
model.fit(X_train, y_train)
# Predict
y_pred = model.predict(X_test)
# Results
print("Accuracy:", accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))
# Save
MODELS_DIR.mkdir(parents=True, exist_ok=True)
with open(MODELS_DIR / "model.pkl", "wb") as model_file:
pickle.dump(model, model_file)
with open(MODELS_DIR / "vectorizer.pkl", "wb") as vectorizer_file:
pickle.dump(vectorizer, vectorizer_file)
print("✅ Improved model trained & saved!")