predict.svm中的错误:常量。无法扩展数据

时间:2017-04-23 07:50:07

标签: r

我从以下函数中获取数据集:

dataGenerator<-function(size){
        x <- runif( size,1,10)
        y <- runif( size,1,10)
        data.frame(X=x,Y=y)
}

然后我尝试使用此代码添加两列

smiller<-function(points){

  for(i in i:nrow(points)){
    if(6<points[i,1] & points[i,1]<7 & 6<points[i,2] & points[i,2]<7)
      points["scores"] <- as.vector((points[i,1]-7)^2+(points[i,2]-7)^2)
    else
      points["scores"] <- as.vector((points[i,1]-5)^2+(points[i,2]-5)^2-9)
  }
  points["class"]<-(as.vector(  points$scores<0 ))
  points
}

这是电话

train <-smiller(  dataGenerator(100))

并且最后我尝试使用svm分类器将其拉出来。

   model <- svm(class ~ . , train,type = "C",kernel = "linear")

但它抱怨:

Variable(s) �scores� constant. Cannot scale data.
 Show Traceback
Error in predict.svm(ret, xhold, decision.values = TRUE) : Model is empty!

1 个答案:

答案 0 :(得分:0)

在我看来,您的smiller功能存在一些问题。 您可以在下面找到一些代码版本,其中包含一些(希望是正确的)更改。

dataGenerator <- function(size) {
    x <- runif( size,1,10)
    y <- runif( size,1,10)
    data.frame(X=x,Y=y)
}

smiller <- function(points){
  for(i in 1:nrow(points)){
   if(6<points[i,1] & points[i,1]<7 & 6<points[i,2] & points[i,2]<7)
    points[i,"scores"] <- as.vector((points[i,1]-7)^2+(points[i,2]-7)^2)
   else
    points[i,"scores"] <- as.vector((points[i,1]-5)^2+(points[i,2]-5)^2-9)
  }
 points["class"]<-(as.vector(points$scores<0))
 points
}

train <- smiller(dataGenerator(100))

library(e1071)
model <- svm(class ~ . , train, type = "C", kernel = "linear")
print(model)

结果是:

Call:
svm(formula = class ~ ., data = train, type = "C", kernel = "linear")

Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  linear 
       cost:  1 
      gamma:  0.3333333 

Number of Support Vectors:  25