获取错误“(下标)逻辑下标太长"在 R 中从 e1071 包训练 SVM 时 [英] Getting an error "(subscript) logical subscript too long" while training SVM from e1071 package in R

查看:86
本文介绍了获取错误“(下标)逻辑下标太长"在 R 中从 e1071 包训练 SVM 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我的训练数据训练 svm.(R 中的 e1071 包).以下是有关我的数据的信息.

I am training svm using my traindata. (e1071 package in R). Following is the information about my data.

> str(train)
'data.frame':   891 obs. of  10 variables:
$ survived: int  0 1 1 1 0 0 0 0 1 1 ...
$ pclass  : int  3 1 3 1 3 3 1 3 3 2 ...
$ name    : Factor w/ 15 levels "capt","col","countess",..: 12 13 9 13 12 12 12 8 13 13 
$ sex     : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
$ age     : num  22 38 26 35 35 ...
$ ticket  : Factor w/ 533 levels "110152","110413",..: 516 522 531 50 473 276 86 396 
$ fare    : num  7.25 71.28 7.92 53.1 8.05 ...
$ cabin   : Factor w/ 9 levels "a","b","c","d",..: 9 3 9 3 9 9 5 9 9 9 ...
$ embarked: Factor w/ 4 levels "","C","Q","S": 4 2 4 4 4 3 4 4 4 2 ...
$ family  : int  1 1 0 1 0 0 0 4 2 1 ...

我按照以下方式训练它.

I train it as the following.

library(e1071)
model1 <- svm(survived~.,data=train, type="C-classification")

这里没问题.但是当我预测为:

No problem here. But when I predict as:

pred <- predict(model1,test)

我收到以下错误:

Error in newdata[, object$scaled, drop = FALSE] : 
(subscript) logical subscript too long

我还尝试从火车和测试数据中删除票"预测器.但还是同样的错误.有什么问题?

I also tried removing "ticket" predictor from both train and test data. But still same error. What is the problem?

推荐答案

'test' 数据集中的一个因素的级别数可能有所不同.

There might a difference in the number of levels in one of the factors in 'test' dataset.

运行 str(test) 并检查因子变量是否与 'train' 数据集中的相应变量具有相同的水平.

run str(test) and check that the factor variables have the same levels as corresponding variables in the 'train' dataset.

即下面的示例显示 my.test$foo 只有 4 个级别.....

ie the example below shows my.test$foo only has 4 levels.....

str(my.train)
'data.frame':   554 obs. of  7 variables:
 ....
 $ foo: Factor w/ 5 levels "C","Q","S","X","Z": 2 2 4 3 4 4 4 4 4 4 ...

str(my.test)
'data.frame':   200 obs. of  7 variables:
 ...
 $ foo: Factor w/ 4 levels "C","Q","S","X": 3 3 3 3 1 3 3 3 3 3 ...

这篇关于获取错误“(下标)逻辑下标太长"在 R 中从 e1071 包训练 SVM 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