-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_minimal.py
More file actions
76 lines (62 loc) · 2.27 KB
/
train_minimal.py
File metadata and controls
76 lines (62 loc) · 2.27 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
train_minimal.py
----------------
Sadece subject, text, label kolonlarıyla minimal eğitim.
Kullanım:
python train_minimal.py --csv emails_dataset.csv
"""
import argparse
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report, roc_auc_score
import joblib
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--csv", required=True, help="CSV yolu (kolonlar: subject,text,label)")
ap.add_argument("--model", default="spam_model_min.joblib", help="Kaydedilecek model dosyası")
ap.add_argument("--test-size", type=float, default=0.2, help="Test oranı")
ap.add_argument("--seed", type=int, default=42, help="Rastgelelik tohumu")
args = ap.parse_args()
# Veri oku
df = pd.read_csv(args.csv)
for c in ["subject", "text", "label"]:
if c not in df.columns:
raise SystemExit(f"Kolon eksik: {c}. CSV kolonları: {list(df.columns)}")
# Subject + text birleştir
X = (df["subject"].fillna("").astype(str) + " \n " + df["text"].fillna("").astype(str))
y = df["label"].astype(int)
# Eğitim/Test böl
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=args.test_size, random_state=args.seed, stratify=y
)
# Pipeline
pipe = Pipeline([
("tfidf", TfidfVectorizer(
lowercase=True,
strip_accents="unicode",
stop_words=None,
ngram_range=(1,2),
min_df=1,
max_df=0.95
)),
("clf", LogisticRegression(
max_iter=2000,
C=4.0,
class_weight="balanced"
))
])
pipe.fit(X_train, y_train)
y_pred = pipe.predict(X_test)
print(classification_report(y_test, y_pred, digits=3))
if hasattr(pipe.named_steps["clf"], "predict_proba"):
y_proba = pipe.predict_proba(X_test)[:,1]
print("ROC-AUC:", roc_auc_score(y_test, y_proba))
joblib.dump(pipe, args.model)
print(f"Model kaydedildi: {args.model}")
if __name__ == "__main__":
main()