'svr'的参数调整
Parameter tuning of 'svr'
我如何使用支持向量 REGRESSION 的 tune 函数而不是分类,因为当我尝试使用 "svr" 作为函数的第一个参数时它不起作用,我找不到一个例子调整回归。所以这是我使用 e1071 包执行的代码:
tuneSVR <- tune(svm,
train.x = train_[, -which(names(train) %in% c("Identifier", "Sales"))],
train.y = train$Sales,
data = train,
ranges = list(epsilon = seq(0,1,0.1), cost = 2^(seq(0.5,8,.5))))
是不是因为我的问题是回归问题?这些代码行需要很长时间才能执行是否正常?以及如何计算 svr 的 R 平方?
在 e1071::svm()
中,问题类型自动从响应变量中推断出来,但可以使用 type
参数覆盖。 tune()
函数及其对 svm 的包装器的行为类似:
library( e1071 )
# Option 1: using the wrapper
tuneSVR1 <- tune.svm( mtcars[,c("drat","wt")], mtcars$mpg )
# Option 2: using the base function
tuneSVR2 <- tune( svm, mpg~drat+wt, data=mtcars )
# In both cases, the methods correctly infer that the task is regression
tuneSVR2$best.model
# Call:
# best.tune(method = svm, train.x = mpg ~ drat + wt, data = mtcars)
#
# Parameters:
# SVM-Type: eps-regression
# SVM-Kernel: radial
# cost: 1
# gamma: 0.5
# epsilon: 0.1
#
# Number of Support Vectors: 28
,可以直接计算为
ypred <- predict( tuneSVR2$best.model, mtcars[,c("drat","wt")] )
cor( ypred, mtcars$mpg )^2
# [1] 0.8325807
我如何使用支持向量 REGRESSION 的 tune 函数而不是分类,因为当我尝试使用 "svr" 作为函数的第一个参数时它不起作用,我找不到一个例子调整回归。所以这是我使用 e1071 包执行的代码:
tuneSVR <- tune(svm,
train.x = train_[, -which(names(train) %in% c("Identifier", "Sales"))],
train.y = train$Sales,
data = train,
ranges = list(epsilon = seq(0,1,0.1), cost = 2^(seq(0.5,8,.5))))
是不是因为我的问题是回归问题?这些代码行需要很长时间才能执行是否正常?以及如何计算 svr 的 R 平方?
在 e1071::svm()
中,问题类型自动从响应变量中推断出来,但可以使用 type
参数覆盖。 tune()
函数及其对 svm 的包装器的行为类似:
library( e1071 )
# Option 1: using the wrapper
tuneSVR1 <- tune.svm( mtcars[,c("drat","wt")], mtcars$mpg )
# Option 2: using the base function
tuneSVR2 <- tune( svm, mpg~drat+wt, data=mtcars )
# In both cases, the methods correctly infer that the task is regression
tuneSVR2$best.model
# Call:
# best.tune(method = svm, train.x = mpg ~ drat + wt, data = mtcars)
#
# Parameters:
# SVM-Type: eps-regression
# SVM-Kernel: radial
# cost: 1
# gamma: 0.5
# epsilon: 0.1
#
# Number of Support Vectors: 28
ypred <- predict( tuneSVR2$best.model, mtcars[,c("drat","wt")] )
cor( ypred, mtcars$mpg )^2
# [1] 0.8325807