我正在尝试使用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]&#34;折叠1&#34;
makeTask中的警告(type = type,data = data,weights = weights, blocking = blocking,:列的空因子级别被删除: COL1,COL2,COL3,COL4,COL5
[1]&#34; classif.lda fold 1 test auc:0.673604162894944&#34;
[1]&#34;弃2&#34;
makeTask中的警告(type = type,data = data,weights = weights, blocking = blocking,:列的空因子级别被删除: COL1,COL2,COL3,COL4,COL5
[1]&#34; classif.lda fold 2 test auc:0.686717528654292&#34;
[1]&#34;弃3&#34;
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具有新级别&#39;新级别&#39;
我该如何解决这个问题?