-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
89 lines (76 loc) · 2.54 KB
/
train_model.py
File metadata and controls
89 lines (76 loc) · 2.54 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
import argparse, joblib, pandas as pd
from data_loader import load_transactions
from rules import rule_based_label
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import OneHotEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, accuracy_score
def bootstrap_labels(df):
"""
Apply rule_based_label and fill missing (None) with 0
so we can train a first-pass model.
"""
labels = []
for txt in df["text"]:
lbl, _ = rule_based_label(txt) # import from rules.py
# Treat None (no clear rule) as 0 for this bootstrap stage
labels.append(0 if lbl is None else lbl)
df["label"] = labels
return df
def train_and_evaluate(input_csv, model_path):
# Load + weak labels
df = load_transactions(input_csv)
df = bootstrap_labels(df)
# Split
X = df[["text", "merchant"]]
y = df["label"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42
)
# Preprocessing: text TF-IDF + merchant One-Hot
preprocessor = ColumnTransformer([
("text", TfidfVectorizer(
stop_words="english",
ngram_range=(1,3),
sublinear_tf=True,
min_df=2
), "text"),
("merchant", OneHotEncoder(handle_unknown="ignore"), ["merchant"])
])
# Pipeline with hyperparameter tuning
pipeline = Pipeline([
("pre", preprocessor),
("clf", LogisticRegression(max_iter=2000))
])
param_grid = {
"clf__C": [0.1, 1, 10],
"clf__penalty": ["l2"]
}
grid = GridSearchCV(
pipeline,
param_grid,
cv=5,
scoring="f1",
n_jobs=-1,
verbose=1
)
# Train
grid.fit(X_train, y_train)
best = grid.best_estimator_
print("Best params:", grid.best_params_)
# Evaluate
y_pred = best.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))
# Save
joblib.dump(best, model_path)
print(f"Model saved to {model_path}")
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument("--input", default="data/sample_transactions-2.csv")
p.add_argument("--output", default="models/tax_deductible_clf.joblib")
args = p.parse_args()
train_and_evaluate(args.input, args.output)