SVM支持向量機算法原理詳解及人臉識別實戰

一、先利用庫簡單的來了解一下流程

1. 代碼

from sklearn import svm

x = [[2,0],
    [1,1],
    [2,5]]
y = [0,0,1]

# 選擇核
clf = svm.SVC(kernel='linear')
# 建立模型
clf.fit(x, y)

# 模型帶有很多的參數, 可以自己進行調整
print(clf)
# 得到支持向量點的索引,就是離線最近的兩個分類中的點
print(clf.support_)
#獲取每一個分類中支持向量的個數
print(clf.n_support_)


# 進行預測
print(clf.predict([[2,4]]))

2. 結果

# clf, 一個分類器的調節的參數
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
    kernel='linear', max_iter=-1, probability=False, random_state=None,
    shrinking=True, tol=0.001, verbose=False)

# 得到支持向量點的索引,就是離線最近的兩個分類中的點
[1 2]
#獲取每一個分類中支持向量的個數
[1 1]
#預測的結果
[1]

二、來個稍微複雜的,並畫出線

2.1代碼


import numpy as np
import pylab as pl
from sklearn import svm

# 1. 創建40個點
X = np.r_[np.random.randn(20,2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
Y = [0] * 20 + [1]*20

# 2. 建立訓練出模型
clf = svm.SVC(kernel='linear')
clf.fit(X, Y)

# 3. 獲取訓練出來的參數 - 超平面
w = clf.coef_[0]
a = -w[0]/w[1]
xx = np.linspace(-5, 5)
yy = a*xx - (clf.intercept_[0])/w[1]

# 4. 畫出平行線
b = clf.support_vectors_[0]
yy_down = a*xx + (b[1] - a*b[0])
b = clf.support_vectors_[-1]
yy_up = a*xx + (b[1] - a*b[0])

print("w : ", w)
print("a : ", a)

print("xx : ", xx)
print("yy : ", yy)

print("clf.support_vectors_ : ", clf.support_vectors_)
print("clf.coef_ : ", clf.coef_)

# switching to the generic n-dimensional parameterization of the hyperplan to the 2D-specific equation
# of a line y=a.x +b: the generic w_0x + w_1y +w_3=0 can be rewritten y = -(w_0/w_1) x + (w_3/w_1)

# plot the line, the points, and the nearest vectors to the plane
pl.plot(xx, yy, 'k-')
pl.plot(xx, yy_down, 'k--')
pl.plot(xx, yy_up, 'k--')

# 爲每一個點指定大小和顏色
pl.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
          s=80, facecolors='none')
pl.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired)

pl.axis('tight')
pl.show()

2.2 運行結果

w :  [0.62585066 0.76808981]
a :  -0.8148144330888917
xx :  [-5.         -4.79591837 -4.59183673 -4.3877551  -4.18367347 -3.97959184
 -3.7755102  -3.57142857 -3.36734694 -3.16326531 -2.95918367 -2.75510204
 -2.55102041 -2.34693878 -2.14285714 -1.93877551 -1.73469388 -1.53061224
 -1.32653061 -1.12244898 -0.91836735 -0.71428571 -0.51020408 -0.30612245
 -0.10204082  0.10204082  0.30612245  0.51020408  0.71428571  0.91836735
  1.12244898  1.32653061  1.53061224  1.73469388  1.93877551  2.14285714
  2.34693878  2.55102041  2.75510204  2.95918367  3.16326531  3.36734694
  3.57142857  3.7755102   3.97959184  4.18367347  4.3877551   4.59183673
  4.79591837  5.        ]
yy :  [ 3.67069705  3.50440839  3.33811973  3.17183107  3.00554241  2.83925375
  2.67296509  2.50667643  2.34038777  2.17409911  2.00781045  1.84152179
  1.67523313  1.50894448  1.34265582  1.17636716  1.0100785   0.84378984
  0.67750118  0.51121252  0.34492386  0.1786352   0.01234654 -0.15394212
 -0.32023078 -0.48651944 -0.6528081  -0.81909676 -0.98538542 -1.15167408
 -1.31796274 -1.4842514  -1.65054006 -1.81682872 -1.98311738 -2.14940604
 -2.3156947  -2.48198336 -2.64827202 -2.81456068 -2.98084934 -3.147138
 -3.31342666 -3.47971532 -3.64600398 -3.81229264 -3.9785813  -4.14486996
 -4.31115862 -4.47744728]
clf.support_vectors_ :  [[-1.5102335  -0.47474615]
 [-0.2351354   1.0901477 ]]
clf.coef_ :  [[0.62585066 0.76808981]]

三、* 解決難題 - 線性不可分和核函數問題

3.1原理講解

 

2.2.3 視覺化演示 https://www.youtube.com/watch?v=3liCbRZPrZA

 

 

 多類: 轉化爲多個二分類問題來循環處理

