在R中使用SVM预测时的因数(0) [英] factor(0) when using predict for SVM in R
问题描述
我有一个数据框trainData
,其中包含198行,看起来像
I have a data frame trainData
which contains 198 rows and looks like
Matchup Win HomeID AwayID A_TWPCT A_WST6 A_SEED B_TWPCT B_WST6 B_SEED
1 2010_1115_1457 1 1115 1457 0.531 5 16 0.567 4 16
2 2010_1124_1358 1 1124 1358 0.774 5 3 0.75 5 14
...
testData
与之类似.
为了使用SVM,我必须将响应变量Win
更改为factor
.我尝试了以下方法:
In order to use SVM, I have to change the response variable Win
to a factor
. I tried the below:
trainDataSVM <- data.frame(Win=as.factor(trainData$Win), A_WST6=trainData$A_WST6, A_SEED=trainData$A_SEED, B_WST6=trainData$B_WST6, B_SEED= trainData$B_SEED,
Matchup=trainData$Matchup, HomeID=trainData$HomeID, AwayID=trainData$AwayID)
然后我想要一个SVM并预测概率,所以我尝试了以下
I then want to a SVM and predict the probabilities, so I tried the below
svmfit =svm (Win ~ A_WST6 + A_SEED + B_WST6 + B_SEED , data = trainDataSVM , kernel ="linear", cost =10,scale =FALSE )
#use CV with a range of cost values
set.seed (1)
tune.out = tune(svm, Win ~ A_WST6 + A_SEED + B_WST6 + B_SEED, data=trainDataSVM , kernel ="linear",ranges =list (cost=c(0.001 , 0.01 , 0.1, 1 ,5 ,10 ,100) ))
bestmod =tune.out$best.model
testDataSVM <- data.frame(Win=as.factor(testData$Win), A_WST6=testData$A_WST6, A_SEED=testData$A_SEED, B_WST6=testData$B_WST6, B_SEED= testData$B_SEED,
Matchup=testData$Matchup, HomeID=testData$HomeID, AwayID=testData$AwayID)
predictions_SVM <- predict(bestmod, testDataSVM, type = "response")
但是,当我尝试打印predictions_SVM
时,我收到消息
However, when I try to print out predictions_SVM
, I get the message
factor(0)
Levels: 0 1
代替一列概率值.发生了什么事?
instead of a column of probability values. What is going on?
推荐答案
我本人并没有使用太多,但是我知道SVM算法本身不会产生类概率,只会产生响应函数(与超平面的距离).如果查看svm函数的文档,则默认情况下,参数"probability"(概率)(即逻辑表明模型是否应允许概率预测")为FALSE,并且未将其设置为TRUE.相类似,predict.svm的文档说,参数概率"是一种逻辑,指示是否应该计算并返回类概率.只有在模型启用了概率选项的情况下才有可能."希望对您有所帮助.
I haven't used this much myself, but I know that the SVM algorithm itself does not produce class probabilities, only the response function (distance from hyperplane). If you look at the documentation for svm function, the argument "probability" - "logical indicating whether the model should allow for probability predictions" - is FALSE by default and you did not set it equal to TRUE. Documentation for predict.svm says similarly, argument "probability" is a "Logical indicating whether class probabilities should be computed and returned. Only possible if the model was fitted with the probability option enabled." Hope that's helpful.
这篇关于在R中使用SVM预测时的因数(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!