开始使用免费开始使用

使用 `tune.svm()`

本练习将带您动手使用 tune.svm() 函数。您将用它来为基于本章前面创建的径向可分数据集的 SVM 模型,寻找 costgammacoef0 参数的最优取值。训练数据在数据框 trainset 中,测试数据在 testset 中,且已为您预加载 e1071 库。请记住,类别变量 y 位于 trainsettestset 的第 3 列。

另外回顾一下视频中,Kailash 使用了 cost=10^(1:3),以便将 cost 参数的搜索范围设为从 10=10^11000=10^3,步长为 10。

本练习是课程的一部分

R 中的支持向量机

查看课程

练习说明

  • 按如下设置参数搜索范围:
    • cost —— 从 0.1(10^(-1))到 100(10^2),以 10 为倍数递增。
    • gammacoef0 —— 取值为以下之一: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$___
编辑并运行代码