找到【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%就是我们最好的得分。

相关推荐
猫头虎几秒前
Claude Code 永动机:ralph-loop 无限循环迭代插件详解(安装 / 原理 / 最佳实践 / 避坑)
ide·人工智能·langchain·开源·编辑器·aigc·编程技术
aigcapi4 分钟前
如何让AI推广我的品牌?成长期企业GEO优化的“降本增效”实战指南
人工智能
百***243712 分钟前
GPT-5.2国内调用+API中转+成本管控
大数据·人工智能·深度学习
min18112345619 分钟前
金融风控中的实时行为建模
大数据·人工智能
笙枫22 分钟前
基于AI Agent框架下的能源优化调度方案和实践 |工具函数介绍(详细)
java·人工智能·能源
lanicc23 分钟前
TOON:为大语言模型优化的紧凑结构化数据格式
人工智能·语言模型·自然语言处理
:mnong23 分钟前
人工智能发展简史
人工智能
沛沛老爹28 分钟前
Skills高级设计模式(一):向导式工作流与模板生成
java·人工智能·设计模式·prompt·aigc·agent·web转型
学习研习社33 分钟前
人工智能能让医疗变得更有人性化吗?
人工智能
言之。34 分钟前
大模型 API 中的 Token Log Probabilities(logprobs)
人工智能·算法·机器学习