SVM支持向量机sklearn代码V2

from sklearn import svm
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
import numpy as np

X, y = make_blobs(n_samples = 100, centers = 2
                 ,random_state = 0, cluster_std = 0.3
                 )
clf = svm.SVC(C = 1.0, kernel = 'linear')
clf.fit(X, y)


def plot_hyperplane(clf, X, y, 
                    h=0.02, 
                    draw_sv=True, 
                    title='hyperplan'):
    # create a mesh to plot in
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))

    plt.title(title)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.xticks(())
    plt.yticks(())

    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # SVM的分割超平面
    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    plt.contourf(xx, yy, Z, cmap='hot', alpha=0.5)

    markers = ['o', 's', '^']
    colors = ['b', 'r', 'c']
    labels = np.unique(y)
    for label in labels:
        plt.scatter(X[y==label][:, 0], 
                    X[y==label][:, 1], 
                    c=colors[label], 
                    marker=markers[label])
    # 画出支持向量
    if draw_sv:
        sv = clf.support_vectors_
        plt.scatter(sv[:, 0], sv[:, 1], c='y', marker='x')

plt.figure(figsize=(12, 4), dpi=244)
plot_hyperplane(clf, X, y, h=0.01, title='Maximum Margin Hyperplan')

X, y = make_blobs(n_samples = 100, centers = 3
                 ,random_state = 0, cluster_std = 0.8
                 )
clf_linear = svm.SVC(C = 1.0, kernel = 'linear')
clf_poly   = svm.SVC(C = 1.0, kernel = 'poly', degree = 3)
clf_rbf    = svm.SVC(C = 1.0, kernel = 'rbf' , gamma  = 0.5)
clf_rbf2   = svm.SVC(C = 1.0, kernel = 'rbf' , gamma  = 0.1)



plt.figure(figsize = (10, 10), dpi = 144)
clfs  = [clf_linear, clf_poly, clf_rbf, clf_rbf2]
titles = ['Linear Kernel'
        ,'Polynomial Kernel with Degree=3'
        ,'Gaussian Kernel with $\gamma=0.5$'
        ,'Gaussian Kernel with $\gamma=0.1$'
        ]
for clf, i in zip(clfs, range(len(clfs))):
    clf.fit(X,y)
    plt.subplot(2, 2, i + 1)
    plot_hyperplane(clf, X ,y, title = titles[i])


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值