SVM predict on dataframe with different factor levels

后端 未结 1 971
一个人的身影
一个人的身影 2021-01-26 18:20

I have a dataframe I want to make predictions on from an SVM, but the dataframe doesn\'t have all of the levels that the original training dataframe did. Is there an easy way ar

1条回答
  •  囚心锁ツ
    2021-01-26 18:54

    Just be sure to specify the levels in 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)
    

    0 讨论(0)
提交回复
热议问题