首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于循环错误,在R中“尝试选择小于一个元素”

对于循环错误,在R中“尝试选择小于一个元素”
EN

Stack Overflow用户
提问于 2016-02-27 03:59:24
回答 1查看 6.8K关注 0票数 1

我已经为我的向量创建了正确的indeces数,并且我尝试输入for循环中的I‘the元素作为索引,以保存分类错误值。但我知道错误是:

indeces.gen_error[i] <-粘贴(Classification_error)中的错误: 尝试选择少于一个元素

uspscl.txt

uspsdata.txt

我的代码:

代码语言:javascript
复制
library(e1071)
library(caret)

set.seed(733)
uspscldf = read.table('uspscl.txt', header=F, sep=',')
uspsdatadf = read.table('uspsdata.txt', header=F, sep='\t')

trainIndex <- createDataPartition(uspscldf$V1,list=FALSE, p = .80,times=1)
dataTrain <- uspsdatadf[ trainIndex,]
dataTest  <- uspsdatadf[-trainIndex,]

classTrain <- uspscldf[ trainIndex,]
classTest  <- uspscldf[-trainIndex,]

indeces = seq(0.00001, 1, by=0.001)

indeces.gen_error = NULL
indeces.softmargin = NULL
for (i in seq(0.00001, 1, by=0.001)){
    # For svm(): soft margin is "cost" 
    # Gaussian kernel bandwidth (sigma) = is implicitly defined by "gamma"
    # kernal=radial is non-linear while kernal=linear is linear
    svm.model <- svm(classTrain ~ ., data = dataTrain, cost = i,type="C-classification",kernal = "linear")

    svm.pred <- predict(svm.model, dataTrain)

    # confusion matrix
    tab <- table(pred = svm.pred, true = classTrain)

    classification_error <- 1- sum(svm.pred == classTrain)/length(svm.pred)

    indeces.gen_error[[i]] <- paste(classification_error)
    indeces.softmargin[[i]]<-i
}

我在第一次迭代中打印了第一个i,它给出了1e-5,这是正确的,所以我不知道为什么它说我选择的元素少于一个。任何帮助都将不胜感激。谢谢

答:我没有看到皮埃尔对这个问题的回答,直到我自己解决了答案,但他的解释更好,所以我接受了他的回答。我现在的新代码是:

代码语言:javascript
复制
indeces = seq(0.00001, 1, by=0.001)

indeces.gen_error = NULL
indeces.softmargin = NULL
count=0
for (i in indeces){
  count=count+1
    # For svm(): soft margin is "cost" 
    # Gaussian kernel bandwidth (sigma) = is implicitly defined by "gamma"
    # kernal=radial is non-linear while kernal=linear is linear
    svm.model <- svm(classTrain ~ ., data = dataTrain, cost = i,type="C-classification",kernal = "linear")

    svm.pred <- predict(svm.model, dataTrain)

    # confusion matrix
    tab <- table(pred = svm.pred, true = classTrain)

    classification_error <- 1- sum(svm.pred == classTrain)/length(svm.pred)

    indeces.gen_error[[count]] <- paste(classification_error)
    indeces.softmargin[[count]]<-i
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-27 04:45:08

代码语言:javascript
复制
#Example
x <- NULL
for( i in seq(0.01, 1, .01)) {
  a <- 10 * i
  x[[i]] <- paste("b", a)
}
# Error in x[[i]] <- paste("b", a) : 
#   attempt to select less than one element

#The right way
x <- NULL
myseq <- seq(0.01, 1, 0.01)
for( i in 1:length(myseq)) {
  a <- 10 * myseq[i]
  x[i] <- paste("b", a)
}

为什么第一种方法失败,for( i in seq(0.01, 1, .01))将使用序列作为'i‘。任何时候,当一个循环失败时,第一个解决问题的方法就是一个一个地尝试每个循环。因此,每个循环都取一个序列的值,并在有i的地方输入它。第一个循环如下所示:

代码语言:javascript
复制
for (i in seq(0.00001, 1, by=0.001)){

    svm.model <- svm(classTrain ~ ., data = dataTrain, cost = 0.00001,type="C-classification",kernal = "linear")

    svm.pred <- predict(svm.model, dataTrain)

    # confusion matrix
    tab <- table(pred = svm.pred, true = classTrain)

    classification_error <- 1- sum(svm.pred == classTrain)/length(svm.pred)

    indeces.gen_error[[0.00001]] <- paste(classification_error)
    indeces.softmargin[[0.00001]]<- 0.00001
}

你看到问题了吗?有了indeces.gen_error[[0.00001]],请注意这里发生的事情。你不是故意这么做的。您的意思是让indeces.gen_error[[1]]成为第一个条目。

您正在用小数点进行减法。如果我们有:

代码语言:javascript
复制
x <- 1:10

你认为x[2.5]会发生什么?我们要求在2.5位置的元素R。那也太没道理了。没有半个位置。要么是2号要么是3号。试着看看返回了什么。

在您的循环中,您向R请求indeces.gen_error[[0.00001]]。因此,您请求的是1/100,000的职位。那也太没道理了。评估器将强制子集为整数。它会传到0。我们得到了一个错误。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35665486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档