How to create a contour graph in R

I configured SVM with different values ​​for cost and gamma for dataset

> library(e1071) > library(foreign) > dataframe <- read.arff("/diabetes.arff") > index <- seq_len(nrow(dataframe)) > trainindex <- sample(index, trunc(length(index)/2)) > trainset <- dataframe[trainindex, ] > testset <- dataframe[-trainindex, ] > tunedsvm <- tune.svm(class ~ ., data = trainset, gamma = 2^(seq(-15,3,by=2)), cost = 2^(seq(-5,15,by=2))) > tunedsvm Parameter tuning of 'svm': - sampling method: 10-fold cross validation - best parameters: gamma cost 0.0001220703 2048 - best performance: 0.2187029 > head(tunedsvm$performances) gamma cost error dispersion 1 3.051758e-05 0.03125 0.351546 0.06245835 2 1.220703e-04 0.03125 0.351546 0.06245835 3 4.882812e-04 0.03125 0.351546 0.06245835 4 1.953125e-03 0.03125 0.351546 0.06245835 5 7.812500e-03 0.03125 0.351546 0.06245835 6 3.125000e-02 0.03125 0.351546 0.06245835 > nrow(tunedsvm$performances) [1] 110 

I want to create a contour graph similar to what matlab generates (below example)

enter image description here

I tried the plot command, but I cannot get much out of the graph that it creates.

 > plot(tunedsvm) 

enter image description here

+7
r contour
source share
1 answer

tune.svm returns an object of class "tune" :

 > class(tunedsvm) [1] "tune" 

Therefore the corresponding help page is ?plot.tune . A little reading of this shows a few useful arguments for customization:

 plot(tunedsvm, transform.x = log2, transform.y = log2, # log 2 scale for x and y transform.z = function(x) 1 - x, # convert error rate to accuracy rate swapxy = TRUE, # put gamma on y axis color.palette = terrain.colors, # define color palette for contours xlab = expression(log[2](cost)), ylab = expression(log[2](gamma)), main = "Accuracy Rate of SVM") 

This code creates the following graph:

contour plot showing accuracy rate of svm over log2 gamma vs log2 cost

+9
source share

All Articles