sms-classify
基于支持向量機的垃圾郵件分類,使用SVM+flask+vue
數據集和源碼地址
數據集 SMS Spam Collection Data Set 來源于 UCI。樣例被分為非垃圾郵件(86.6%)和垃圾郵件(13.4%),數據格式如下:
ham Go until jurong point, crazy.. Available only in bugis n great world la e buffet...
ham Ok lar... Joking wif u oni...
spam Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005.
ham U dun say so early hor... U c already then say...
代碼結構
- client: 前端實現
- server: 后端實現- models: svm model- sms_classify.py: 垃圾郵件分類實現- SMSSpamCollection: 數據集- app.py 系統實現
- svm.py: 支持向量機算法實現
- svm_test.py:算法test
性能評估
綜合比較了垃圾郵件分類任務在支持向量機、樸素貝葉斯、最近鄰、決策樹算法下的性能,
評估指標包括accuracy、precision、recall、f1-score等。
從accuracy來看,支持向量機的accuracy為98%,是所有測試算法中最高的,可以看出
垃圾郵件分類任務適合使用支持向量機來做。
各算法表現具體如下表:
- 支持向量機:
precision recall f1-score support0 0.98 1.00 0.99 4821 1.00 0.86 0.92 76accuracy 0.98 558macro avg 0.99 0.93 0.96 558
weighted avg 0.98 0.98 0.98 558
支持向量機的accuracy有 98.029%。
- 貝葉斯算法:
precision recall f1-score support0 0.94 1.00 0.97 4821 1.00 0.62 0.76 76accuracy 0.95 558macro avg 0.97 0.81 0.87 558
weighted avg 0.95 0.95 0.94 558
貝葉斯算法的accuracy只有 94.803%。
- 最近鄰算法:
precision recall f1-score support0 0.97 0.99 0.98 4821 0.93 0.83 0.88 76accuracy 0.97 558macro avg 0.95 0.91 0.93 558
weighted avg 0.97 0.97 0.97 558
最近鄰算法的accuracy為 96.774%。
- 決策樹算法:
precision recall f1-score support0 0.97 0.98 0.98 4821 0.88 0.79 0.83 76accuracy 0.96 558macro avg 0.92 0.89 0.90 558
weighted avg 0.96 0.96 0.96 558
決策樹算法的accuracy為 95.699%。
如何運行
首先安裝必要的包
# 創建虛擬環境
python -m venv env
# 激活虛擬環境
source env/bin/activate
# 安裝依賴包
pip install -r requirements.txt
運行SVM算法實現
# 確保安裝 matplotlib 和 numpy
python3 svm_test.py
運行垃圾郵件分類
~ cd server/models/
~ python3 sms_classify.py
運行垃圾郵件分類系統
server端
# 確保安裝必要的包
# 啟動flask
python app.py
client端
# 確保安裝node & npm
npm install
npm run server