0

我正在尝试使用 mlr 包使用以下用户定义函数进行分类clasFunc。当我使用调用函数时

 clasFunc("classif.lda")

我收到一条错误消息说

model.frame.default 中的错误(术语,newdata,na.action = na.pass,xlev = object$xlevels):因子 col1 具有新级别“新级别”

我试图通过使用代码合并训练和测试数据的因素来解决这个问题

 for(j in 1: ncol(train)){

   if(class(train[,j])=="factor"){
         lvls=   union(levels(train[,j]), levels(test[,j]))
         levels(train[,j]) =lvls
         levels(test[,j]) =lvls

   }

 }

但这似乎不起作用。

makeTask(type = type, data = data, weights = weights, blocking = blocking, : col1,col2,col3,col4,col5 列的空因子级别被删除

这是我的完整代码。

clasFunc = function(clsnam){
try(

for( i in 1:5){

  print(paste0("fold ", i))
  train = read.csv(file =paste0("D:\\arff_csv_folds\\real_original\\train", i,".csv"))
  test = read.csv(file =paste0("D:\\arff_csv_folds\\real_original\\test", i,".csv"))

 for(j in 1: ncol(train)){

   if(class(train[,j])=="factor"){
         lvls=   union(levels(train[,j]), levels(test[,j]))
         levels(train[,j]) =lvls
         levels(train[,j]) =lvls

   }

 }

  trainTask <- makeClassifTask(data = train,target = "cls", positive = "yes")
  testTask <- makeClassifTask(data = test, target = "cls",  positive = "yes")

 Clslearn = makeLearner(clsnam, predict.type = "prob")

  trained <- train(Clslearn, trainTask)

  predicted <- predict(trained, testTask)

  print(paste0(clsnam, " fold ", i," test auc:",auc(predicted$data$truth, predicted$data$prob.yes)))

}
)
}

这是完整的输出

[1]“折叠1”

makeTask(type = type, data = data, weights = weights, blocking = blocking, : col1,col2,col3,col4,col5 列的空因子级别被删除

[1] “classif.lda 折叠 1 测试 auc:0.673604162894944”

[1]“折叠2”

makeTask(type = type, data = data, weights = weights, blocking = blocking, : col1,col2,col3,col4,col5 列的空因子级别被删除

[1] “classif.lda 折叠 2 测试 auc:0.686717528654292”

[1]“折叠3”

makeTask(type = type, data = data, weights = weights, blocking = blocking, : col1,col2,col3,col4,col5 列的空因子级别被删除

计时停止于:0 0 0

model.frame.default 中的错误(术语,newdata,na.action = na.pass,xlev = object$xlevels):因子 col1 具有新级别“新级别”

我怎样才能解决这个问题?

4

0 回答 0