Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Ubuntu 下安裝與使用 LIBSVM 完整教學(含 Python 工具)

編輯:Linux 教學

LIBSVM 是廣泛使用的支援向量機(SVM)函式庫。本文記錄在 Ubuntu 環境下從下載、編譯到實際訓練/預測的完整流程,並介紹 easy.py、grid.py 自動化參數搜尋工具。

一、下載與解壓縮

wget https://www.csie.ntu.edu.tw/~cjlin/libsvm/libsvm-2.91.tar.gz
tar -zxvf libsvm-2.91.tar.gz
cd libsvm-2.91

二、編譯與常見錯誤排除

make

若出現 make: g++: Command not found,表示缺少 C++ 編譯器:

sudo apt-get update
sudo apt-get install g++

安裝完成後再執行 make 即可成功產生 svm-train、svm-predict、svm-scale 等執行檔。

三、基本訓練與預測流程

  1. 訓練模型:./svm-train heart_scale
    產生 heart_scale.model。
  2. 預測:./svm-predict heart_scale heart_scale.model out
    out 為預測結果輸出檔。

四、使用 easy.py 自動化參數搜尋與預測

Ubuntu 內建 Python 與 gnuplot,可直接使用 tools/ 目錄下的腳本:

cd tools
python easy.py ../SYN ../SYN2NMF
  • SYN:訓練資料檔
  • SYN2NMF:測試資料檔

執行過程會自動進行縮放、網格搜尋(grid search)、交叉驗證,並輸出最佳參數與準確率:

Best c=2.0, g=0.0078125 CV rate=77.45
Training...
Output model: SYN.model
Scaling testing data...
Testing...
Accuracy = 83.35% (1667/2000) (classification)
Output prediction: SYN2NMF.predict

五、小提醒

  • 建議先執行 sudo -i 切換至 root 環境,避免權限問題。
  • 資料格式須符合 LIBSVM 規範(每行:label index:value ...)。
  • 若需呼叫 C/C++ 介面,請參考 svm.h 與範例程式。

完成上述步驟,即可在 Ubuntu 上順利執行 LIBSVM 進行分類與迴歸分析。

Copyright © Windows教程網 All Rights Reserved