I've tuned a SVM with various values for cost and gamma for a 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 plot similar to what matlab generates (below is an example)
I tried the plot
command but the contour I can't deduce much from the plot it generates.
> plot(tunedsvm)
A contour plot is a graphical technique for representing a 3-dimensional surface by plotting constant z slices, called contours, on a 2-dimensional format. That is, given a value for z, lines are drawn for connecting the (x,y) coordinates where that z value occurs.
The ContourFunctions R package provides functions that make it easier to make contour plots. The function cf is a quick function that can take in grid data, a function, or any data, and give a contour plot showing the function or data.
To plot 3D contour we will use countour3() to plot different types of 3D modules. Syntax: contour3(X,Y,Z): Specifies the x and y coordinates for the values in Z. contour3(Z): Creates a 3-D contour plot containing the isolines of matrix Z, where Z contains height values on the x-y plane.
Example Contour Plot For example, a biologist studies the effect of stream depth and canopy cover on fish biomass. A contour plot typically contains the following elements: X and Y-axes denoting values of two continuous independent variables. Colored bands representing ranges of the continuous dependent (Z) variable.
tune.svm
returns an object of class "tune"
:
> class(tunedsvm)
[1] "tune"
Therefore the relevant help page is ?plot.tune
. A little reading of this reveals several useful arguments for customisation:
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 produces the following plot:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With