-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtrain.py
More file actions
41 lines (33 loc) · 1.33 KB
/
train.py
File metadata and controls
41 lines (33 loc) · 1.33 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
import joblib, pandas as pd
from pathlib import Path
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import classification_report, accuracy_score
ROOT = Path(__file__).parent
DATA = ROOT / "data" / "customer_feedback.csv"
MODELS = ROOT / "models"
MODELS.mkdir(exist_ok=True)
def main():
df = pd.read_csv(DATA).dropna(subset=["text","label"])
X = df["text"].astype(str).values
y = df["label"].values
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
vec = TfidfVectorizer(lowercase=True, ngram_range=(1,2), max_features=25000, min_df=1)
X_train_vec = vec.fit_transform(X_train)
X_test_vec = vec.transform(X_test)
base = LinearSVC()
clf = CalibratedClassifierCV(base, cv=3)
clf.fit(X_train_vec, y_train)
y_pred = clf.predict(X_test_vec)
acc = accuracy_score(y_test, y_pred)
print("Accuracy:", acc)
print("\nClassification Report:\n", classification_report(y_test, y_pred))
joblib.dump(vec, MODELS / "vectorizer.pkl")
joblib.dump(clf, MODELS / "classifier.pkl")
print(f"Saved artifacts to {MODELS.resolve()}")
if __name__ == "__main__":
main()