-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
406 lines (361 loc) · 19.6 KB
/
train_model.py
File metadata and controls
406 lines (361 loc) · 19.6 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/env python3
"""
SMS Spam Detection Model Training Script
Trains a machine learning model on the spam collection dataset
"""
import pandas as pd
import numpy as np
import re
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.pipeline import Pipeline
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
import warnings
warnings.filterwarnings('ignore')
class SMSFeatureExtractor:
"""Extract features from SMS text for spam detection"""
def __init__(self):
self.spam_keywords = [
'free', 'win', 'winner', 'congratulations', 'urgent', 'limited time',
'click here', 'act now', 'cash prize', 'guaranteed', 'no obligation',
'risk free', 'special promotion', 'limited offer', 'exclusive deal',
'call now', 'text stop', 'unsubscribe', 'opt out', 'sms', 'txt',
'mobile', 'phone', 'call', 'text', 'reply', 'stop', 'end',
'prize', 'award', 'claim', 'winner', 'selected', 'chosen',
'congratulations', 'urgent', 'immediate', 'expires', 'limited',
'offer', 'deal', 'discount', 'save', 'money', 'cash', 'dollar',
'pound', 'euro', '£', '$', '€', 'million', 'thousand',
'subscription', 'service', 'charge', 'billing', 'payment',
'credit', 'card', 'account', 'bank', 'security', 'verify',
'confirm', 'update', 'suspended', 'blocked', 'expired',
'click', 'link', 'website', 'www', 'http', 'https', 'com',
'download', 'install', 'software', 'virus', 'malware',
'lottery', 'raffle', 'contest', 'competition', 'draw',
'ticket', 'entry', 'participate', 'join', 'register',
'subscribe', 'newsletter', 'alert', 'notification',
'reminder', 'appointment', 'meeting', 'schedule',
'delivery', 'shipping', 'order', 'purchase', 'buy',
'sell', 'sale', 'auction', 'bid', 'offer', 'price',
'cost', 'fee', 'charge', 'payment', 'billing', 'invoice',
'receipt', 'refund', 'return', 'exchange', 'warranty',
'insurance', 'coverage', 'policy', 'claim', 'settlement',
'loan', 'credit', 'debt', 'mortgage', 'finance', 'investment',
'stock', 'share', 'trading', 'broker', 'advisor',
'consultant', 'expert', 'professional', 'specialist',
'doctor', 'lawyer', 'attorney', 'counsel', 'legal',
'medical', 'health', 'treatment', 'therapy', 'cure',
'medicine', 'drug', 'pharmacy', 'prescription', 'dosage',
'side effect', 'adverse', 'warning', 'caution', 'danger',
'risk', 'hazard', 'safety', 'security', 'protection',
'privacy', 'confidential', 'secret', 'private', 'personal',
'identity', 'theft', 'fraud', 'scam', 'fake', 'phishing',
'spoofing', 'hacking', 'breach', 'leak', 'exposure',
'data', 'information', 'record', 'file', 'document',
'report', 'statement', 'summary', 'analysis', 'review',
'rating', 'score', 'grade', 'rank', 'position', 'status',
'level', 'tier', 'category', 'class', 'type', 'kind',
'sort', 'variety', 'selection', 'choice', 'option',
'alternative', 'substitute', 'replacement', 'backup',
'copy', 'duplicate', 'original', 'genuine', 'authentic',
'real', 'true', 'false', 'fake', 'counterfeit', 'imitation',
'replica', 'model', 'sample', 'example', 'instance',
'case', 'scenario', 'situation', 'circumstance', 'condition',
'state', 'status', 'position', 'location', 'place',
'address', 'contact', 'phone', 'mobile', 'cell', 'number',
'email', 'mail', 'message', 'text', 'sms', 'notification',
'alert', 'reminder', 'announcement', 'notice', 'update',
'news', 'information', 'data', 'details', 'facts',
'statistics', 'numbers', 'figures', 'results', 'outcomes',
'consequences', 'effects', 'impacts', 'influences', 'changes',
'modifications', 'adjustments', 'improvements', 'enhancements',
'upgrades', 'updates', 'revisions', 'corrections', 'fixes',
'solutions', 'answers', 'responses', 'replies', 'feedback',
'comments', 'suggestions', 'recommendations', 'advice',
'tips', 'hints', 'clues', 'signs', 'indicators', 'markers',
'flags', 'warnings', 'alerts', 'notifications', 'messages',
'communications', 'correspondence', 'letters', 'emails',
'calls', 'contacts', 'connections', 'relationships',
'associations', 'partnerships', 'collaborations', 'alliances',
'agreements', 'contracts', 'deals', 'arrangements', 'plans',
'strategies', 'approaches', 'methods', 'techniques', 'procedures',
'processes', 'systems', 'frameworks', 'structures', 'models',
'patterns', 'templates', 'formats', 'standards', 'guidelines',
'rules', 'regulations', 'policies', 'procedures', 'protocols',
'requirements', 'specifications', 'criteria', 'conditions',
'terms', 'clauses', 'sections', 'parts', 'components',
'elements', 'factors', 'variables', 'parameters', 'settings',
'configurations', 'customizations', 'personalizations',
'adaptations', 'modifications', 'adjustments', 'changes',
'updates', 'upgrades', 'improvements', 'enhancements',
'optimizations', 'refinements', 'revisions', 'corrections',
'fixes', 'repairs', 'maintenance', 'servicing', 'support',
'assistance', 'help', 'aid', 'guidance', 'direction',
'instruction', 'education', 'training', 'learning', 'teaching',
'coaching', 'mentoring', 'advising', 'consulting', 'counseling',
'therapy', 'treatment', 'healing', 'recovery', 'rehabilitation',
'restoration', 'renewal', 'revival', 'refreshment', 'rejuvenation',
'regeneration', 'reconstruction', 'rebuilding', 'redevelopment',
'renovation', 'remodeling', 'refurbishment', 'restoration',
'conservation', 'preservation', 'protection', 'maintenance',
'care', 'attention', 'focus', 'concentration', 'emphasis',
'priority', 'importance', 'significance', 'relevance',
'applicability', 'usefulness', 'value', 'worth', 'benefit',
'advantage', 'merit', 'quality', 'excellence', 'superiority',
'premium', 'deluxe', 'luxury', 'exclusive', 'special',
'unique', 'rare', 'uncommon', 'unusual', 'extraordinary',
'exceptional', 'outstanding', 'remarkable', 'notable',
'significant', 'important', 'crucial', 'critical', 'essential',
'vital', 'necessary', 'required', 'mandatory', 'compulsory',
'obligatory', 'binding', 'enforceable', 'valid', 'legal',
'legitimate', 'authorized', 'approved', 'certified', 'verified',
'confirmed', 'authenticated', 'validated', 'tested', 'proven',
'reliable', 'trustworthy', 'credible', 'believable', 'convincing',
'persuasive', 'compelling', 'attractive', 'appealing', 'desirable',
'wanted', 'needed', 'required', 'demanded', 'requested',
'ordered', 'purchased', 'bought', 'acquired', 'obtained',
'received', 'delivered', 'shipped', 'sent', 'dispatched',
'transmitted', 'transferred', 'moved', 'relocated', 'transported',
'carried', 'conveyed', 'delivered', 'handed', 'passed',
'given', 'provided', 'supplied', 'furnished', 'equipped',
'outfitted', 'prepared', 'ready', 'available', 'accessible',
'obtainable', 'achievable', 'attainable', 'reachable',
'approachable', 'contactable', 'reachable', 'available',
'present', 'here', 'there', 'everywhere', 'anywhere',
'somewhere', 'nowhere', 'always', 'never', 'sometimes',
'often', 'rarely', 'occasionally', 'frequently', 'regularly',
'constantly', 'continuously', 'permanently', 'temporarily',
'briefly', 'momentarily', 'instantly', 'immediately',
'quickly', 'rapidly', 'swiftly', 'speedily', 'fast',
'slow', 'gradual', 'steady', 'consistent', 'stable',
'reliable', 'dependable', 'trustworthy', 'faithful',
'loyal', 'devoted', 'committed', 'dedicated', 'focused',
'determined', 'persistent', 'tenacious', 'resilient',
'strong', 'powerful', 'mighty', 'forceful', 'intense',
'severe', 'serious', 'grave', 'critical', 'urgent',
'immediate', 'instant', 'quick', 'fast', 'rapid',
'swift', 'speedy', 'hasty', 'hurried', 'rushed',
'pressed', 'stressed', 'strained', 'tension', 'pressure',
'burden', 'load', 'weight', 'responsibility', 'duty',
'obligation', 'commitment', 'promise', 'pledge', 'vow',
'oath', 'swear', 'declare', 'announce', 'proclaim',
'state', 'say', 'tell', 'speak', 'talk', 'discuss',
'converse', 'chat', 'communicate', 'correspond', 'contact',
'reach', 'connect', 'link', 'join', 'unite', 'combine',
'merge', 'blend', 'mix', 'integrate', 'incorporate',
'include', 'involve', 'participate', 'engage', 'take part',
'join in', 'contribute', 'add', 'supply', 'provide',
'offer', 'give', 'present', 'deliver', 'hand over',
'transfer', 'pass', 'move', 'shift', 'relocate',
'transport', 'carry', 'convey', 'bring', 'take',
'fetch', 'get', 'obtain', 'acquire', 'gain', 'win',
'earn', 'achieve', 'accomplish', 'complete', 'finish',
'end', 'stop', 'halt', 'pause', 'break', 'rest',
'relax', 'unwind', 'calm', 'peaceful', 'quiet',
'silent', 'still', 'motionless', 'static', 'stable',
'steady', 'firm', 'solid', 'strong', 'tough', 'hard',
'difficult', 'challenging', 'complex', 'complicated',
'intricate', 'sophisticated', 'advanced', 'modern',
'contemporary', 'current', 'latest', 'newest', 'recent',
'fresh', 'new', 'novel', 'original', 'unique', 'special',
'particular', 'specific', 'individual', 'personal',
'private', 'confidential', 'secret', 'hidden', 'concealed',
'covered', 'protected', 'secured', 'safe', 'secure',
'reliable', 'dependable', 'trustworthy', 'faithful',
'loyal', 'devoted', 'committed', 'dedicated', 'focused',
'determined', 'persistent', 'tenacious', 'resilient',
'strong', 'powerful', 'mighty', 'forceful', 'intense',
'severe', 'serious', 'grave', 'critical', 'urgent',
'immediate', 'instant', 'quick', 'fast', 'rapid',
'swift', 'speedy', 'hasty', 'hurried', 'rushed',
'pressed', 'stressed', 'strained', 'tension', 'pressure',
'burden', 'load', 'weight', 'responsibility', 'duty',
'obligation', 'commitment', 'promise', 'pledge', 'vow',
'oath', 'swear', 'declare', 'announce', 'proclaim',
'state', 'say', 'tell', 'speak', 'talk', 'discuss',
'converse', 'chat', 'communicate', 'correspond', 'contact',
'reach', 'connect', 'link', 'join', 'unite', 'combine',
'merge', 'blend', 'mix', 'integrate', 'incorporate',
'include', 'involve', 'participate', 'engage', 'take part',
'join in', 'contribute', 'add', 'supply', 'provide',
'offer', 'give', 'present', 'deliver', 'hand over',
'transfer', 'pass', 'move', 'shift', 'relocate',
'transport', 'carry', 'convey', 'bring', 'take',
'fetch', 'get', 'obtain', 'acquire', 'gain', 'win',
'earn', 'achieve', 'accomplish', 'complete', 'finish',
'end', 'stop', 'halt', 'pause', 'break', 'rest',
'relax', 'unwind', 'calm', 'peaceful', 'quiet',
'silent', 'still', 'motionless', 'static', 'stable',
'steady', 'firm', 'solid', 'strong', 'tough', 'hard',
'difficult', 'challenging', 'complex', 'complicated',
'intricate', 'sophisticated', 'advanced', 'modern',
'contemporary', 'current', 'latest', 'newest', 'recent',
'fresh', 'new', 'novel', 'original', 'unique', 'special',
'particular', 'specific', 'individual', 'personal',
'private', 'confidential', 'secret', 'hidden', 'concealed',
'covered', 'protected', 'secured', 'safe', 'secure'
]
self.spam_patterns = [
r'\b\d{4,}\b', # Long numbers
r'[A-Z]{3,}', # Multiple caps
r'[!]{2,}', # Multiple exclamation marks
r'\$[0-9]+', # Dollar amounts
r'%[0-9]+', # Percentages
r'http[s]?://', # URLs
r'www\.', # www links
r'\b[A-Z]{2,}\b', # All caps words
r'\b\d{3,}\b', # Numbers with 3+ digits
r'[!?]{2,}', # Multiple punctuation
r'\b(?:call|text|sms|txt)\b', # Action words
r'\b(?:now|today|immediately|urgent)\b', # Urgency words
]
def extract_features(self, text):
"""Extract comprehensive features from SMS text"""
features = []
# Basic text features
features.append(len(text)) # Length
features.append(len(text.split())) # Word count
features.append(len(text.split('\n'))) # Line count
# Character features
features.append(text.count('!')) # Exclamation marks
features.append(text.count('?')) # Question marks
features.append(text.count('$')) # Dollar signs
features.append(text.count('%')) # Percent signs
features.append(text.count('@')) # At symbols
features.append(text.count('#')) # Hash symbols
features.append(text.count('*')) # Asterisks
# Case features
uppercase_count = sum(1 for c in text if c.isupper())
features.append(uppercase_count)
features.append(uppercase_count / len(text) if len(text) > 0 else 0) # Uppercase ratio
# Number features
number_count = sum(1 for c in text if c.isdigit())
features.append(number_count)
features.append(number_count / len(text) if len(text) > 0 else 0) # Number ratio
# Spam keyword features
text_lower = text.lower()
spam_keyword_count = sum(1 for keyword in self.spam_keywords if keyword in text_lower)
features.append(spam_keyword_count)
features.append(spam_keyword_count / len(text.split()) if len(text.split()) > 0 else 0)
# Pattern matching features
pattern_matches = sum(1 for pattern in self.spam_patterns if re.search(pattern, text))
features.append(pattern_matches)
# Special character features
special_chars = sum(1 for c in text if not c.isalnum() and not c.isspace())
features.append(special_chars)
features.append(special_chars / len(text) if len(text) > 0 else 0)
# Word length features
words = text.split()
if words:
avg_word_length = sum(len(word) for word in words) / len(words)
features.append(avg_word_length)
features.append(max(len(word) for word in words)) # Max word length
else:
features.extend([0, 0])
# Repetition features
features.append(len(set(words)) / len(words) if words else 0) # Unique word ratio
return features
def load_and_preprocess_data(file_path):
"""Load and preprocess the SMS dataset"""
print("Loading SMS dataset...")
# Read the dataset
data = []
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line:
parts = line.split('\t', 1)
if len(parts) == 2:
label, text = parts
data.append({'label': label, 'text': text})
df = pd.DataFrame(data)
print(f"Loaded {len(df)} messages")
print(f"Spam: {len(df[df['label'] == 'spam'])}")
print(f"Ham: {len(df[df['label'] == 'ham'])}")
return df
def train_models(df):
"""Train multiple models and return the best one"""
print("\nTraining models...")
# Prepare data
X_text = df['text'].values
y = df['label'].map({'spam': 1, 'ham': 0}).values
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X_text, y, test_size=0.2, random_state=42, stratify=y
)
# Feature extractor
feature_extractor = SMSFeatureExtractor()
# Extract features
print("Extracting features...")
X_train_features = np.array([feature_extractor.extract_features(text) for text in X_train])
X_test_features = np.array([feature_extractor.extract_features(text) for text in X_test])
# TF-IDF features
vectorizer = TfidfVectorizer(
max_features=5000,
stop_words='english',
ngram_range=(1, 2),
lowercase=True,
strip_accents='unicode'
)
X_train_tfidf = vectorizer.fit_transform(X_train)
X_test_tfidf = vectorizer.transform(X_test)
# Combine features
from scipy.sparse import hstack
X_train_combined = hstack([X_train_tfidf, X_train_features])
X_test_combined = hstack([X_test_tfidf, X_test_features])
# Train models
models = {
'Naive Bayes': MultinomialNB(),
'Logistic Regression': LogisticRegression(random_state=42, max_iter=1000),
'Random Forest': RandomForestClassifier(random_state=42, n_estimators=100)
}
best_model = None
best_score = 0
best_name = ""
for name, model in models.items():
print(f"\nTraining {name}...")
model.fit(X_train_combined, y_train)
# Evaluate
y_pred = model.predict(X_test_combined)
accuracy = accuracy_score(y_test, y_pred)
print(f"{name} Accuracy: {accuracy:.4f}")
print(f"Classification Report:\n{classification_report(y_test, y_pred)}")
if accuracy > best_score:
best_score = accuracy
best_model = model
best_name = name
print(f"\nBest model: {best_name} with accuracy: {best_score:.4f}")
return best_model, vectorizer, feature_extractor, best_name
def save_model(model, vectorizer, feature_extractor, model_name):
"""Save the trained model and components"""
print(f"\nSaving {model_name} model...")
model_data = {
'model': model,
'vectorizer': vectorizer,
'feature_extractor': feature_extractor,
'model_name': model_name
}
with open('sms_spam_model.pkl', 'wb') as f:
pickle.dump(model_data, f)
print("Model saved successfully!")
def main():
"""Main training function"""
print("🚀 SMS Spam Detection Model Training")
print("=" * 50)
# Load data
df = load_and_preprocess_data('spam_collection.txt')
# Train models
model, vectorizer, feature_extractor, model_name = train_models(df)
# Save model
save_model(model, vectorizer, feature_extractor, model_name)
print("\n✅ Training completed successfully!")
print("Model saved as 'sms_spam_model.pkl'")
if __name__ == "__main__":
main()