AnswerBun.com

Tuning parameters of SVM in tune function

Cross Validated Asked by siegfried on September 5, 2020

I would like to fit a radial SVM onto the data. The tuning parameters cost and gamma are chosen by CV with the tune function. However the summary of the optimal model does not show the optimal gamma value.

The summary where gamma is not shown:

> svmult= svmcv$best.model
> svmult

Call:
best.tune(method = svm, train.x = type ~ ., data = wheat[tr, 
    ], ranges = list(cost = seq(0.001, 1, length.out = 50)), 
    kernel = "radial", gamma = c(0.1, 1, 2, 3, 4, 5))


Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  radial 
       cost:  0.2864286 

Number of Support Vectors:  171

Meanwhile when I try to plot the data and the SVM hyperplanes, I received this error:
Error in plot.svm(svmult, wheat) : missing formula.. Please help me with the CV and plotting. Thanks in advance!

The data:

> head(wheat)
  class  density  hardness    size  weight moisture    type
1   hrw 1.313662  73.32533 2.19946 26.3070  6.97526 Healthy
2   srw 1.233610  31.06556 1.84809 28.4400 12.51565 Healthy
3   srw 1.203221 -23.43620 3.15953 40.0530 12.36660  Sprout
4   hrw 1.279238  70.97478 3.09405 25.0035  7.53971 Healthy
5   srw 1.341604  60.25526 2.14827 30.0990 12.22773 Healthy
6   hrw 1.384803  53.07190 2.42810 26.5440  7.22400 Healthy
> dim(wheat)
[1] 275   7
rm(list=ls())
wheat= read.csv('Wheat.csv')
wheat= na.omit(wheat)
head(wheat)
wheat$class= factor(wheat$class)
attach(wheat)
library(e1071)
x= model.matrix(type~., data= wheat)[,-1]
y= wheat$type
tr= sample(1:nrow(wheat), floor(nrow(wheat)*2/3))
trainX= x[tr,]
testX= x[-tr,]
trainY= y[tr]
testY= y[-tr]
svmcv= tune(svm, type~., data = wheat[tr,], kernel= 'radial', ranges = list(cost=seq(0.001, 1, length.out = 50)), gamma= c(0.1,1,2,3,4,5))
summary(svmcv)
svmult= svmcv$best.model
plot(svmult, wheat)

One Answer

I believe gamma should be put inside the ranges list. The same goes for kernel if you want to tune for different kernels.

As for the plotting problem, you are missing a formula for the plot, and since it is a 2D-plot, you probably need to specify a slice for the other variables so that it is visualizable in 2D. Try something like this:

plot(svmModel, data=wheat, type~Variable2ChosenAsFree,
    slice=list(
        Variable3ChosenAsConstant=3,
        Variable4ChosenAsConstant=8
    )

Where you need to provide the correct variable names instead of my made up variable names.

Answered by dekuShrub on September 5, 2020

Add your own answers!

Related Questions

Linear model selection – Subset, Forward

0  Asked on February 16, 2021 by davud-mursalov

   

Churn prediction for customers with limited data

0  Asked on February 14, 2021 by ahmet-turul-bayrak

 

multiple linear regression vs polynomial regression models

0  Asked on February 14, 2021 by gracetam

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir