sklearn机器学习之SVM探索核函数以及寻优(癌症数据集)

1.导入相应包

from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
from time import time
import datetime
import pandas as pd

2.准备数据集并可视化

data = load_breast_cancer()
X = data.data
y = data.target
X.shape

可视化散点图

plt.scatter(X[:,0],X[:,1],c=y)
plt.show()

这里不是因为特征是二维矩阵,而是只选择了前两特征在平面中进行可视化。
在这里插入图片描述

3.选择不同模型进行训练

Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,y,test_size=0.3,random_state=420)
Kernel = ["linear", 'poly',"rbf","sigmoid"]
for kernel in Kernel:
    time0 = time()
    clf= SVC(kernel = kernel
             , gamma="auto"
             , degree = 1
             , cache_size=5000
           ).fit(Xtrain,Ytrain)
    print("The accuracy under kernel %s is %f" % (kernel,clf.score(Xtest,Ytest)))
    print(datetime.datetime.fromtimestamp(time()-time0).strftime("%M:%S:%f"))

最终输出如下:
The accuracy under kernel linear is 0.929825
00:00:600904
The accuracy under kernel poly is 0.923977
00:00:110731
The accuracy under kernel rbf is 0.596491
00:00:058461
The accuracy under kernel sigmoid is 0.596491
00:00:009973
由结果可以看出线性核和多项式核时精度较好,且时间消耗较高。

4.进一步观察数据集

癌症数据集是典型的线性数据集,且是未去量纲的数据,这里我们利用标准差去量纲,然后分别进行训练。

from sklearn.preprocessing import StandardScaler
X = StandardScaler().fit_transform(X)
data = pd.DataFrame(X)
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,y,test_size=0.3,random_state=420)
Kernel = ["linear","poly","rbf","sigmoid"]
for kernel in Kernel:
    time0 = time()
    clf= SVC(kernel = kernel
             , gamma="auto"
             , degree = 1
             , cache_size=5000
           ).fit(Xtrain,Ytrain)
    print("The accuracy under kernel %s is %f" % (kernel,clf.score(Xtest,Ytest)))
    print(datetime.datetime.fromtimestamp(time()-time0).strftime("%M:%S:%f"))

输出结果如下:
The accuracy under kernel linear is 0.976608
00:00:018951
The accuracy under kernel poly is 0.964912
00:00:005982
The accuracy under kernel rbf is 0.970760
00:00:014959
The accuracy under kernel sigmoid is 0.953216
00:00:007980
这里我们的时间和精确度都有很大的提升,说明两点:
1.线性核,尤其是多项式核函数在高次项时计算非常缓慢
2.rbf和多项式核函数都不擅长处理量纲不统一的数据集

5.关于gamma的选取

score = []
gamma_range = np.logspace(-10, 1, 50)
for i in gamma_range:
    clf = SVC(kernel='rbf', gamma=i, cache_size=5000).fit(Xtrain, Ytrain)
    score.append(clf.score(Xtest, Ytest))
print(max(score), gamma_range[score.index(max(score))])
plt.plot(gamma_range, score)
plt.show()

绘制图像如下:
在这里插入图片描述

6.通过网格搜索搜索多项式核最佳参数

from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import GridSearchCV

time0 = time()
gamma_range = np.logspace(-10, 1, 20)
coef0_range = np.linspace(0, 5, 10)
param_grid = dict(gamma=gamma_range, coef0=coef0_range)
#设置数据集分离器
cv = StratifiedShuffleSplit(n_splits=5, test_size=0.3, random_state=420)

grid = GridSearchCV(SVC(kernel='poly', degree=1, cache_size=5000), param_grid=param_grid, cv=cv)
grid.fit(X, y)
print("The best parameters are %s with a score of %0.5f" % (grid.best_params_, grid.best_score_))
print(datetime.datetime.fromtimestamp(time() - time0).strftime("%M:%S:%f"))

输出为:
The best parameters are {‘coef0’: 0.0, ‘gamma’: 0.18329807108324375} with a score of 0.96959
00:10:113562

7.重要参数C

C越大越容易过拟合,C越小越容易欠拟合,如何选取可以通过验证集。

#线性核
score = []
C_range = np.linspace(0.01, 30, 50)
for i in C_range:
    clf = SVC(kernel='linear', C=i, cache_size=500).fit(Xtrain, Ytrain)
    score.append(clf.score(Xtest, Ytest))
print(max(score), C_range[score.index(max(score))])
plt.plot(C_range, score)
plt.savefig(r"C:\Users\86377\Desktop\3.png")

plt.show()

绘制图像如下:
在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
sklearn中,SVM模型提供了多种核函数供选择。常用的核函数有以下几种: 1. 线性核函数(linear):线性核函数是最简单的一种核函数,它在原始特征空间中实现线性分类器,适用于特征空间是线性可分的情况。 2. 多项式核函数(poly):多项式核函数通过将样本映射到高维空间来实现非线性分类。它可以定义为在原始特征空间中进行多项式函数运算后的内积。 3. RBF核函数(rbf):径向基函数(RBF)核函数是一种常用的非线性核函数。它通过将样本映射到无穷维的特征空间来实现非线性分类。RBF核函数在实际应用中被广泛使用,因为它具有很好的表示能力。 4. Sigmoid核函数(sigmoid):Sigmoid核函数将样本映射到高维空间,并通过Sigmoid函数来实现非线性分类。它主要用于二分类问题。 在使用sklearn中的SVM模型时,可以通过设置参数`kernel`来指定所需的核函数。默认情况下,SVM模型的核函数为RBF核函数。例如,可以使用以下代码创建一个使用多项式核函数SVM模型: ```python from sklearn.svm import SVC # 创建SVM模型,使用多项式核函数 svm_model = SVC(kernel='poly') ``` 需要根据具体的数据集和问题选择合适的核函数。在实际应用中,可以通过尝试不同的核函数并比较它们在训练集和测试集上的性能来选择最佳的核函数。在选择核函数时,需要考虑数据集的线性可分性、特征空间的维度以及模型的复杂度等因素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值