02-32 线性支持向量机(鸢尾花分类)

线性支持向量机(鸢尾花分类)

在这里插入图片描述

导入模块

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib.font_manager import FontProperties
from sklearn import datasets
from sklearn.svm import SVC
%matplotlib inline
font = FontProperties(fname='/Library/Fonts/Heiti.ttc')

获取数据

iris_data = datasets.load_iris()
X = iris_data.data[:, [2, 3]]
y = iris_data.target
label_list = ['山鸢尾', '杂色鸢尾', '维吉尼亚鸢尾']

构建决策边界

def plot_decision_regions(X, y, classifier=None):
    marker_list = ['o', 'x', 's']
    color_list = ['r', 'b', 'g']
    cmap = ListedColormap(color_list[:len(np.unique(y))])

    x1_min, x1_max = X[:, 0].min()-1, X[:, 0].max()+1
    x2_min, x2_max = X[:, 1].min()-1, X[:, 1].max()+1
    t1 = np.linspace(x1_min, x1_max, 666)
    t2 = np.linspace(x2_min, x2_max, 666)

    x1, x2 = np.meshgrid(t1, t2)
    y_hat = classifier.predict(np.array([x1.ravel(), x2.ravel()]).T)
    y_hat = y_hat.reshape(x1.shape)
    plt.contourf(x1, x2, y_hat, alpha=0.2, cmap=cmap)
    plt.xlim(x1_min, x1_max)
    plt.ylim(x2_min, x2_max)

    for ind, clas in enumerate(np.unique(y)):
        plt.scatter(X[y == clas, 0], X[y == clas, 1], alpha=0.8, s=50,
                    c=color_list[ind], marker=marker_list[ind], label=label_list[clas])

线性可分支持向量机

在这里插入图片描述

训练模型

svm = SVC(kernel='linear', random_state=1)
svm.fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
  kernel='linear', max_iter=-1, probability=False, random_state=1,
  shrinking=True, tol=0.001, verbose=False)

可视化

plot_decision_regions(X, y, classifier=svm)
plt.xlabel('花瓣长度(cm)', fontproperties=font)
plt.ylabel('花瓣宽度(cm)', fontproperties=font)
plt.title('线性可分支持向量机(鸢尾花分类)', fontproperties=font, fontsize=20)
plt.legend(prop=font)
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rLdooqrx-1582896939412)(02-32%20%E7%BA%BF%E6%80%A7%E6%94%AF%E6%8C%81%E5%90%91%E9%87%8F9-%E6%9C%BA%28%E9%B8%A2%E5%B0%BE%E8%8A%B1%E5%88%86%E7%B1%BB%29_files/02-32%20%E7%BA%BF%E6%80%A7%E6%94%AF%E6%8C%81%E5%90%91%E9%87%8F9-%E6%9C%BA%28%E9%B8%A2%E5%B0%BE%E8%8A%B1%E5%88%86%E7%B1%BB%29_11_0.png)]

线性支持向量机

训练模型(C=0.01)

svm = SVC(kernel='linear', C=0.01, random_state=1)
svm.fit(X, y)
SVC(C=0.01, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
  kernel='linear', max_iter=-1, probability=False, random_state=1,
  shrinking=True, tol=0.001, verbose=False)

可视化

plot_decision_regions(X, y, classifier=svm)
plt.xlabel('花瓣长度(cm)', fontproperties=font)
plt.ylabel('花瓣宽度(cm)', fontproperties=font)
plt.title('线性支持向量机(鸢尾花分类)', fontproperties=font, fontsize=20)
plt.legend(prop=font)
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H9S2Rctz-1582896939413)(02-32%20%E7%BA%BF%E6%80%A7%E6%94%AF%E6%8C%81%E5%90%91%E9%87%8F9-%E6%9C%BA%28%E9%B8%A2%E5%B0%BE%E8%8A%B1%E5%88%86%E7%B1%BB%29_files/02-32%20%E7%BA%BF%E6%80%A7%E6%94%AF%E6%8C%81%E5%90%91%E9%87%8F9-%E6%9C%BA%28%E9%B8%A2%E5%B0%BE%E8%8A%B1%E5%88%86%E7%B1%BB%29_16_0.png)]

训练模型(C=100)

svm = SVC(kernel='linear', C=100, random_state=1)
svm.fit(X, y)
SVC(C=100, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
  kernel='linear', max_iter=-1, probability=False, random_state=1,
  shrinking=True, tol=0.001, verbose=False)

可视化

plot_decision_regions(X, y, classifier=svm)
plt.xlabel('花瓣长度(cm)', fontproperties=font)
plt.ylabel('花瓣宽度(cm)', fontproperties=font)
plt.title('线性支持向量机(鸢尾花分类)', fontproperties=font, fontsize=20)
plt.legend(prop=font)
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-REwSmiYJ-1582896939413)(02-32%20%E7%BA%BF%E6%80%A7%E6%94%AF%E6%8C%81%E5%90%91%E9%87%8F9-%E6%9C%BA%28%E9%B8%A2%E5%B0%BE%E8%8A%B1%E5%88%86%E7%B1%BB%29_files/02-32%20%E7%BA%BF%E6%80%A7%E6%94%AF%E6%8C%81%E5%90%91%E9%87%8F9-%E6%9C%BA%28%E9%B8%A2%E5%B0%BE%E8%8A%B1%E5%88%86%E7%B1%BB%29_20_0.png)]

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值