使用 `tune.svm()`
本练习将带您动手使用 tune.svm() 函数。您将用它来为基于本章前面创建的径向可分数据集的 SVM 模型,寻找 cost、gamma 和 coef0 参数的最优取值。训练数据在数据框 trainset 中,测试数据在 testset 中,且已为您预加载 e1071 库。请记住,类别变量 y 位于 trainset 和 testset 的第 3 列。
另外回顾一下视频中,Kailash 使用了 cost=10^(1:3),以便将 cost 参数的搜索范围设为从 10=10^1 到 1000=10^3,步长为 10。
本练习是课程的一部分
R 中的支持向量机
练习说明
- 按如下设置参数搜索范围:
cost—— 从 0.1(10^(-1))到 100(10^2),以 10 为倍数递增。gamma和coef0—— 取值为以下之一:0.1、1、10。
交互式实操练习
通过完成这段示例代码来试试这个练习。
#tune model
tune_out <-
tune.svm(x = trainset[, -3], y = trainset[, 3],
type = "C-classification",
kernel = "polynomial", degree = 2, cost = 10^(___:___),
gamma = c(___, ___, ___), coef0 = c(0.1, 1, 10))
#list optimal values
tune_out$best.parameters$___
tune_out$best.parameters$___
tune_out$best.parameters$___