採用LibSVM實現One-Class SVM

在One-Class SVM中我們介紹了One-Class SVM的

  1. 應用場景
  2. 
u-SVM
  3. SVDD

本篇我們採用模擬數據,並採用LibSVM來實現One-Class SVM的檢測.


1. 數據模擬

我們用一個點 (x,y) 在單位圓內表示+1,單位圓外表示-1,隨機生成單位圓里的點做為訓練數據,並在[-1,1]中隨機生成的數據作為測試集合。

訓練數據

測試數據

模擬代碼如下所示:

# !/usr/local/bin/python# coding: utf-8import randomimport sysdocument1 = open("train_circle", "w+");document2 = open("test_circle", "w+");def write(line,document): document.write(line)def isInCircle(x,y): 判斷是否在單位圓中 if pow(x,2) + pow(y,2) >1: return False return Trueif __name__ == __main__: for i in range(10000): x = random.uniform(-1,1) y = random.uniform(-1,1) if isInCircle(x,y): write("+1 1:"+str(x)+" 2:"+str(y)+" "+
,document1) write("+1 1:"+str(x)+" 2:"+str(y)+" "+
,document2) else: write("-1 1:"+str(x)+" 2:"+str(y)+" "+
,document2)

訓練數據如下所示:

+1 1:-0.305008847524 2:0.660376967578 +1 1:0.00511085977014 2:-0.691684078353 +1 1:-0.0928898473812 2:0.191786487008 +1 1:-0.338018467287 2:-0.483977800022 +1 1:0.412803967535 2:0.624705892166 +1 1:0.217196162882 2:0.801820514059 +1 1:0.865775129693 2:0.176455450977 +1 1:-0.438003739285 2:0.171723491411 +1 1:0.566102112345 2:-0.659897784552 +1 1:-0.0415844182218 2:-0.628542905901 +1 1:-0.194107528424 2:0.017680013272 +1 1:-0.484127817877 2:-0.438736835228 +1 1:0.876123896458 2:-0.0179453439023 +1 1:-0.0912539974261 2:0.802209691246 +1 1:-0.319771861056 2:-0.219766012212 +1 1:0.365501987116 2:-0.830697741306 +1 1:-0.0146416413454 2:-0.469358964134 +1 1:0.592233111286 2:-0.119020069187 +1 1:0.332707880916 2:0.104474044892 +1 1:-0.149239685683 2:-0.227194546665 +1 1:-0.225918682336 2:-0.915141628603

測試數據如下所示:

+1 1:-0.305008847524 2:0.660376967578 +1 1:0.00511085977014 2:-0.691684078353 +1 1:-0.0928898473812 2:0.191786487008 +1 1:-0.338018467287 2:-0.483977800022 +1 1:0.412803967535 2:0.624705892166 +1 1:0.217196162882 2:0.801820514059 +1 1:0.865775129693 2:0.176455450977 +1 1:-0.438003739285 2:0.171723491411 +1 1:0.566102112345 2:-0.659897784552 +1 1:-0.0415844182218 2:-0.628542905901 +1 1:-0.194107528424 2:0.017680013272 +1 1:-0.484127817877 2:-0.438736835228 +1 1:0.876123896458 2:-0.0179453439023 +1 1:-0.0912539974261 2:0.802209691246 -1 1:0.759173353342 2:-0.707234670203 +1 1:-0.319771861056 2:-0.219766012212 +1 1:0.365501987116 2:-0.830697741306 -1 1:-0.885991151465 2:-0.959934802504 -1 1:-0.65226867931 2:-0.824362726583 +1 1:-0.0146416413454 2:-0.469358964134


2.訓練

../svm-train -s 2 -t 2 -n 0.01 -g 0.01 train_circle*optimization finished, #iter = 92obj = 3041.503318, rho = 77.228993nSV = 81, nBSV = 76

這裡參數包括:

  1. -s 2 表示one-class SVM
  2. -t 2 表示 radial basis function: exp(-gamma*|u-v|^2)
  3. -n 表示模型中的 
u
  4. -g 表示kernel function中的參數 gamma K(x,z)=e^{-gamma ||x-z||^{2}}

3. 測試

../svm-predict test_circle train_circle.model outputAccuracy = 99.22% (9922/10000) (classification)

推薦閱讀:

One-Class SVM介紹
kkt 條件
機器學習技法筆記4:軟邊界SVM
BAT機器學習面試1000題系列(181-185題)
現在還有必要對SVM深入學習嗎?

TAG:機器學習 | SVM | libsvm |