Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
800 views
in Technique[技术] by (71.8m points)

must a dataset contain all factors in SVM in R

I'm trying to find class probabilities of new input vectors with support vector machines in R. Training the model shows no errors.

fit <-svm(device~.,data=dataframetrain,
    kernel="polynomial",probability=TRUE)

But predicting some input vector shows some errors.

predict(fit,dataframetest,probability=prob)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
contrasts can be applied only to factors with 2 or more levels

dataframetrain looks like:

> str(dataframetrain)
'data.frame':   24577 obs. of  5 variables:
 $ device   : Factor w/ 3 levels "mob","pc","tab": 1 1 1 1 1 1 1 1 1 1 ...
 $ geslacht : Factor w/ 2 levels "M","V": 1 1 1 1 1 1 1 1 1 1 ...
 $ leeftijd : num  77 67 67 66 64 64 63 61 61 58 ...
 $ invultijd: num  12 12 12 12 12 12 12 12 12 12 ...
 $ type     : Factor w/ 8 levels "A","B","C","D",..: 5 5 5 5 5 5 5 5 5 5 ...

and dataframetest looks like:

> str(dataframetest)
'data.frame':   8 obs. of  4 variables:
 $ geslacht : Factor w/ 1 level "M": 1 1 1 1 1 1 1 1
 $ leeftijd : num  20 60 30 25 36 52 145 25
 $ invultijd: num  6 12 2 5 6 8 69 7
 $ type     : Factor w/ 8 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8

I trained the model with 2 factors for 'geslacht' but sometime I have to predict data with only 1 factor of 'geslacht'. Is it maybe possible that the class probabilites can be predicted with a test set with only 1 factor of 'geslacht'?

I hope someone can help me!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add another level (but not data) to geslacht.

x <- factor(c("A", "A"), levels = c("A", "B"))
x
[1] A A
Levels: A B

or

x <- factor(c("A", "A"))
levels(x) <- c("A", "B")
x
[1] A A
Levels: A B

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...