機器學習筆記14-----SVM實踐和分類器的性能的評價指標(了解python畫圖的技巧)


1.主要內容

 

2.SVM的應用

(1)利用SVM處理分類問題

 

分類器的性能的評價指標:

應用案例:

accuracy=3/6=0.5

precision=3/5=0.6

recall=3/4=0.75

3.代碼示例

(1)鳶尾花SVM案例

#!/usr/bin/python
# -*- coding:utf-8 -*-

import numpy as np
from sklearn import svm
from sklearn.model_selection import train_test_split
import matplotlib as mpl
import matplotlib.pyplot as plt

def iris_type(s):
    it = {b'Iris-setosa': 0, b'Iris-versicolor': 1, b'Iris-virginica': 2}
    return it[s]


# 'sepal length', 'sepal width', 'petal length', 'petal width'
iris_feature = u'花萼長度', u'花萼寬度', u'花瓣長度', u'花瓣寬度'


def show_accuracy(a, b, tip):
    acc = a.ravel() == b.ravel()
    print(tip + '正確率:', np.mean(acc))


if __name__ == "__main__":
    path = '8.iris.data'  # 數據文件路徑
    data = np.loadtxt(path, dtype=float, delimiter=',', converters={4: iris_type})
    x, y = np.split(data, (4,), axis=1)
    x = x[:, :2]
    x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1, train_size=0.6)

    # 分類器
    # clf = svm.SVC(C=0.1, kernel='linear', decision_function_shape='ovr')
    clf = svm.SVC(C=0.8, kernel='rbf', gamma=20, decision_function_shape='ovr')
    clf.fit(x_train, y_train.ravel())

    # 准確率
    print(clf.score(x_train, y_train))  # 精度
    y_hat = clf.predict(x_train)
    show_accuracy(y_hat, y_train, '訓練集')
    print(clf.score(x_test, y_test))
    y_hat = clf.predict(x_test)
    show_accuracy(y_hat, y_test, '測試集')

    # 畫圖
    x1_min, x1_max = x[:, 0].min(), x[:, 0].max()  # 第0列的范圍
    x2_min, x2_max = x[:, 1].min(), x[:, 1].max()  # 第1列的范圍
    x1, x2 = np.mgrid[x1_min:x1_max:500j, x2_min:x2_max:500j]  # 生成網格采樣點
    grid_test = np.stack((x1.flat, x2.flat), axis=1)  # 測試點

    Z = clf.decision_function(grid_test)    # 樣本到決策面的距離
    print(Z)
    grid_hat = clf.predict(grid_test)       # 預測分類值
    print(grid_hat)
    grid_hat = grid_hat.reshape(x1.shape)  # 使之與輸入的形狀相同
    mpl.rcParams['font.sans-serif'] = [u'SimHei']
    mpl.rcParams['axes.unicode_minus'] = False

    cm_light = mpl.colors.ListedColormap(['#A0FFA0', '#FFA0A0', '#A0A0FF'])
    cm_dark = mpl.colors.ListedColormap(['g', 'r', 'b'])
    x1_min, x1_max = x[:, 0].min(), x[:, 0].max()  # 第0列的范圍
    x2_min, x2_max = x[:, 1].min(), x[:, 1].max()  # 第1列的范圍
    x1, x2 = np.mgrid[x1_min:x1_max:500j, x2_min:x2_max:500j]  # 生成網格采樣點
    grid_test = np.stack((x1.flat, x2.flat), axis=1)  # 測試點
    plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light)

    plt.scatter(x[:, 0], x[:, 1], c=np.squeeze(y), edgecolors='k', s=50, cmap=cm_dark)      # 樣本
    plt.scatter(x_test[:, 0], x_test[:, 1], s=120, facecolors='none', zorder=10)     # 圈中測試集樣本
    plt.xlabel(iris_feature[0], fontsize=13)
    plt.ylabel(iris_feature[1], fontsize=13)
    plt.xlim(x1_min, x1_max)
    plt.ylim(x2_min, x2_max)
    plt.title(u'鳶尾花SVM二特征分類', fontsize=15)
    plt.grid()
    plt.show()

效果圖:

(2)

#!/usr/bin/python
# -*- coding:utf-8 -*-

import numpy as np
from sklearn import svm
import matplotlib as mpl
import matplotlib.colors
import matplotlib.pyplot as plt


def show_accuracy(a, b):
    acc = a.ravel() == b.ravel()
    print('正確率:%.2f%%' % (100 * float(acc.sum()) / a.size))


if __name__ == "__main__":
    data = np.loadtxt('14.bipartition.txt', dtype=np.float, delimiter='\t')
    x, y = np.split(data, (2, ), axis=1)
    y[y == 0] = -1
    y = y.ravel()

    # 分類器
    clfs = [svm.SVC(C=0.3, kernel='linear'),
           svm.SVC(C=10, kernel='linear'),
           svm.SVC(C=5, kernel='rbf', gamma=1),
           svm.SVC(C=5, kernel='rbf', gamma=4)]
    titles = 'Linear,C=0.3', 'Linear, C=10', 'RBF, gamma=1', 'RBF, gamma=4'

    x1_min, x1_max = x[:, 0].min(), x[:, 0].max()  # 第0列的范圍
    x2_min, x2_max = x[:, 1].min(), x[:, 1].max()  # 第1列的范圍
    x1, x2 = np.mgrid[x1_min:x1_max:500j, x2_min:x2_max:500j]  # 生成網格采樣點
    grid_test = np.stack((x1.flat, x2.flat), axis=1)  # 測試點

    cm_light = matplotlib.colors.ListedColormap(['#77E0A0', '#FF8080'])
    cm_dark = matplotlib.colors.ListedColormap(['g', 'r'])
    matplotlib.rcParams['font.sans-serif'] = [u'SimHei']
    matplotlib.rcParams['axes.unicode_minus'] = False
    plt.figure(figsize=(10,8), facecolor='w')
    for i, clf in enumerate(clfs):
        clf.fit(x, y)

        y_hat = clf.predict(x)
        show_accuracy(y_hat, y)  # 准確率

        # 畫圖
        print('支撐向量的數目:', clf.n_support_)
        print('支撐向量的系數:', clf.dual_coef_)
        print('支撐向量:', clf.support_)
        print
        plt.subplot(2, 2, i+1)
        grid_hat = clf.predict(grid_test)       # 預測分類值
        grid_hat = grid_hat.reshape(x1.shape)  # 使之與輸入的形狀相同
        plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light, alpha=0.8)
        plt.scatter(x[:, 0], x[:, 1], c=y, edgecolors='k', s=40, cmap=cm_dark)      # 樣本的顯示
        plt.scatter(x[clf.support_, 0], x[clf.support_, 1], edgecolors='k', facecolors='none', s=100, marker='o')   # 支撐向量
        z = clf.decision_function(grid_test)
        z = z.reshape(x1.shape)
        plt.contour(x1, x2, z, colors=list('krk'), linestyles=['--', '-', '--'], linewidths=[1, 2, 1], levels=[-1, 0, 1])
        plt.xlim(x1_min, x1_max)
        plt.ylim(x2_min, x2_max)
        plt.title(titles[i])
        plt.grid()
    plt.suptitle(u'SVM不同參數的分類', fontsize=18)
    plt.tight_layout(2)
    plt.subplots_adjust(top=0.92)
    plt.show()

效果圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM