-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.py
More file actions
34 lines (26 loc) · 934 Bytes
/
2.py
File metadata and controls
34 lines (26 loc) · 934 Bytes
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
# Den här algoritmen är Multinomial Naive Bayes och används ofta för textklassificering, t.ex. spamfilter.
#model = MultinomialNB()
# crossval_spam_detector.py
# 1. Importera bibliotek
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import cross_val_score
from sklearn.metrics import make_scorer, f1_score
# 2. Ladda in datan
df = pd.read_csv("cleaned_spam_dataset.csv")
# 3. Förbered data
texts = df['message']
labels = df['label'].map({'ham': 0, 'spam': 1})
# 4. Text -> TF-IDF
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(texts)
y = labels
# 5. Modell
model = MultinomialNB()
# 6. Cross-validation (t.ex. 5-fold)
scores = cross_val_score(model, X, y, cv=5, scoring='f1')
# 7. Visa resultat
print("F1-score per fold:", scores)
print("Genomsnittligt F1-score:", np.mean(scores))