-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_text.py
More file actions
91 lines (73 loc) · 2.15 KB
/
train_text.py
File metadata and controls
91 lines (73 loc) · 2.15 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
90
91
import pandas as pd
import joblib
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report
# =========================
# 1. LOAD DATA MANUAL
# =========================
labels = []
texts = []
with open("data/spam.csv", encoding="latin-1") as f:
for line in f:
line = line.strip()
if not line:
continue
# split pakai ; pertama saja
parts = line.split(";", 1)
if len(parts) == 2:
label = parts[0].strip()
text = parts[1].strip()
if label in ["ham", "spam"]:
labels.append(label)
texts.append(text)
df = pd.DataFrame({
"label": labels,
"text": texts
})
print(df.head())
print(df.info())
# =========================
# 2. ENCODE LABEL
# =========================
df["label"] = df["label"].map({"ham": 0, "spam": 1})
# =========================
# 3. SPLIT DATA
# =========================
X_train, X_test, y_train, y_test = train_test_split(
df["text"], df["label"],
test_size=0.2,
random_state=42,
stratify=df["label"]
)
# =========================
# 4. TF-IDF
# =========================
vectorizer = TfidfVectorizer(
stop_words="english",
max_features=3000
)
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)
# =========================
# 5. NN BASE (NON-PRETRAINED)
# =========================
model = MLPClassifier(
hidden_layer_sizes=(128,),
max_iter=20,
random_state=42
)
model.fit(X_train_vec, y_train)
# =========================
# 6. EVALUASI
# =========================
y_pred = model.predict(X_test_vec)
print("\n=== CLASSIFICATION REPORT ===")
print(classification_report(y_test, y_pred))
# =========================
# 7. SIMPAN MODEL
# =========================
joblib.dump(model, "model_nn_base/model_text_nn.pkl")
joblib.dump(vectorizer, "model_nn_base/vectorizer.pkl")
print("✅ Model & vectorizer berhasil disimpan")