欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python機(jī)器學(xué)習(xí)之SVM支持向量機(jī)

 更新時(shí)間:2017年12月27日 09:49:02   作者:lsldd  
這篇文章主要為大家詳細(xì)介紹了Python機(jī)器學(xué)習(xí)之SVM支持向量機(jī),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

SVM支持向量機(jī)是建立于統(tǒng)計(jì)學(xué)習(xí)理論上的一種分類算法,適合與處理具備高維特征的數(shù)據(jù)集。
SVM算法的數(shù)學(xué)原理相對(duì)比較復(fù)雜,好在由于SVM算法的研究與應(yīng)用如此火爆,CSDN博客里也有大量的好文章對(duì)此進(jìn)行分析,下面給出幾個(gè)本人認(rèn)為講解的相當(dāng)不錯(cuò)的:
支持向量機(jī)通俗導(dǎo)論(理解SVM的3層境界)
JULY大牛講的是如此詳細(xì),由淺入深層層推進(jìn),以至于關(guān)于SVM的原理,我一個(gè)字都不想寫了。。強(qiáng)烈推薦。
還有一個(gè)比較通俗的簡單版本的:手把手教你實(shí)現(xiàn)SVM算法

SVN原理比較復(fù)雜,但是思想很簡單,一句話概括,就是通過某種核函數(shù),將數(shù)據(jù)在高維空間里尋找一個(gè)最優(yōu)超平面,能夠?qū)深悢?shù)據(jù)分開。

針對(duì)不同數(shù)據(jù)集,不同的核函數(shù)的分類效果可能完全不一樣。可選的核函數(shù)有這么幾種:
線性函數(shù):形如K(x,y)=x*y這樣的線性函數(shù);
多項(xiàng)式函數(shù):形如K(x,y)=[(x·y)+1]^d這樣的多項(xiàng)式函數(shù);
徑向基函數(shù):形如K(x,y)=exp(-|x-y|^2/d^2)這樣的指數(shù)函數(shù);
Sigmoid函數(shù):就是上一篇文章中講到的Sigmoid函數(shù)。

我們就利用之前的幾個(gè)數(shù)據(jù)集,直接給出Python代碼,看看運(yùn)行效果:

測試1:身高體重?cái)?shù)據(jù)

# -*- coding: utf-8 -*- 
import numpy as np 
import scipy as sp 
from sklearn import svm 
from sklearn.cross_validation import train_test_split 
import matplotlib.pyplot as plt 
 
data  = [] 
labels = [] 
with open("data\\1.txt") as ifile: 
    for line in ifile: 
      tokens = line.strip().split(' ') 
      data.append([float(tk) for tk in tokens[:-1]]) 
      labels.append(tokens[-1]) 
x = np.array(data) 
labels = np.array(labels) 
y = np.zeros(labels.shape) 
y[labels=='fat']=1 
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.0) 
 
h = .02  
# create a mesh to plot in 
x_min, x_max = x_train[:, 0].min() - 0.1, x_train[:, 0].max() + 0.1 
y_min, y_max = x_train[:, 1].min() - 1, x_train[:, 1].max() + 1 
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), 
           np.arange(y_min, y_max, h)) 
 
''''' SVM ''' 
# title for the plots 
titles = ['LinearSVC (linear kernel)', 
     'SVC with polynomial (degree 3) kernel', 
     'SVC with RBF kernel', 
     'SVC with Sigmoid kernel'] 
clf_linear = svm.SVC(kernel='linear').fit(x, y) 
#clf_linear = svm.LinearSVC().fit(x, y) 
clf_poly  = svm.SVC(kernel='poly', degree=3).fit(x, y) 
clf_rbf   = svm.SVC().fit(x, y) 
clf_sigmoid = svm.SVC(kernel='sigmoid').fit(x, y) 
 
for i, clf in enumerate((clf_linear, clf_poly, clf_rbf, clf_sigmoid)): 
  answer = clf.predict(np.c_[xx.ravel(), yy.ravel()]) 
  print(clf) 
  print(np.mean( answer == y_train)) 
  print(answer) 
  print(y_train) 
 
  plt.subplot(2, 2, i + 1) 
  plt.subplots_adjust(wspace=0.4, hspace=0.4) 
   
  # Put the result into a color plot 
  z = answer.reshape(xx.shape) 
  plt.contourf(xx, yy, z, cmap=plt.cm.Paired, alpha=0.8) 
   
  # Plot also the training points 
  plt.scatter(x_train[:, 0], x_train[:, 1], c=y_train, cmap=plt.cm.Paired) 
  plt.xlabel(u'身高') 
  plt.ylabel(u'體重') 
  plt.xlim(xx.min(), xx.max()) 
  plt.ylim(yy.min(), yy.max()) 
  plt.xticks(()) 
  plt.yticks(()) 
  plt.title(titles[i]) 
   
plt.show() 

運(yùn)行結(jié)果如下:

可以看到,針對(duì)這個(gè)數(shù)據(jù)集,使用3次多項(xiàng)式核函數(shù)的SVM,得到的效果最好。

測試2:影評(píng)態(tài)度

下面看看SVM在康奈爾影評(píng)數(shù)據(jù)集上的表現(xiàn):(代碼略)

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,  kernel='linear', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)
0.814285714286

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,  kernel='poly', max_iter=-1, probability=False, random_state=None,  shrinking=True, tol=0.001, verbose=False)
0.492857142857

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,  kernel='rbf', max_iter=-1, probability=False, random_state=None,  shrinking=True, tol=0.001, verbose=False)
0.492857142857

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,  kernel='sigmoid', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)
0.492857142857

可見在該數(shù)據(jù)集上,線性分類器效果最好。

測試3:圓形邊界

最后我們測試一個(gè)數(shù)據(jù)分類邊界為圓形的情況:圓形內(nèi)為一類,原型外為一類??催@類非線性的數(shù)據(jù)SVM表現(xiàn)如何:
測試數(shù)據(jù)生成代碼如下所示:

''''' 數(shù)據(jù)生成 ''' 
h = 0.1 
x_min, x_max = -1, 1 
y_min, y_max = -1, 1 
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), 
           np.arange(y_min, y_max, h)) 
n = xx.shape[0]*xx.shape[1] 
x = np.array([xx.T.reshape(n).T, xx.reshape(n)]).T 
y = (x[:,0]*x[:,0] + x[:,1]*x[:,1] < 0.8) 
y.reshape(xx.shape) 
 
x_train, x_test, y_train, y_test\ 
  = train_test_split(x, y, test_size = 0.2) 

測試結(jié)果如下:

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,  kernel='linear', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)
0.65
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,  kernel='poly', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)
0.675
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,  kernel='rbf', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)
0.9625
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,  kernel='sigmoid', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)
0.65

可以看到,對(duì)于這種邊界,徑向基函數(shù)的SVM得到了近似完美的分類結(jié)果。而其他的分類器顯然束手無策。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論