2016-12-26 169 views
1

我正在模拟数据,我有一些问题。我正在尝试修复参数。问题ROC曲线SVM模拟数据

library(e1071) 
library(ROCR) 
set.seed(10) 

#function to generate data 
generate.data <- function(n){ 
x2 <- runif(n) 
x1 <- runif(n) 
y <- as.factor(ifelse((x2>2*x1)|(x2>(2-2*x1)),-1,1)) 
return(data.frame(x1,x2,y)) 
} 

#Training and test: n = 500 
dtrain <- generate.data(500) 
dtest <- generate.data(200) 

我对训练集进行交叉验证,我不得不与径向内核参数cost=1000gamma=0.1

tune.out = tune(svm, y~x1+x2, data=dtrain, kernel="radial", 
       ranges=list(cost=c(0.1,1,10,100,1000), gamma=c(0.01,0.1,1,10,100))) 
svmbestmod = svm(y~x1+x2, data=dtrain, kernel="radial", cost=1000, gamma=0.1, 
       probability=TRUE) 

我想预测我的测试集,但我有0错误。我不明白。

yrad.test <- predict(svmbestmod, dtest) 

#confusion matrix 
mc.rad <- table(dtest$y, yrad.test) 
print(mc.rad) 

#Error 
err.rad <- 1-sum(diag(mc.rad))/sum(mc.rad) 
print(err.rad) 

如果有人能帮我理解我的错误或有什么问题,那就太好了。

回答

0

我已经把20000分的测试集

# First I isolate any misclassified points in the test set 
library(dplyr) 
errors <- cbind(dtest,yrad.test) %>% dplyr::filter(y != yrad.test) 

# Then I plot all the points in the train set, 
# coloured based on thier respective class, 
# while misclassified entries in the test set are shown in black 

library(ggplot2) 
p <- ggplot2::ggplot(data = dtrain, aes(x1,x2)) + 
geom_point(aes(colour = factor(y)))+ 
geom_point(data = errors,colour = "black")` 

In black misclassified points

在我看来,你的数据是完全分离的,基本上你的数据是好得是真实的,你的模型能够做出完美的预测,也许你可以给产生它的公式增加一些噪音。

此外,如果您的测试数据仅包含200个条目,则很可能它们中的任何一个都不足以接近错误分类的决策边界,正如我所提到的,必须生成20000个测试集才能获得大约200个错误分类点你在图片中看到。

+0

对不起,迟到了..谢谢你的回答,我修改了条目数。我必须学习如何使用gg2plot!哈哈有一个愉快的一天 – Mohamed