python 支持向量机SVM实例解析

import numpy as npy
from sklearn import svm
import matplotlib.pyplot as plt

'''加载数据'''
x1 = []
y1 = []
for i in range(0, 10):
    if (i <= 3 or i >= 8):
        x1.append([i, i])
        y1.append(0)
    else:
        x1.append([i, i])
        y1.append(1)

x = npy.array(x1)
y = npy.array(y1)
'''创建SVM'''
# 线性核函数
linear = svm.SVC(kernel='linear').fit(x, y)
# 多项式核函数
poly = svm.SVC(kernel='poly', degree=4).fit(x, y)
# 径向基核函数
rbf = svm.SVC().fit(x, y)
#
sigmoid = svm.SVC(kernel='sigmoid').fit(x, y)
a = 1
x21, x22 = npy.meshgrid(npy.arange(x[:, 0].min(), x[:, 0].max(), 0.01), npy.arange(x[:, 1].min(), x[:, 0].max(), 0.01))
for i in [linear, poly, rbf, sigmoid]:
    rst = i.predict(npy.c_[x21.ravel(), x22.ravel()])
    # plt.subplot(横向划分,纵向划分, 定位)
    plt.subplot(2, 2, a)
    # contourf 等高线
    plt.contourf(x21, x22, rst.reshape(x21.shape))
    # yk 设置不同颜色
    # 训练数据的点也绘制出来
    for j in range(0, len(y1)):
        if (int(y1[j]) == 0):
            plt.plot(x[j:j + 1, 0], x[j:j + 1, 1], 'yo')
        else:
            plt.plot(x[j:j + 1, 0], x[j:j + 1, 1], 'ko')
    a += 1
plt.show()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值