license: mit
---import pandas as pd
import numpy as np
import re
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
1. Load Dataset (Using SMS Spam Collection Dataset)
2. Clean Text
def clean_text(text):
text = text.lower()
text = re.sub(r"[^a-zA-Z0-9\s]", '', text)
return text
df['cleaned_message'] = df['message'].apply(clean_text)
3. Encode Labels
label_encoder = LabelEncoder()
df['label_num'] = label_encoder.fit_transform(df['label']) # ham=0, spam=1
4. Tokenize and Pad Sequences
tokenizer = Tokenizer(num_words=5000, oov_token="")
tokenizer.fit_on_texts(df['cleaned_message'])
sequences = tokenizer.texts_to_sequences(df['cleaned_message'])
max_length = 100
padded_sequences = pad_sequences(sequences, maxlen=max_length, padding='post')
5. Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(padded_sequences, df['label_num'], test_size=0.2, random_state=42)
6. Build LSTM Model
model = Sequential()
model.add(Embedding(input_dim=5000, output_dim=64, input_length=max_length))
model.add(LSTM(64, return_sequences=False))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
7. Train Model
history = model.fit(X_train, y_train, epochs=5, batch_size=32, validation_data=(X_test, y_test))
8. Evaluate Model
loss, accuracy = model.evaluate(X_test, y_test)
print(f"Test Accuracy: {accuracy:.4f}")
9. Plot Accuracy and Loss
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Val Accuracy')
plt.title('Model Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.title('Model Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
10. Predict on new message
def predict_message(text):
cleaned = clean_text(text)
seq = tokenizer.texts_to_sequences([cleaned])
padded = pad_sequences(seq, maxlen=max_length, padding='post')
pred = model.predict(padded)[0][0]
return "SPAM" if pred > 0.5 else "HAM"
Test
print(predict_message("Congratulations! You've won a free iPhone. Click here now."))
print(predict_message("Hey, are you coming to class today?"))