r - 数据集必须包含 R 中 SVM 中的所有因素

标签 r svm

我正在尝试使用 R 中的支持向量机查找新输入向量的类概率。 训练模型没有显示错误。

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

但是预测某些输入向量会出现一些错误。

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 看起来像:

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

数据帧测试看起来像:

> 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

我使用 2 个“geslacht”因子训练模型,但有时我必须仅使用 1 个“geslacht”因子来预测数据。 是否有可能通过仅包含 1 个“geslacht”因子的测试集来预测类别概率?

希望有人能帮助我!!

最佳答案

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 - 数据集必须包含 R 中 SVM 中的所有因素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55472705/

相关文章:

machine-learning - Liblinear 与 Pegasos

R Plotly改变堆积条形图的颜色

Rbuildignore 不忽略 .git

基于三列删除数据框中的行

r - (在 R 中)为什么使用用户定义的线性内核的 ksvm 的结果与使用 "vanilladot"的 ksvm 的结果不同?

r - 带有插入符号错误的 SVM 分类(基本)

RStudio - 在查看器 Pane 中渲染 html 内容

r - 使用 gganimate 时修复模糊/重叠的文本

machine-learning - RBF 和 SVM 多项式内核的参数选择 - 两个内核的最佳 'c'(成本参数)是否相同?

java - 为简单的支持 vector 机计算拉格朗日乘数