ã½ãããã¼ã¸ã³SVM
ååï¼2010/5/2ï¼ã®ãã¼ããã¼ã¸ã³SVMã§ã¯ããã¼ã¿ã«éãªããããå ´åãä¸ã®ããã«ã¡ããã¨åé¡å¢çãæ±ãããã¾ããã§ãããä»åã¯ãéãªãã®ããã¯ã©ã¹åå¸ã«å¯¾å¿ã§ããããã«æ¡å¼µãã¦ã¿ã¾ãããã®ãããªSVMã¯ãã¼ããã¼ã¸ã³SVMã«å¯¾ãã¦ã½ãããã¼ã¸ã³SVMã¨å¼ã°ãã¾ããå¥åã¨ãã¦C-SVMã¨ãå¼ã°ããããã§ãã
PRMLã®7.1.1ã«ããããã«ããã¼ã¿ã®èª¤åé¡ã許ãããã«SVMãä¿®æ£ãã¾ãããã¼ããã¼ã¸ã³SVMã§ã¯ããã¼ã¿ç¹ããã¼ã¸ã³å ï¼-1 < y < 1ï¼ã«çµ¶å¯¾ã«å ¥ããªããã¨ãåæã«ãã¦ãã¾ããããã½ãããã¼ã¸ã³SVMã§ã¯ãå ¥ã£ã¦ãã¾ã£ããã®ã¯ä»æ¹ãªããã ãããã«ãã£ãä¸ããï¼ãã¨å°ãæ¡ä»¶ãç·©ãã¾ãã
ã¾ããã¹ã©ãã¯å¤æ°Î¶ï¼ã¼ã¼ã¿ï¼ããã¼ã¿ãã¨ã«å°å ¥ãã¾ããã¹ã©ãã¯å¤æ°ã¯ããã¼ã¿ãæ£ããåé¡ãããã¤ãã¼ã¸ã³å¢çä¸ã¾ãã¯å¤å´ã«ããå ´åã¯0ãæ£ããåé¡ããã¦ããããã¼ã¸ã³å ã«ä¾µå ¥ãã¦ãã¾ã£ã¦ãããã¼ã¿ã¯0 < ζ <= 1ãåé¡å¢çãè¶ãã¦èª¤åé¡ããã¦ãã¾ã£ããã¼ã¿ã¯Î¶ > 1ã¨ãªãã¾ãããã¼ããã¼ã¸ã³SVMã§ã¯ãå¾ã®2ã¤ã®ããã«ãã¼ã¸ã³å ã«ä¾µå ¥ãããã誤åé¡ããããã¨ã¯ä»®å®ãã¦ãªãã£ãããã§ããï¼ã§ããã¼ã¸ã³æ大åããããã§ãããä¸è¨ã®ã¹ã©ãã¯å¤æ°ãããã«ãã£ã¨ãã¦ä¸ãã¾ãã
ãã¼ã¸ã³ã¯ã1/|w|ããä¸ã®å¼ã¯æ大åã§ã¯ãªãæå°åãç®æãã®ã§æ³¨æã§ããããã§ãC > 0ã¯ã¹ã©ãã¯å¤æ°ãç¨ãã¦è¡¨ãããããã«ãã£ã¨ããã¼ã¸ã³ã®å¤§ããã®éã®ãã¬ã¼ããªããå¶å¾¡ãããã©ã¡ã¼ã¿ã§ãããã®ãã©ã¡ã¼ã¿ã®Cããã¨ã£ã¦ã½ãããã¼ã¸ã³SVMãC-SVMã¨å¼ã°ããã®ã ã¨æãã¾ããν-SVMï¼ãã¥ã¼SVMï¼ã¨ããã®ããããã§ããããã®Î½ããã©ã¡ã¼ã¿ãªã®ã§ã誤åé¡ãå¤ãã»ã©ï¼7.21ï¼ã®ç¬¬1é ã¹ã©ãã¯å¤æ°ã®åè¨ã¯å¤§ãããªãã®ã§æå°åããã®ã«å¦¨ãã¨ãªãã¾ãããªã®ã§ãæå°åãããã¨ããã¨ãªãã¹ã誤åé¡ãå°ãªããªãããã«ãã©ã¡ã¼ã¿wã調æ´ãããã¨ããã¯ãã§ããï¼ï¼Î¶ã®å®ç¾©ã«ãwãå«ã¾ãã¦ã¾ãï¼æ£ååãããã§ãã誤差é¢æ°ã«é ãä»ãå ãã¦ããããã«ãããã£ã¦ã®ã¯èªåã«ã¨ã£ã¦æ°é®®ãªèãæ¹ã§ãããæé©åæãã¹ããã¡ãªã¿ã«ãã¼ããã¼ã¸ã³SVMã¯Cââã®ã¨ãã§ããããã«ãã£ããã®ããã大ããã®ã§èª¤ãã¯çµ¶å¯¾ã«è¨±ããªãï¼ã£ã¦ãã¨ã§ãããããã¯å¾ã§è©¦ãã¦ã¿ã¾ãããã
ãã¼ããã¼ã¸ã³SVMã®ã¨ãã¨åæ§ã«ã©ã°ã©ã³ã¸ã¥é¢æ°ããã¦ã¦ã©ã°ã©ã³ã¸ã¥ä¹æ°aãç¨ããå対表ç¾ã«å¤æããããã§ãããå対表ç¾ãä½ã¨ãã¼ããã¼ã¸ã³SVMã®ã¨ãã¨ã¾ã£ããåãã«ãªã£ã¦ãã¾ãã¾ãã
å®éã«å°åºãã¦ã¿ã¾ãã¨ãã¹ã©ãã¯å¤æ°Î¶ãå ¥ã£ãé ã¯+ã¨-ã§ãããã«ç¸æ®ºããã¦å ¨é¨æ¶ãã¦ãã¾ãã¾ããããããã¼ããã¼ã¸ã³SVMã¨åããããªããã¨æã£ãã®ã§ãããå¶ç´æ¡ä»¶ãéããã§ããã¼ãã½ãããã¼ã¸ã³SVMã®ã©ã°ã©ã³ã¸ã¥ä¹æ°aã®å¶ç´æ¡ä»¶ã¯ã
ã§ãããã¼ããã¼ã¸ã³SVMã®å¶ç´æ¡ä»¶ã¯ï¼7.11ï¼ï¼7.12ï¼ã§ããã
ä½ãéããã¨ããã¨ã©ã°ã©ã³ã¸ã¥ä¹æ°ã«C以ä¸ã¨ããæ¡ä»¶ãå ãã£ãã ãã§ãããããã ãã¹ã©ãã¯å¤æ°äºã ã¨å°å ¥ãã¦ããã®ã«çµå±ãã©ã¡ã¼ã¿Cããæ®ããªããªãã¦ãä½ããããã§ããã§ã¯ãæ©éãã®ãã©ã¡ã¼ã¿Cã®ããããããã°ã©ãã³ã°ãã¦è©¦ãã¦ã¿ã¾ãã
#coding:utf-8 # éç·å½¢SVMï¼ã½ãããã¼ã¸ã³ï¼ # cvxoptã®Quadratic Programmingã解ãé¢æ°ãä½¿ç¨ import numpy as np from scipy.linalg import norm import cvxopt import cvxopt.solvers from pylab import * N = 200 # ãã¼ã¿æ° C = 0.5 # ã¹ã©ãã¯å¤æ°ãç¨ãã¦è¡¨ãããããã«ãã£ã¨ãã¼ã¸ã³ã®ãã¬ã¼ããªããã©ã¡ã¼ã¿ SIGMA = 0.3 # ã¬ã¦ã¹ã«ã¼ãã«ã®ãã©ã¡ã¼ã¿ # å¤é å¼ã«ã¼ãã« def polynomial_kernel(x, y): return (1 + np.dot(x, y)) ** P # ã¬ã¦ã¹ã«ã¼ãã« def gaussian_kernel(x, y): return np.exp(-norm(x-y)**2 / (2 * (SIGMA ** 2))) # ã©ã¡ãã®ã«ã¼ãã«é¢æ°ã使ããããã§æå® kernel = gaussian_kernel def f(x, a, t, X, b): sum = 0.0 for n in range(N): sum += a[n] * t[n] * kernel(x, X[n]) return sum + b if __name__ == "__main__": # è¨ç·´ãã¼ã¿ããã¼ã data = np.genfromtxt("classification.txt") X = data[:,0:2] t = data[:,2] * 2 - 1.0 # æ師信å·ã-1 or 1ã«å¤æ # ã©ã°ã©ã³ã¸ã¥ä¹æ°ãäºæ¬¡è¨ç»æ³ï¼Quadratic Programmingï¼ã§æ±ãã K = np.zeros((N, N)) for i in range(N): for j in range(N): K[i, j] = t[i] * t[j] * kernel(X[i], X[j]) Q = cvxopt.matrix(K) p = cvxopt.matrix(-np.ones(N)) temp1 = np.diag([-1.0]*N) temp2 = np.identity(N) G = cvxopt.matrix(np.vstack((temp1, temp2))) temp1 = np.zeros(N) temp2 = np.ones(N) * C h = cvxopt.matrix(np.hstack((temp1, temp2))) A = cvxopt.matrix(t, (1,N)) b = cvxopt.matrix(0.0) sol = cvxopt.solvers.qp(Q, p, G, h, A, b) a = array(sol['x']).reshape(N) print a # ãµãã¼ããã¯ãã«ã®ã¤ã³ããã¯ã¹ãæ½åº S = [] M = [] for n in range(len(a)): if 0 < a[n]: S.append(n) if 0 < a[n] < C: M.append(n) # bãè¨ç® sum = 0 for n in M: temp = 0 for m in S: temp += a[m] * t[m] * kernel(X[n], X[m]) sum += (t[n] - temp) b = sum / len(M) print S, b # è¨ç·´ãã¼ã¿ãæç» for n in range(N): if t[n] > 0: scatter(X[n,0], X[n,1], c='b', marker='o') else: scatter(X[n,0], X[n,1], c='r', marker='o') # ãµãã¼ããã¯ãã«ãæç» # for n in S: # scatter(X[n,0], X[n,1], s=80, c='c', marker='o') # èå¥å¢çãæç» X1, X2 = meshgrid(linspace(-2,2,50), linspace(-2,2,50)) w, h = X1.shape X1.resize(X1.size) X2.resize(X2.size) Z = array([f(array([x1, x2]), a, t, X, b) for (x1, x2) in zip(X1, X2)]) X1.resize((w, h)) X2.resize((w, h)) Z.resize((w, h)) CS = contour(X1, X2, Z, [0.0], colors='k', linewidths=1, origin='lower') for n in S: print f(X[n], a, t, X, b) xlim(-2, 2) ylim(-2, 2) show()
ãµãã¼ããã¯ãã«ã¨bã®æ±ãæ¹ããã¼ããã¼ã¸ã³SVMã¨å°ãéãã¾ãã詳ããã¯PRMLãåç §ãã¦ãã ãããã¾ããã¡ãã£ã¨ããå®è£ ä¸ã®ãã¯ããã¯ã§ãããæé©åã©ã¤ãã©ãªcvxoptã®äºæ¬¡è¨ç»æ³ã®å¶ç´æ¡ä»¶ã¯Ga <= hã®ããã«1ã¤ã®è¡åã®å½¢å¼ã§ã¾ã¨ããå¿ è¦ãããã¾ãã0 < a < Cã1ã¤ã®è¡åã§ã©ããã£ã¦æ¸ãã°ãããå°ãæ©ãã ã®ã§ãããããªãå¼·å¼ã«2ã¤ã®è¡åããã£ã¤ããå½¢ã§ä¸ã®ããã«æ¸ãã¾ããããã¨ãã°ããã¼ã¿æ°N=2ã®å ´åã«ã¯ãä¸ã®ããã«Gã¨hã決ããã¨ã»ããã£ã4ã¤ã®ä¸çå¼ãå¾ããã¾ããä¸ã®ããã°ã©ã ã§ã¯temp1ã¨temp2ã¨ããä¸ååã®è¡åã¨ä¸ååã®è¡åãç©ã¿éãã¦Gã¨hãä½ã£ã¦ã¾ããããã§ã¡ããã¨å¶ç´ããã¦ããã®ãcvxoptã®ä¸èº«ãè¦ã¦ãªãã®ã§ããããªãã®ã§ããçµæãæ£ãããããªã®ã§OKãªã®ã§ãããã
ã§ã¯ãçµæã§ããã¬ã¦ã·ã¢ã³ã«ã¼ãã«ãç¨ãããã©ã¡ã¼ã¿ã¯C=0.5ã§ããã¬ã¦ã·ã¢ã³ã«ã¼ãã«ã®ãã©ã¡ã¼ã¿Ïã¯ä¸ãã0.1, 0.3, 1.0ã§ãã
ã©ãããã¬ã¦ã·ã¢ã³ã«ã¼ãã«ã®ãã©ã¡ã¼ã¿Ïã大ããããã¨åé¡å¢çãæ»ããã«ãªãããã§ãããããã¯ãã¬ã¦ã¹åå¸ã®åæ£ã大ããã»ã©æ»ããã«ãªãã¤ã¡ã¼ã¸ã¨ç´æçã«ãåã£ã¦ããããã§ããã§ã¯ã次ã«ã¬ã¦ã·ã¢ã³ã«ã¼ãã«ã®ãã©ã¡ã¼ã¿ã¯Ï=0.3ã§åºå®ãã¦Cã20, 1000, 1e+10ã¨ãã¦ã¿ã¾ãã
ãã¼ããCã大ããããã¨èª¤åé¡ã®ããã«ãã£ã大ãããªããããããåé¡å¢çãã©ããªã«ç´°ãããªã£ã¦ãããããä½ã¨ãåé¡ãããã ï¼ãã¨ããæå°ãä¼ãã£ã¦ãã¾ããï¼ç¬ï¼å®éã¯ãç´°ããããã¨æ±åæ§è½ãè½ã¡ãã®ã§ããã«ãã£ã大ããããã®ãèããã®ã ã¨æãã¾ããã»ã»ã»ããã¯ããããã«ãã£ã大ããããã«ç®å ã®ææã«ã¨ããããã¨å¤§å±ãè¦å¤±ã£ã¦ãã¾ããã¨ããããæè¨ã«ãªãããã§ããã¾ããåè«ã¯ãã¦ãããCââã®ã¨ããã¼ããã¼ã¸ã³SVMã«ãªãããã§ãããå®å ¨ã«åé¢ãããã¨ããã¨ããæå³ã§ã¯ãããªã£ã¦ããã§ãããå®éãC=1e+10ã§ã¯å®å ¨ã«åé¢ã§ãã¦ãã¾ãã
調æ´ããªãã¡ããããªããã©ã¡ã¼ã¿ã¯ï¼C, Ïï¼ã§ããéç·å½¢SVMï¼2010/5/2ï¼ã§ãæ¸ãã¾ããããæé©ãªãã©ã¡ã¼ã¿ã¯ãåé¡ã«ãããã¦å®é¨ã«ãã£ã¦æ±ããã®ãæ®éã ããã§ããã°ãªãããµã¼ãã¨ãã£ã¦Cã¨Ïã®çµã¿åããã®ã°ãªãããã¤ããç¶²ç¾ çã«åé¡ç²¾åº¦ãè©ä¾¡ããã®ãããã¨ã®ãã¨ã
ä¸å¿ããµãã¼ããã¯ãã«ãã·ã³ã¯ä»åãæå¾ã§ããäºæ¬¡è¨ç»æ³ä»¥å¤ã¯èªåã§å®è£ ãã¦ã¿ã¾ããããæ®éã¯è»è¼ªã®åçºæã¯ããã«æ¢åã®ã©ã¤ãã©ãªã使ãã¾ãï¼ãã¡ãããSVMã®ã¢ã«ã´ãªãºã èªä½ãç 究ããå ´åã¯å¥ã§ããï¼ã朱鷺の杜ã«ãããããªã©ã¤ãã©ãªã¸ã®ãªã³ã¯ãããã®ã§åèã«ãã¦ãã ãããä»åº¦ã¯ãããã¹ããç»åãªã©å ·ä½çãªåé¡ã«å¿ç¨ãã¦ã¿ããã¨æãã¾ããä¹ããæå¾ ï¼
åèæç®
- サポートベクターマシン入門ï¼PDFï¼
- 痛快!サポートベクトルマシン : 古くて新しいパターン認識手法 - ãã¬ãã¥ã¼ãã¯ãªãã¯ããã¨PDFãè½ã¨ãã¾ã
- Support Vector Machine って,なに? - 説æããããããã
- やる夫で学ぶSVM with R - ãã夫ã£ã¦ä½ããããããï¼
- やる夫で学ぶ非線形なSVM
- A Practical Guide to Support Vector Classification - SVMã§é«ç²¾åº¦ããããåºããã³ããæºè¼
ã«ã¼ãã«æ³ã«é¢ãã¦ã¯ãã®æ¬ãè©å¤ããããã§ããæ©ä¼ãããã°èªã¿ããã
- ä½è : 赤ç©æ太é
- åºç社/ã¡ã¼ã«ã¼: 岩波æ¸åº
- çºå£²æ¥: 2008/11/27
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 7人 ã¯ãªãã¯: 180å
- ãã®ååãå«ãããã° (32件) ãè¦ã