sklearn svm如何选择核函数_【笔记】sklearn中的SVM以及使用多项式特征以及核函数...

sklearn中的SVM以及使用多项式特征以及核函数

sklearn中的SVM的使用

SVM的理论部分

需要注意的是,使用SVM算法,和KNN算法一样,都是需要做数据标准化的处理才可以,因为不同尺度的数据在其中的话,会严重影响SVM的最终结果

(在notebook中)

加载好需要的包,使用鸢尾花数据集,为了方便可视化,只取前两个特征,然后将其绘制出来

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],color='red')

plt.scatter(X[y==1,0],X[y==1,1],color='blue')

图像如下

8efce856941659fbd207c20ba66bf90f.png

首先进行数据的标准化的操作,实例化并fit操作,然后对x进行transform操作,传入x_standard,这样就完成了标准化的操作

from sklearn.preprocessing import StandardScaler

standardScaler = StandardScaler()

standardScaler.fit(X,y)

X_standard = standardScaler.transform(X)

在标准化以后就可以调用SVM算法了,对于线性的SVM,可以直接使用LinearSVC类,然后实例化操作,在进行fit,设置C为10的九次方

from sklearn.svm import LinearSVC

svc = LinearSVC(C=1e9)

svc.fit(X_standard,y)

使用先前的绘制函数并绘制图像

from matplotlib.colors import ListedColormap

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)

custom_cmap = ListedColormap(['#EF9A9A', '#FFF59D&

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值