caret
给我以下错误。我正在训练一个用于预测的SVM模型,起始于词袋模型,并且希望使用caret来调整C参数,然而:
bow.model.svm.tune <- train(Training.match ~ ., data = data.frame( Training.match = factor(Training.Data.old$Training.match, labels = c('no match', 'match')), Text.features.dtm.df) %>% filter(Training.Data.old$Data.tipe == 'train'), method = 'svmRadial', tuneLength = 9, preProc = c("center","scale"), metric="ROC", trControl = trainControl( method="repeatedcv", repeats = 5, summaryFunction = twoClassSummary, classProbs = T))
错误:至少有一个类别级别不是有效的R变量名;这将在生成类概率时导致错误,因为变量名将被转换为no.match, match。请使用可以作为有效R变量名的因子级别(请参阅?make.names以获取帮助)。
原来的e1071::svm()
函数不会出现问题,因此我认为错误是在调整阶段产生的:
bow.model.svm.tune <- svm(Training.match ~ ., data = data.frame( Training.match = factor(Training.Data.old$Training.match, labels = c('no match', 'match')), Text.features.dtm.df) %>% filter(Training.Data.old$Data.tipe == 'train'))
数据只是一个结果因子变量和一组经过TfIdf转换的词向量列表:
'data.frame': 1796 obs. of 1697 variables: $ Training.match : Factor w/ 2 levels "no match","match": 2 1 1 1 1 1 1 1 2 1 ... $ azienda : num 0.12 0 0 0 0 ... $ bus : num 0.487 0 0 0 0 ... $ locale : num 0.275 0 0 0 0 ... $ martini : num 0.852 0.741 0.947 0.947 0.501 ... $ osp : num 0.339 0 0 0 0 ... $ ospedale : num 0.0389 0.0676 0.0864 0.0864 0.0915 ...
回答:
在预测时(内部使用train
或自己使用predict.train
),函数会为每个类别概率创建新列。如果你的代码期望有一个名为"no match"
的列,它将不会看到"no.match"
(这是data.frame
将其转换成的名称),并会抛出错误。