本文介绍了数据集必须包含 R 中 SVM 中的所有因素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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 看起来像:

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 ...

和 dataframetest 看起来像:

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

我使用 'geslacht' 的 2 个因子训练模型,但有时我必须仅使用 1 个因子的 'geslacht' 来预测数据.是否有可能使用只有 1 个 'geslacht' 因子的测试集来预测类别概率?

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'?

希望有人能帮帮我!!

推荐答案

geslacht 添加另一个级别(但不是数据).

Add another level (but not data) to geslacht.

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

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

这篇关于数据集必须包含 R 中 SVM 中的所有因素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 08:04