找到【SVM】中最优的惩罚项系数C

因为本来SVM是想找到间隔最大的分割面,所以C越大,SVC会选择边际更小的,能够更好的分类所有训练点的决策边界,不过模型的训练时间也会越长。如果C的设定值较小,那SVC会尽量最大化边界,决策功能会更简单,但代价是训练的准确度。

我们先来调线性核函数:

python 复制代码
#调线性核函数
score = []
C_range = np.linspace(0.01,30,50)
for i in C_range:
    clf = SVC(kernel="linear",C=i,cache_size=5000).fit(Xtrain,Ytrain)
    score.append(clf.score(Xtest,Ytest))
print(max(score), C_range[score.index(max(score))])
plt.plot(C_range,score)
plt.show()

输出结果为:0.9766081871345029 1.2340816326530613

可以看到准确率最高是97%以上。接下来我们来看看在rbf上的结果:

python 复制代码
score = []
C_range = np.linspace(0.01,30,50)
for i in C_range:
    clf = SVC(kernel="rbf",C=i,gamma = 0.012742749857031322,cache_size=5000).fit(Xtrain,Ytrain)
    score.append(clf.score(Xtest,Ytest))
    
print(max(score), C_range[score.index(max(score))])
plt.plot(C_range,score)
plt.show()

输出结果为:0.9824561403508771 6.130408163265306

既然最高的得分所对应的C值是6,那么我们可以在5-7之间进一步细化,看能否找到一个更好的局部最优:

python 复制代码
#进一步细化
score = []
C_range = np.linspace(5,7,50)
for i in C_range:
    clf = SVC(kernel="rbf",C=i,gamma = 
0.012742749857031322,cache_size=5000).fit(Xtrain,Ytrain)
    score.append(clf.score(Xtest,Ytest))
    
print(max(score), C_range[score.index(max(score))])
plt.plot(C_range,score)
plt.show()

输出结果为:0.9824561403508771 5.938775510204081

可以看到,98.2456%就是我们最好的得分。

相关推荐
IT_陈寒4 分钟前
Redis性能提升3倍的5个冷门技巧,90%开发者都不知道!
前端·人工智能·后端
Rsun045518 分钟前
SpringAI相关内容
人工智能
yc_Blog14 分钟前
卷积神经网络是什么:从图像识别问题说起
人工智能·神经网络·cnn
love530love24 分钟前
ComfyUI rgthree-comfy Image Comparer 节点无输出问题排查与解决
人工智能·windows·python·comfyui·rgthree-comfy·nodes 2.0·vue 节点
新缸中之脑36 分钟前
应该使用AI构建内部工具吗?
人工智能
badhope40 分钟前
Docker从零开始安装配置全攻略
运维·人工智能·vscode·python·docker·容器·github
AI攻城狮1 小时前
lossless-claw vs mem0:别再把上下文管理和长期记忆混为一谈
人工智能·云原生·aigc
qq_349523261 小时前
OpenClaw 架构全解析:本地优先的开源 AI Agent 框架
人工智能·架构·开源
寻见9031 小时前
智能体开发_07Function Calling道法术器拆解,一文搞懂大模型如何“做事”
人工智能·后端·ai编程
未来之窗软件服务1 小时前
vosk-ASR asterisk调用[AI人工智能(五十三)]—东方仙盟
人工智能·语音识别·vosk·仙盟创梦ide·东方仙盟