「机器学习」支持向量机分类

前言

支持向量机是一类按监督学习方式对数据进行二元分类的广义线性分类器,其决策边界是对学习样本求解的最大边距超平面。SVM尝试寻找一个最优决策边界,使距离两个类别最近的样本最远。
SVM使用铰链损失函数计算经验风险并在求解系统中加入了正则化项以优化结构风险,是一个具有稀疏性和稳健性的分类器 。SVM可以通过核方法(kernel method)进行非线性分类,是常见的核学习(kernel learning)方法之一

SVM原理

「机器学习」支持向量机分类

非线性SVM与核函数

如何变幻空间

对于非线性的数据我们是通过核函数把数据分为不同的平面在进行处理。

「机器学习」支持向量机分类

多分类处理应用

优点

开源包

LibSVM:https://www.csie.ntu.edu.tw/~cjlin/libsvm/

Liblinear:https://www.csie.ntu.edu.tw/~cjlin/liblinear/

数据集

数据集是使用sklearn包中的数据集。也可以下载下来方便使用。

百度网盘:
链接:https://pan.baidu.com/s/16H2xRXQItIY0hU0_wIAvZw
提取码:vq2i

SVM实现鸢尾花分类

复制代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596ER-HLJS## 数据集 sklearn中


import numpy as np

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import colors

from sklearn import svm
from sklearn import model_selection


## 加载数据集

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


data = np.loadtxt('Iris-data/iris.data',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 = model_selection.train_test_split(x,y,random_state=1,test_size=0.2)


## 构建SVM分类器,训练函数

def classifier():
    clf = svm.SVC(C=0.8, kernel='linear', decision_function_shape='ovr')
    return clf

def train(clf, x_train, y_train):
    clf.fit(x_train, y_train.ravel())


clf = classifier()
train(clf,x_train,y_train)

## 初始化分类器,训练模型
def show_accuracy(a, b, tip):
    acc = a.ravel()==b.ravel()
    print('%s accracy:%.3f'%(tip, np.mean(acc)))

## 展示训练结果,及验证结果

def print_accracy(clf, x_train, y_train, x_test, y_test):
    print('training prediction:%.3f'%(clf.score(x_train, y_train)))
    print('test prediction:%.3f'%(clf.score(x_test, y_test)))

    show_accuracy(clf.predict(x_train),y_train, 'training data')
    show_accuracy(clf.predict(x_test), y_test, 'testing data')

    print('decision_function:
',clf.decision_function(x_train)[:2])

print_accracy(clf, x_train, y_train, x_test, y_test)



def draw(clf, x):
    iris_feature = 'sepal length', 'sepal width', 'petal length', 'petal width'

    x1_min,x1_max = x[:,0].min(), x[:,0].max()
    x2_min,x2_max = x[:,1].min(), x[:,1].max()

    x1, x2 = np.mgrid[x1_min:x1_max:200j, x2_min:x2_max:200j]

    grid_test = np.stack((x1.flat, x2.flat), axis=1)
    print('grid_test:
',grid_test[:2])

    z = clf.decision_function(grid_test)
    print('the distance:',z[:2])

    grid_hat = clf.predict(grid_test)
    print(grid_hat[:2])


    grid_hat = grid_hat.reshape(x1.shape)
    cm_light = mpl.colors.ListedColormap(['#A0FFA0', '#FFA0A0', '#A0A0FF'])
    cm_dark = mpl.colors.ListedColormap(['g', 'b', 'r'])

    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, facecolor='none', zorder=10)
    plt.xlabel(iris_feature[0])
    plt.ylabel(iris_feature[1])
    plt.xlim(x1_min, x1_max)
    plt.ylim(x2_min, x2_max)
    plt.title('Iris data classification via SVM')
    plt.grid()
    plt.show()

draw(clf, x)

折叠 

结果展示

可以看到分类效果和之前的k-means聚类效果图是差不多的。

有兴趣的可以看看k-means聚类进行分类:

使用k-means聚类对鸢尾花进行分类:https://www.cnblogs.com/hjk-airl/p/16410359.html

总结

可以看到SVM鸢尾花分类和K-means聚类是不同的,但是都可以达到分类的效果。

文章来自https://www.cnblogs.com/hjk-airl/p/16457435.html

展开阅读全文

页面更新:2024-05-06

标签:向量   子类   线性   样本   函数   平面   机器   类别   风险   两个   数据

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top