我有一个数据帧我想从SVM做出预测,但数据帧没有原始训练数据帧所做的所有级别。有一个简单的方法吗?
这是一个简单的例子
library(e1071)
df = data.frame(y = c(rep(1:3, each = 3)), x = rep(c("A", "B", "C"), each = 3))
m1 = svm(y ~ x, df)
df2 = data.frame(x = "B")
predict(m1, df2)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
答案 0 :(得分:1)
请务必在df2
library(e1071)
df = data.frame(y = c(rep(1:3, each = 3)), x = rep(c("A", "B", "C"), each = 3))
m1 = svm(y ~ x, df)
df2 = data.frame(x = factor("B",levels = c("A","B","C")))
predict(m1, df2)