2017-10-28 58 views
0

我有一個數據幀,我想從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 

回答

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) 
指定水平