支持向量机

支持向量机Support vector machine,简称:SVM

在解决小样本、非线性及高维模式识别中该模型都表现出了许多独特的优势,在样本量较小的情况,其实际运用效果甚至超过了神经网络

并且其不仅可以应用于线性分布数据,还可以用于非线性分布数据,相比于其他基本机器学习分类算法如逻辑回归、KNN、朴素贝叶斯等,其最终效果的表现一般都会优于这些方法。

 

目录

1.总结草图

2.代码块

2.1 支持向量机线性硬间隔分类模型

2.2支持向量机线性软间隔分类模型

​2.3 支持向量机非线性分类模型


1.总结草图

2.代码块

语言:python

版本:v-3.6

编译器 : spyder

 

2.1 支持向量机线性硬间隔分类模型

# -*- coding: utf-8 -*-
"""
Created on Tue Aug 21 11:25:22 2018

svm-
支持向量机线性硬间隔分类模型

数据:随机生成示例数据
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import samples_generator# 示例数据生成
x,y=samples_generator.make_blobs(n_samples=60,centers=2,random_state=30,cluster_std=0.8)
print(x,y)#x  [[ 2.00275568 -3.43754548]  y [0 0 1 1 0 
from sklearn.svm import SVC
linear_svc=SVC(kernel='linear')
linear_svc.fit(x,y)
def svc_plot(model):
    ax=plt.gca()
    x=np.linspace(ax.get_xlim()[0],ax.get_xlim()[1],50)
    y=np.linspace(ax.get_ylim()[0],ax.get_ylim()[1],50)
    Y,X=np.meshgrid(y,x) # np.shape(Y),np.shape(X)  (50, 50) (50, 50)
    xy=np.vstack([X.ravel(),Y.ravel()]).T #np.shape(xy)  (2500, 2)  [(y1,x1),(y2,x2)....
    P=model.decision_function(xy).reshape(X.shape)#计算样本点到分割超平面的函数距离。 
    print(model.decision_function(xy).shape)
    ax.contour(X, Y, P, colors='green', levels=[-1, 0, 1], linestyles=['--', '-', '--'])
    ax.scatter(model.support_vectors_[:, 0], model.support_vectors_[:, 1], c='green', s=50)
plt.figure(figsize=(10, 8))
plt.scatter(x[:, 0], x[:, 1], c=y, s=40, cmap='bwr')    
svc_plot(linear_svc)

运行结果:

 

2.2支持向量机线性软间隔分类模型

# -*- coding: utf-8 -*-
"""
Created on Tue Aug 21 20:50:42 2018

支持向量机线性软间隔分类模型
数据L:混入零星不规则点
发现;惩罚系数c,取值到100000才有效果,确实比较大

"""


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import samples_generator
x,y=samples_generator.make_blobs(n_samples=60,centers=2,random_state=30,cluster_std=0.8)
x=np.concatenate((x,np.array([[3,-4],[4,-3.8],[2.5,-6.3],[3.3,-5.8]])))
y=np.concatenate((y,np.array([1,1,0,0])))
from sklearn.svm import SVC
linear_svc=SVC(kernel='linear',C=100000)
linear_svc.fit(x,y)
def svc_plot(model):
    ax=plt.gca()
    x=np.linspace(ax.get_xlim()[0],ax.get_xlim()[1],50)
    y=np.linspace(ax.get_ylim()[0],ax.get_ylim()[1],50)
    Y,X=np.meshgrid(y,x) # np.shape(Y),np.shape(X)  (50, 50) (50, 50)
    xy=np.vstack([X.ravel(),Y.ravel()]).T #np.shape(xy)  (2500, 2)  [(y1,x1),(y2,x2)....
    P=model.decision_function(xy).reshape(X.shape)#计算样本点到分割超平面的函数距离。 
    ax.contour(X, Y, P, colors='green', levels=[-1, 0, 1], linestyles=['--', '-', '--'])
    ax.scatter(model.support_vectors_[:, 0], model.support_vectors_[:, 1], c='green', s=100)
plt.figure(figsize=(8,6))
plt.scatter(x[:,0],x[:,1],c=y,s=30,cmap='bwr')
svc_plot(linear_svc)

运行结果:

2.3 支持向量机非线性分类模型

# -*- coding: utf-8 -*-
"""
Created on Tue Aug 21 21:19:28 2018

支持向量机非线性分类模型

核技巧:k(xi,xj)=x2i+x2j【2是平方】

核函数模型:RBF 高斯径向基核函数

"""
from sklearn.datasets import samples_generator
x2,y2=samples_generator.make_circles(150,factor=0.5,noise=0.1,random_state=30)
import matplotlib.pyplot as plt
#plt.scatter(x2[:,0],x2[:,1],c=y2,s=40)
#核技巧
def kernel_function(xi,yi):
    poly=xi**2+yi**2
    return poly
from mpl_toolkits import mplot3d
from ipywidgets import interact,fixed
r=kernel_function(x2[:,0],x2[:,1])
#print('r',r)
plt.figure(figsize=(8,6))
#ax=plt.subplot(projection='3d')
#ax.scatter3D(x2[:,0],x2[:,1],r,c=y2,s=40)
#ax.set_xlabel('x')
#ax.set_ylabel('y')
#ax.set_zlabel('z')
model=SVC(kernel='rbf',C=1)
model.fit(x2,y2)
def svc_plot(model):
    ax=plt.gca()
    x=np.linspace(ax.get_xlim()[0],ax.get_xlim()[1],50)
    y=np.linspace(ax.get_ylim()[0],ax.get_ylim()[1],50)
    Y,X=np.meshgrid(y,x) # np.shape(Y),np.shape(X)  (50, 50) (50, 50)
    xy=np.vstack([X.ravel(),Y.ravel()]).T #np.shape(xy)  (2500, 2)  [(y1,x1),(y2,x2)....
    P=model.decision_function(xy).reshape(X.shape)#计算样本点到分割超平面的函数距离。 
    ax.contour(X, Y, P, colors='green', levels=[-1, 0, 1], linestyles=['--', '-', '--'])
    ax.scatter(model.support_vectors_[:, 0], model.support_vectors_[:, 1], c='green', s=10)
    
plt.figure(figsize=(8,6))
plt.scatter(x2[:,0],x2[:,1],c=y2,s=40)
svc_plot(model)

 运行效果图:

 

 

2.4 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值