如何在R中创建等高线图

时间:2015-01-09 16:46:14

标签: r contour

我使用dataset

的各种成本和伽玛值来调整SVM
> 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

我想创建一个类似于matlab生成的等高线图(下面是一个例子)

enter image description here

我尝试了plot命令,但我不能从它生成的图中推断出轮廓。

> plot(tunedsvm)

enter image description here

1 个答案:

答案 0 :(得分:9)

tune.svm返回类"tune"的对象:

> class(tunedsvm)
[1] "tune"

因此,相关帮助页面为?plot.tune。稍微阅读一下,就会发现一些有用的自定义参数:

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")

此代码生成以下图表:

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