-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
80 lines (57 loc) · 2.12 KB
/
train_model.py
File metadata and controls
80 lines (57 loc) · 2.12 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
import json
import os
# SAVING MODELS
import pickle
# CONVERTING NORMAL TEXT INTO NUMBERS
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
TRAIN_PATH = "dataset/training_set.json"
TEST_PATH = "dataset/testing_set.json"
MODEL_DIR = "saved_model"
MODEL_PATH = os.path.join(MODEL_DIR, "email_classifier.pkl")
VECTORIZER_PATH = os.path.join(MODEL_DIR, "tfidf_vectorizer.pkl")
def load_database(filename):
try:
with open(filename, "r", encoding="utf-8") as json_file:
return json.load(json_file)
except FileNotFoundError:
print(f"File not found: {filename}")
except json.JSONDecodeError:
print(f"Invalid JSON in file: {filename}")
train_data = load_database(TRAIN_PATH)
train_emails = train_data["emails"]
test_data = load_database(TEST_PATH)
test_emails = test_data["emails"]
def build_text_and_labels(emails):
texts = []
labels = []
for email in emails:
subject = email.get("subject", "")
body = email.get("body", "")
status = email.get("status", "")
combined_text = f"{subject} {body}".strip()
texts.append(combined_text)
labels.append(status)
return texts, labels
def save_pickle(obj, path):
with open(path, "wb") as file:
pickle.dump(obj, file)
X_train, y_train = build_text_and_labels(train_emails)
X_test, y_test = build_text_and_labels(test_emails)
print(f"Training emails: {len(X_train)}")
print(f"Testing emails: {len(X_test)}")
vectorizer = TfidfVectorizer(lowercase=True, stop_words="english")
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)
model = LogisticRegression(max_iter=1000)
model.fit(X_train_vec, y_train)
predictions = model.predict(X_test_vec)
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy: {accuracy:.4f}")
print(classification_report(y_test, predictions))
os.makedirs(MODEL_DIR, exist_ok=True)
save_pickle(model, MODEL_PATH)
save_pickle(vectorizer, VECTORIZER_PATH)
print("Model saved.")
print("Vectorizer saved.")