ãããªãèªåã§ãã¼ãã«ä¸ãã¦ã¿ã¾ããï¼ç¬ï¼ãã¡ãªã¿ã«ä½æ
åç´ãã¼ã»ãããã³ãæåã«æã£ã¦ããã®ãï¼ã¨ããã¨ãid:echizen_tmããã®ブログ記事ã§ã触ãã¦ããããéã
æ©æ¢°å¦ç¿ã«ã¯å¤§ããåãã¦ãèå¥é¢æ°ããèå¥ã¢ãã«ããçæã¢ãã«ãã®3ã¤ã®ç¨®é¡ãããããã®ãªãã§èå¥é¢æ°ã¯ç¢ºçã使ããªãã®ã§åå¿è ãå ¥éããã®ã«æé©ã
èå¥é¢æ°ã§æåãªã®ã¯SVM(Support Vector Machineããµãã¼ããã¯ã¿ã¼ãã·ã³)ãååãèãããã¨ããã人ãå¤ãã¨æããããã§æ©éSVMãå¦ã¼ãã¨ããã¨æ·å± ãé«ãã¦æ«æããã¡ã
å®ã¯SVMã¯(大éæã«è¨ãã¨)ãã¼ã»ãããã³ã¨ããåºç¤çãªèå¥é¢æ°ã«ããã¼ã¸ã³æ大åãã¨ãã«ã¼ãã«é¢æ°ãã¨ããèãæ¹ãå°å ¥ãããã®ããªã®ã§æ©æ¢°å¦ç¿å ¥éè ã¯æåã«ãã¼ã»ãããã³ãå¦ã¶ã®ãè¯ãã¨æãããã
ãããããå®éã«åã以åMatlabã§ç³ã³ã¼ãæ¸ããªããåå¼·ãã¦ãæã¯ãã¯ãåç´ãã¼ã»ãããã³ããå
¥ã£ãããã§ããã¨ãããã¨ã§ãåã®ããã°ã§ãåç´ãã¼ã»ãããã³ããå
¥ããã¨æãã¾ãã
åç´ãã¼ã»ãããã³ã¨ã¯ä½ãã
以å社å
ã®åå¼·ä¼ã§è©±ããæã®ã¹ã©ã¤ããSlideShareã«ä¸ãã¦ããã¾ãï¼ã½ã¼ã¹é¨åã¯Matlabã³ã¼ãã ã£ãããå²æï¼*1ã
ããããã§å¾ã¯ã¹ã©ã¤ãã«æ¸ããå®ç¾©ããã®ã¾ã¾Pythonã§ãã¿ã£ã¨æ¸ãã ãï¼ã»ã»ã»ã¨ãããã¨ã§ãã¹ã©ã¤ãä¸éããèªã¿ãã ãããä»åã¯ãã¾ãã¾ã¹ã©ã¤ãããã£ãã®ã§ãææãã«ãªã£ã¦ãã¾ã£ã¦ãã¿ã¾ãã*2ã
å®éã«Pythonã§ç³ã³ã¼ããæ¸ãã¦ã¿ã
注ï¼ããããå
ã¯ãæä½ã§ãä¸è¨slideshareãåèã«ãã¦ä¸åº¦ã¯Pythonã§çµãã§ã¿ãå¾ã§ãèªãããã«ãã¦ä¸ããï¼ãããªãã¨ãæ©æ¢°å¦ç¿ã®ã¢ã«ã´ãªãºã ãç¥ã£ãä¸ã§å®è£
ããã¨ããã¹ãã«ã¯èº«ã«ä»ããªãã§ãï¼
ã»ã»ã»ã¨ããããã§åãæ¸ãã¦ã¿ããã®ã¯ä»¥ä¸ã®éããæ¥ãå¿ãã§å
¬éãã¾ããç³ã³ã¼ããªã®ã¯åã®ã³ã¼ãã£ã³ã°ã¹ãã«ãç³ã ããã§ãï¼æ³£ï¼ãã½ã¼ã¹ã¯GitHubã«ããã¾ãã
# -*- coding: utf-8 -*- # æ¼ç®ç¨ã«NumPyããããããç¨ã«matplotlibãimport import numpy as np import matplotlib.pyplot as plt # èå¥é¢æ°ã®æ¬ä½ï¼y=w'xãè¨ç®ãã¦ãã ã def predict(wvec,xvec): out=np.dot(wvec,xvec) if out>=0: res=1 else: res=-1 return [res,out] # å¦ç¿é¨ï¼èå¥é¢æ°ã«å¦ç¿ãã¼ã¿ãé ç¹°ãã«å ¥ãã¦ã # éã¿ãã¯ãã«ãæ´æ°ãã def train(wvec,xvec,label): [res,out]=predict(wvec,xvec) c=0.5 # å¦ç¿ä¿æ°ã大ãéãã¦ãã¾ã¨ãã«å¦ç¿ãã¦ãããªãã®ã§1æªæºããã㧠if out*label<0: wtmp=wvec+c*label*xvec return wtmp else: return wvec # 以ä¸æ¬ä½ if __name__=='__main__': item_num=100 # å¦ç¿ãã¼ã¿ã¯100å loop=1000 # åæå¤å®ã¯ä¸åãã¦ãªããã©ã¨ãããã1000åã«ã¼ã init_wvec=[1,-1,1] # éã¿ãã¯ãã«ã®åæå¤ãé©å½ # å¦ç¿ãã¼ã¿ã¯xyå¹³é¢ã®ç¬¬1象éã¨ç¬¬3象éã«50åã㤠# np.random.randomã§ã°ãã¤ããã¦ã¿ã x1_1=np.ones(int(item_num/2))+10*np.random.random(int(item_num/2)) x1_2=np.ones(int(item_num/2))+10*np.random.random(int(item_num/2)) x2_1=-np.ones(int(item_num/2))-10*np.random.random(int(item_num/2)) x2_2=-np.ones(int(item_num/2))-10*np.random.random(int(item_num/2)) z=np.ones(int(item_num/2)) # ããã¤ã¯ãã¤ã¢ã¹é ãä»åã¯ç¡è¦ãã¦1ã«åºå® # å¦ç¿ãã¼ã¿ã1ã¤ã®ãããªã¯ã¹ã«ã¾ã¨ãã x1=np.c_[x1_1,x1_2,z] x2=np.c_[x2_1,x2_2,z] class_x=np.array(np.r_[x1,x2]) # æ師ã©ãã«ã1 or -1ã§æ¯ã£ã¦1ã¤ã®ãã¯ãã«ã«ã¾ã¨ãã label1=np.ones(int(item_num/2)) label2=-1*np.ones(int(item_num/2)) label_x=np.array(np.r_[label1,label2]) # NumPy.ndarrayã¯append()ã¡ã½ããã使ããªãã®ã§ # ç³ã³ã¼ããããåæarrayãä½ã£ã¦ããã«åç´ã«è¶³ãã¦ããçãåã wvec=np.vstack((init_wvec,init_wvec)) # ã«ã¼ãåæ°ã®åã ãç¹°ãè¿ãã¤ã¤ãéã¿ãã¯ãã«ãå¦ç¿ããã for j in range(loop): for i in range(item_num): wvec_new=train(wvec[-1],class_x[i,:],label_x[i]) wvec=np.vstack((wvec,wvec_new)) w=wvec[-1] # éã¿ãã¯ãã«ãåç´ã«è¶³ãã¦ãã£ãæå¾ã®ãã®ãæ¡ç¨ãã print w # åé¢ç´ç·ãå¼ã x_fig=range(-15,16) y_fig=[-(w[1]/w[0])*xi-(w[2]/w[1]) for xi in x_fig] # 漫ç¶ã¨ãããããã plt.scatter(x1[:,0],x1[:,1],marker='o',color='g',s=100) plt.scatter(x2[:,0],x2[:,1],marker='s',color='b',s=100) plt.plot(x_fig,y_fig) plt.show()
ã¨ãããã¨ã§ãå®è¡ããã¨å¤§ä½ãããªæãã«ãªãã¾ãã
ãããããåæå¤å®ä¸åããã¦ãªãã®ã§ããã¿ã©ã¡ãªåé¢ç´ç·ã«ãªããã¨ãããã¾ãï¼ç横ã¨ãï¼ãåæå¤å®ãããã人ã¯ãä¾ãã°2åç®ä»¥éã®ã«ã¼ãã§éã¿ãã¯ãã«wvecã®æ´æ°éã«ä¸å®ã®æã¡åãåºæºãè¨ããã¨ãããã°è¯ãããªã¼ã¨æãã¾ã*3ã
ãã¤ã³ãã¯ã__main__以å¤ã«ã¯2ã¤ããé¢æ°ãè¦ããªãã¨ãããããããã®é¢æ°ã¯è¶
ã«ã³ã¿ã³ãã¶ã£ã¡ããããã®ã³ã¼ãæ¸ãæã«è¦å´ããã®ã¯Pythonã®å¶å¾¡æ§é ã¨NumPy arrayã®ä½¿ãæ¹ã®ã¨ãããããã§ã*4ã
æ©æ¢°å¦ç¿ã¯ãæ¸ãã¦ã¿ãã°æå¤ã¨ã«ã³ã¿ã³ã
ã¨ããããã§ãPythonã§æ¸ãã¦ã¿ã¾ããããã®ããã¼ãç°¡åã§ãã*5ã
ãã¼ã»ãããã³ã«éãããæ©æ¢°å¦ç¿ã®å¤ãã¯æ°å¦çã¢ã«ã´ãªãºã ã®å®ç¾©ããå³ãããã¦é£è§£ã«è¦ãã¾ãããå®ã¯å¼èªä½ã®å½¢ãã³ã¼ãã§æ¸ãã®ã«é©ãã¦ã*6ãã¨ãå¤ãã®ã§ãã³ã¼ãã«ãã¦ãã¾ãã°ã«ã³ã¿ã³ã¨ãããã¨ãå¾ã ã«ãã¦ããã¾ãã
ãªã®ã§ãæ©æ¢°å¦ç¿ã®ã¢ã«ã´ãªãºã ã«èå³ãæã£ãããã¾ãã¯èªåã§æãåããã¦ã³ã¼ããçµãã§ãã¾ããã¨ããã²ãè¦ããã¾ããæ¦è¦ããããããæåãããããããã©ã¡ã¼ã¿ãè²ã 試ãã°ãã®ã¢ã«ã´ãªãºã ã®ç¹å¾´ããããã®ã§*7ããã£ã¦æã¯ãªãã§ããï¼
*1:ä½è«ã ãã©slideshareãã©ããã£ã¦embedãããåãããªãã£ãã®ã§èª¿ã¹ã¦ãã¾ã£ãâ http://staff.hatenablog.com/entry/2012/02/27/174925
*2:次å以éã¯ã¡ããã¨å®ç¾©å¼ã®ç°¡åãªèª¬æã¨ãããã°ä¸ã§ãããã¨æã£ã¦ã¾ããããããæ¬æ°ã§ããï¼ï¼
*3:ç³ã³ã¼ãããæ¸ããªã人éã®è¨èãªã®ã§èª¬å¾åããã¾ããã
*4:ã§ãç³ã³ã¼ãã§æ±ãããã³ã©ãã¨ãçªã£è¾¼ã¾ãªãã§ã¼
*5:Pythonèªä½ãé£ããã¨æã£ãããªãã¯æ®å¿µãªããåã¨åã¬ãã«ã§ãç¬
*6:Σã¨ãÎ ã¨ãå ç©ã¨ã
*7:SVMã®æ±åæ§è½ãªããã¯çµ¶å¯¾ã«ã³ã¼ãæ¸ãã¦ãã©ã¡ã¼ã¿å¤ããªããåé¢è¶ å¹³é¢ãçºãã¦ã¿ãªãã¨ããããªã