SpamSlayer là một công cụ machine learning được xây dựng bằng Python để phát hiện email spam sử dụng thuật toán Naive Bayes và Logistic Regression.
- Phát hiện email spam với độ chính xác cao
- So sánh nhiều thuật toán (Naive Bayes vs Logistic Regression)
- Tiền xử lý văn bản tự động (loại bỏ URL, email, số, ký tự đặc biệt)
- Lưu và tải model đã huấn luyện
- Test với email mẫu
SpamSlayer là một công cụ machine learning được xây dựng bằng Python để phát hiện email spam sử dụng thuật toán Naive Bayes và Logistic Regression.
- Phát hiện email spam với độ chính xác cao
- So sánh nhiều thuật toán (Naive Bayes vs Logistic Regression)
- Tiền xử lý văn bản tự động (loại bỏ URL, email, số, ký tự đặc biệt)
- Lưu và tải model đã huấn luyện
- Test với email mẫu
- Python 3.7+
- pip (Python package manager)
git clone <repository-url>
cd SpamSlayer
pip install -r requirements.txt
Hoặc cài đặt thủ công:
pip install pandas scikit-learn
mkdir dataset
# Tải trực tiếp
wget https://github.com/MWiechmann/enron_spam_data/raw/refs/heads/master/enron_spam_data.zip -O dataset/enron_spam_data.zip
# Giải nén
cd dataset
unzip enron_spam_data.zip
Dataset cần có định dạng CSV với các cột:
Subject
: Tiêu đề emailMessage
: Nội dung emailSpam/Ham
: Nhãn (spam hoặc ham)
from spam_detector import SpamDetector
# Khởi tạo detector
detector = SpamDetector()
# Load và huấn luyện
X, y = detector.load_data('dataset/enron_spam_data.csv')
results = detector.train_models(X, y)
# Lưu model
detector.save_model('my_spam_model.pkl')
python main.py
from spam_detector import SpamDetector
# Load model đã lưu
detector = SpamDetector()
detector.load_model('best_spam_detector.pkl')
# Dự đoán
email = "Congratulations! You've won $1000! Click here now!"
prediction = detector.predict(email)
probability = detector.predict_proba(email)
print(f"Prediction: {prediction[0]}")
print(f"Spam probability: {probability[0][1]:.2%}")
SpamSlayer/
├── main.py # File chính để chạy pipeline
├── spam_detector.py # Class SpamDetector
├── utils.py # Các hàm tiện ích (test_detector)
├── data_loader.py # Tải và xử lý dữ liệu
├── requirements.txt # Dependencies
├── README.md # Hướng dẫn này
├── dataset/ # Thư mục chứa dữ liệu
│ └── enron_spam_data.csv
└── models/ # Thư mục lưu model
└── best_spam_detector.pkl
Trong spam_detector.py
, chỉnh sửa models_config
:
models_config = {
'Naive Bayes': MultinomialNB(),
'Logistic Regression': LogisticRegression(max_iter=1000),
'SVM': SVC(probability=True) # Thêm SVM
}
tfidf = TfidfVectorizer(
stop_words='english',
ngram_range=(1, 3), # Thay đổi n-gram
max_features=100000, # Tăng số features
min_df=2, # Tần suất tối thiểu
max_df=0.95 # Tần suất tối đa
)
So sánh kết quả các mô hình:
Model Accuracy Precision Recall F1-Score
Naive Bayes 0.9756 0.9654 0.9724 0.9689
Logistic Regression 0.9834 0.9789 0.9801 0.9795
Best model: Logistic Regression
Xem file Bản sao của SpamSlayer.ipynb
để chạy trên Google Colab với dữ liệu từ Google Drive.
- Kiểm tra đường dẫn tới file CSV
- Đảm bảo file dataset đã được tải về
pip install --upgrade scikit-learn pandas
df = pd.read_csv('dataset/enron_spam_data.csv', encoding='utf-8')
- Fork project
- Tạo feature branch (
git checkout -b feature/AmazingFeature
) - Commit changes (
git commit -m 'Add some AmazingFeature'
) - Push branch (
git push origin feature/AmazingFeature
) - Tạo Pull Request
Distributed under the MIT License. See LICENSE
for more information.
- Author: Thanh Toàn
- Email: lathanhtoan01@gmail.com