码迷,mamicode.com
首页 > 其他好文 > 详细

支持向量机(SVM)

时间:2020-02-06 14:13:49      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:rac   red   mod   idt   learn   step   dom   alpha   nbsp   

#线性支持向量机

#代码:

from sklearn import datasets
x,y = datasets.make_classification(n_samples=100,n_features=2,n_redundant=0,n_classes=2,random_state=7816)
x.shape,y.shape
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(x[:,0],x[:,1],c=y,s=100)
plt.xlabel(‘x values‘)
plt.ylabel(‘y values‘)
import numpy as np
x = x.astype(np.float32)
y = y*2-1
from sklearn import model_selection as ms
x_train,x_test,y_train,y_test = ms.train_test_split(x,y,test_size=0.2,random_state=42)
print(x)
print(y)

x = x.astype(np.float32)
y = y*2-1
from sklearn import model_selection as ms
x_train,x_test,y_train,y_test = ms.train_test_split(x,y,test_size=0.2,random_state=42)
import cv2
svm = cv2.ml.SVM_create()
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.train(x_train,cv2.ml.ROW_SAMPLE,y_train)
_,y_pred = svm.predict(x_test)
from sklearn import metrics
metrics.accuracy_score(y_test,y_pred)
def plot_decision_boundary(svm, X_test, y_test):
    # create a mesh to plot in
    h = 0.02  # step size in mesh
    x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1
    y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
   
    X_hypo = np.c_[xx.ravel().astype(np.float32),
                   yy.ravel().astype(np.float32)]
    _, zz = svm.predict(X_hypo)
    zz = zz.reshape(xx.shape)
   
    plt.contourf(xx, yy, zz, cmap=plt.cm.coolwarm, alpha=0.8)
    plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, s=200)
plt.figure(figsize=(10, 6))
plot_decision_boundary(svm, x_test, y_test)
可视化决策边界结果:
技术图片

 #非线性支持向量机:

#代码:

kernels = [cv2.ml.SVM_LINEAR,cv2.ml.SVM_INTER,cv2.ml.SVM_SIGMOID,cv2.ml.SVM_RBF]
for idx,kernel in enumerate(kernels):
    svm = cv2.ml.SVM_create()
    svm.setKernel(kernel)
    svm.train(x_train,cv2.ml.ROW_SAMPLE,y_train)
    _,y_pred = svm.predict(x_test)
    accuracy = metrics.accuracy_score(y_test,y_pred)
   
    plt.subplot(2,2,idx+1)
    plot_decision_boundary(svm,x_test,y_test)
    plt.title(‘accuracy = %.2f‘ %accuracy)

技术图片

 

 

 

支持向量机(SVM)

标签:rac   red   mod   idt   learn   step   dom   alpha   nbsp   

原文地址:https://www.cnblogs.com/shiheyuanfang/p/12268312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!