使用SVM模型进行预测时,R返回系数(0) [英] R returns factor(0) when predicting with a SVM model
问题描述
我的问题与此线程完全相同.但是,由于这似乎还没有令人满意的答案,因此我认为再次询问可复制的代码仍然是适当的.
My question is quite the same with this thread, however, since that seems not to have an satisfying answer yet, I think it is still appropriate to ask again along with reproducible codes.
training <- read.csv("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv")[,-1]
testing <- read.csv("https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv")[,-1]
# Importing data
library(e1071)
# Load the required package for SVM
svm_model <- svm(classe ~ pitch_arm + pitch_forearm + pitch_dumbbell + pitch_belt +
roll_arm + roll_forearm + roll_dumbbell + roll_belt +
yaw_arm + yaw_forearm + yaw_dumbbell + yaw_belt,
data = training, scale = FALSE, cross = 10)
# Perform SVM analysis with default gamma and cost, and do 10-fold cross validation
predict(svm_model, testing)
# R returns factor(0) here
我已经检查了测试数据帧是否具有所有需要的列,并且那些必需的列中不存在NA.请给我一些想法继续.谢谢!
I have checked that testing data frame has all columns needed, and no NA exists in those required columns. Please give me some ideas to carry on. Thanks!
推荐答案
这似乎是e1071预告.svm函数中一个古怪的结果.虽然您的测试数据没有模型中变量的缺失值.每个点都有缺失值.
This seems to be the result of a quirk in the e1071 predict.svm function. While your test data has no missing values for the variables in your model. Every point has missing values.
complete.cases(testing)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[14] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
您可以通过消除不需要的变量来解决此问题.
You can work around this problem by eliminating the unneeded variables.
ModelVars = which(names(training) %in%
c("pitch_arm", "pitch_forearm", "pitch_dumbbell", "pitch_belt",
"roll_arm", "roll_forearm", "roll_dumbbell", "roll_belt",
"yaw_arm", "yaw_forearm", "yaw_dumbbell", "yaw_belt"))
test2 = testing[, ModelVars]
predict(svm_model, test2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
A A B A A A D B A A A C A A A A A A A A
Levels: A B C D E
这篇关于使用SVM模型进行预测时,R返回系数(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!