線性不可分: 轉化爲高維

3.2 代碼實戰人臉識別

from __future__ import print_function

from time import time
import logging
import matplotlib.pyplot as plt

from sklearn.cross_validation import train_test_split
from sklearn.datasets import fetch_lfw_people
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import RandomizedPCA
from sklearn.svm import SVC


print(__doc__)

# Display progress logs on stdout
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')


###############################################################################
# Download the data, if not already on disk and load it as numpy arrays

lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)

# introspect the images arrays to find the shapes (for plotting)
n_samples, h, w = lfw_people.images.shape

# for machine learning we use the 2 data directly (as relative pixel
# positions info is ignored by this model)
X = lfw_people.data
# 返回矩陣的列數 - 特徵數
n_features = X.shape[1]

# the label to predict is the id of the person
y = lfw_people.target
target_names = lfw_people.target_names
# 有多少個類
n_classes = target_names.shape[0]

print("Total dataset size:")
print("n_samples: %d" % n_samples)
print("n_features: %d" % n_features)
print("n_classes: %d" % n_classes)


###############################################################################
# Split into a training set and a test set using a stratified k fold

# split into a training and testing set
# 自帶的分數據的函數 - 兩個矩陣和兩個向量
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25)


###############################################################################
# Compute a PCA (eigenfaces) on the face dataset (treated as unlabeled
# dataset): unsupervised feature extraction / dimensionality reduction
n_components = 150

print("Extracting the top %d eigenfaces from %d faces"
      % (n_components, X_train.shape[0]))
t0 = time()
pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X_train)
print("done in %0.3fs" % (time() - t0))

# 這個變量保存了人臉的特徵值 - 可深究
eigenfaces = pca.components_.reshape((n_components, h, w))

print("Projecting the input data on the eigenfaces orthonormal basis")
t0 = time()
# 完成降維工作
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)
print("done in %0.3fs" % (time() - t0))


###############################################################################
# Train a SVM classification model

print("Fitting the classifier to the training set")
t0 = time()
# svm自帶的參數,5 x 6 = 30種組合,多次嘗試
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
              'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
clf = GridSearchCV(SVC(kernel='rbf', class_weight='auto'), param_grid)
clf = clf.fit(X_train_pca, y_train)
print("done in %0.3fs" % (time() - t0))
print("Best estimator found by grid search:")
print(clf.best_estimator_)


###############################################################################
# Quantitative evaluation of the model quality on the test set

print("Predicting people's names on the test set")
t0 = time()
y_pred = clf.predict(X_test_pca)
print("done in %0.3fs" % (time() - t0))

print(classification_report(y_test, y_pred, target_names=target_names))
#
print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))


###############################################################################
# Qualitative evaluation of the predictions using matplotlib

# 1. 傳入圖片 展覽
def plot_gallery(images, titles, h, w, n_row=3, n_col=4):
    """Helper function to plot a gallery of portraits"""
    plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))
    plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)
    for i in range(n_row * n_col):
        plt.subplot(n_row, n_col, i + 1)
        plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)
        plt.title(titles[i], size=12)
        plt.xticks(())
        plt.yticks(())


# plot the result of the prediction on a portion of the test set

def title(y_pred, y_test, target_names, i):
    pred_name = target_names[y_pred[i]].rsplit(' ', 1)[-1]
    true_name = target_names[y_test[i]].rsplit(' ', 1)[-1]
    return 'predicted: %s\ntrue:      %s' % (pred_name, true_name)

# 保存預測的人名
prediction_titles = [title(y_pred, y_test, target_names, i)
                     for i in range(y_pred.shape[0])]

plot_gallery(X_test, prediction_titles, h, w)

# plot the gallery of the most significative eigenfaces

eigenface_titles = ["eigenface %d" % i for i in range(eigenfaces.shape[0])]
plot_gallery(eigenfaces, eigenface_titles, h, w)

plt.show()




3.3結果展示

 

 

四、知識點補充

4.1  svm模型參數詳解

# 這是輸出一個模型的時候數據,分別是訓練完成的參數, 下面詳細解析一下
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
  kernel='linear', max_iter=-1, probability=False, random_state=None,
  shrinking=True, tol=0.001, verbose=False)

SVC() : SVM的類型,  
        0 -- C-SVC
        1 -- nu-SVC
        2 -- one-class SVM
        3 -- epsilon-SVR
        4 -- nu-SVR


C=1.0, 
cache_size=200, 
class_weight=None,

# coef0:核函數中的coef0設置(針對多項式/sigmoid核函數)((默認0)
coef0=0.0,   

decision_function_shape='ovr',
degree=3, gamma='auto_deprecated',
kernel='linear',
 max_iter=-1, 
probability=False, 
random_state=None,
shrinking=True, 
tol=0.001, 
verbose=False

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章