Python机器学习:SVM004Scikit-learn中的SVM

纵向值很大

在这里插入图片描述
数据尺度归一化
在这里插入图片描述

#SCikit-learn中的SVN
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
#鸢尾花数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target

X = X[y < 2,:2]
y = y[y < 2]
plt.scatter(X[y == 0,0],X[y == 0,1])
plt.scatter(X[y == 1,0],X[y == 1,1])

在这里插入图片描述

from sklearn.preprocessing import StandardScaler
standardScaler = StandardScaler()
#只看下效果,不预测
standardScaler.fit(X,y)
X_standard = standardScaler.transform(X)
#线性SVM
#SVC
from sklearn.svm import LinearSVC
#参数 multi_class = 'ovr',penalty = 'l2'
svc = LinearSVC(C = 1e9)
svc.fit(X_standard,y)
#绘制决策边界
def plot_decision_boundary(model, axis):

    x0, x1 = np.meshgrid(
        np.linspace(axis[0], axis[1], int((axis[1]-axis[0])*100)).reshape(-1, 1),
        np.linspace(axis[2], axis[3], int((axis[3]-axis[2])*100)).reshape(-1, 1),
    )
    X_new = np.c_[x0.ravel(), x1.ravel()]

    y_predict = model.predict(X_new)
    zz = y_predict.reshape(x0.shape)

    from matplotlib.colors import ListedColormap
    custom_cmap = ListedColormap(['#EF9A9A','#FFF59D','#90CAF9'])

    plt.contourf(x0, x1, zz, linewidth=5, cmap=custom_cmap)
#C设置成10亿,hard margin
plot_decision_boundary(svc,[-3,3,-3,3])
plt.scatter(X_standard[y == 0,0],X_standard[y == 0,1])
plt.scatter(X_standard[y == 1,0],X_standard[y == 1,1])

在这里插入图片描述
soft-margin

#soft
svc2 = LinearSVC(C = 0.01)
svc2.fit(X_standard,y)
plot_decision_boundary(svc2,[-3,3,-3,3])
plt.scatter(X_standard[y == 0,0],X_standard[y == 0,1])
plt.scatter(X_standard[y == 1,0],X_standard[y == 1,1])

在这里插入图片描述

def plot_svc_decision_boundary(model, axis):

    x0, x1 = np.meshgrid(
        np.linspace(axis[0], axis[1], int((axis[1]-axis[0])*100)).reshape(-1, 1),
        np.linspace(axis[2], axis[3], int((axis[3]-axis[2])*100)).reshape(-1, 1),
    )
    X_new = np.c_[x0.ravel(), x1.ravel()]

    y_predict = model.predict(X_new)
    zz = y_predict.reshape(x0.shape)

    from matplotlib.colors import ListedColormap
    custom_cmap = ListedColormap(['#EF9A9A','#FFF59D','#90CAF9'])

    plt.contourf(x0, x1, zz, linewidth=5, cmap=custom_cmap)
    w = model.coef_[0]
    b = model.intercept_[0]

    # w0 * x0 + w1 * x1 + b = 0
    # => x1 = -w0 / w1 * x0 - b / w1
    plot_x = np.linspace(axis[0],axis[1],200)

    up_y = -w[0] / w[1] * plot_x - b / w[1] + 1 / w[1]
    down_y = -w[0] / w[1] * plot_x - b / w[1] - 1 / w[1]

    up_index = (up_y >= axis[2]) & (up_y <= axis[3])
    down_index = (down_y >= axis[2]) & (down_y <= axis[3])

    plt.plot(plot_x[up_index],up_y[up_index],color = 'black')
    plt.plot(plot_x[down_index],down_y[down_index],color = 'black')
plot_svc_decision_boundary(svc,[-3,3,-3,3])
plt.scatter(X_standard[y == 0,0],X_standard[y == 0,1])
plt.scatter(X_standard[y == 1,0],X_standard[y == 1,1])

在这里插入图片描述

plot_svc_decision_boundary(svc2,[-3,3,-3,3])
plt.scatter(X_standard[y == 0,0],X_standard[y == 0,1])
plt.scatter(X_standard[y == 1,0],X_standard[y == 1,1])

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值