I am trying to tune my hyper parameter, gamma, in svm model with no success. I have tried some solutions proposed by similar posts with no luck as I am not familiar with this error origin.
This is the setup
#define and split data
d <- diamonds[sample(nrow(diamonds),"4000"), ]
#price_cat new explained variable
d$price_cat <- cut(d$price,10,labels = LETTERS[1:10])
spec = c(train = .5, test = .3, validate = .2)
g = sample(cut(
seq(nrow(d)),
nrow(d)*cumsum(c(0,spec)),
labels = names(spec)
))
res = split(d, g)
#clean the data
df2<-res$train[complete.cases(res$train),]
df3<-res$test[complete.cases(res$test),]
df4<-res$validate[complete.cases(res$validate),]
X_trn <- model.matrix(price_cat~.-1-price, data=df2)
X_val <- model.matrix(price_cat~.-1-price, data=df3)
X_tst <- model.matrix(price_cat~.-1-price, data=df4)
#Scale the data with train
means <- apply(X_trn, 2, mean)
sds <- apply(X_trn, 2, sd)
X_trn.s <- X_trn %>% sweep(MARGIN = 2, STATS = means, FUN = `-`) %>%
sweep(MARGIN = 2, STATS = sds, FUN = `/`)
X_val.s <- X_val %>% sweep(MARGIN = 2, STATS = means, FUN = `-`) %>%
sweep(MARGIN = 2, STATS = sds, FUN = `/`)
X_tst.s <- X_tst %>% sweep(MARGIN = 2, STATS = means, FUN = `-`) %>%
sweep(MARGIN = 2, STATS = sds, FUN = `/`)
Now when I run SVM it works perfectly fine like this
svm.1 <- svm(price_cat~.-1-price, data = df2, kernel="radial",type="C-classification")
But when I try to tune the gamma, it yields error.
gamma <- seq(0.001,0.1, length= 20)
train_e <- numeric(20)
valid_e <- numeric(20)
for(i in 1:20){
m <- svm(price_cat~., data = X_trn.s, kernel="radial",type="C-classification",gamma = gamma[i])
p_tr <- predict(m,X_trn.s)
p_va <- predict(m, X_val.s)
train_e[i] <- 1-mean(p_tr==X_trn.s$price_cat)
valid_e[i] <- 1-mean(p_va==X_val.s$price_cat)
}
When I use the function tune()
it also works fine,
tune(svm,price_cat~., data = df2, ranges = list(gamma = seq(0.001,0.1, length= 20), cost = 2^(2:4)))
But my job is t make it work with this loop.
question from:https://stackoverflow.com/questions/65651231/tuning-hyperparameters-error-in-model-frame-default-variable-lengths-differ