R中的SVM错误

我是R的新手,尝试从文本中检索数据,然后将其应用到SVM中进行分类。这是代码:

train<-read.table("training.txt")
train[which(train=="?",arr.ind=TRUE)]<-NA
train=unique(train)
y=train[,length(train)]

classifier<-svm(y~.,data=train[,-length(train)],scale=F)
classifier<-svm(x=train[,-length(train)],y=factor(y),scale=F)

我尝试了两种不同的方法来调用svm,因为第一种方法(svm(y~.,data=train[,-length(train)],scale=F))似乎还可以,但是第二种方法有问题,它报告:

Error in svm.default(x = train[, length(train)], y = factor(y), scale = F) : 
  NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In svm.default(x = train[, length(train)], y = factor(y), scale = F) :
  NAs introduced by coercion

这是的示例training.txt,最后一列是目标

39,State-gov,77516,Bachelors,13,Never-married,Adm-clerical,Not-in-family,White,Male,2174,0,40,United-States,0
50,Self-emp-not-inc,83311,Bachelors,13,Married-civ-spouse,Exec-managerial,Husband,White,Male,0,0,13,United-States,0
38,Private,215646,HS-grad,9,Divorced,Handlers-cleaners,Not-in-family,White,Male,0,0,40,United-States,0
53,Private,234721,11th,7,Married-civ-spouse,Handlers-cleaners,Husband,Black,Male,0,0,40,United-States,0
28,Private,338409,Bachelors,13,Married-civ-spouse,Prof-specialty,Wife,Black,Female,0,0,40,Cuba,0
37,Private,284582,Masters,14,Married-civ-spouse,Exec-managerial,Wife,White,Female,0,0,40,United-States,0
49,Private,160187,9th,5,Married-spouse-absent,Other-service,Not-in-family,Black,Female,0,0,16,Jamaica,0
52,Self-emp-not-inc,209642,HS-grad,9,Married-civ-spouse,Exec-managerial,Husband,White,Male,0,0,45,United-States,1
31,Private,45781,Masters,14,Never-married,Prof-specialty,Not-in-family,White,Female,14084,0,50,United-States,1
42,Private,159449,Bachelors,13,Married-civ-spouse,Exec-managerial,Husband,White,Male,5178,0,40,United-States,1
37,Private,280464,Some-college,10,Married-civ-spouse,Exec-managerial,Husband,Black,Male,0,0,80,United-States,1
30,State-gov,141297,Bachelors,13,Married-civ-spouse,Prof-specialty,Husband,Asian-Pac-Islander,Male,0,0,40,India,1
23,Private,122272,Bachelors,13,Never-married,Adm-clerical,Own-child,White,Female,0,0,30,United-States,0
32,Private,205019,Assoc-acdm,12,Never-married,Sales,Not-in-family,Black,Male,0,0,50,United-States,0
40,Private,121772,Assoc-voc,11,Married-civ-spouse,Craft-repair,Husband,Asian-Pac-Islander,Male,0,0,40,NA,1

有什么想法吗?提前致谢!

LyzandeR

从文档:

对于x参数:

a data matrix, a vector, or a sparse matrix (object of class Matrix
provided by the Matrix package,or of class matrix.csr provided by the
SparseM package, or of class simple_triplet_matrix provided by the slam package).

对于y参数:

a response vector with one label for each row/component of x. Can be
either a factor (for classification tasks) or a numeric vector (for regression).

x=train[,-length(train)]在第二个函数中键入:时,实际上使用的data.frame是不支持的,它会崩溃。

svm函数适用于数字矩阵

library(e1071)
train[which(train=="?",arr.ind=TRUE)]<-NA
train=unique(train)
y=factor(train[,length(train)])
train <- data.frame(lapply(train,as.numeric)) #convert to numeric. factors are integer fields anyway behind the scenes.

train <- as.matrix(train[-length(train)])

classifier<-svm(x= train ,y=y,scale=F)

输出:

> summary(classifier)

Call:
svm.default(x = train, y = y, scale = F)


Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  radial 
       cost:  1 
      gamma:  0.07142857 

Number of Support Vectors:  14

 ( 9 5 )


Number of Classes:  2 

Levels: 
 0 1

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

R中predict.svm中的错误

来自分类Dev

R中的predict.svm错误

来自分类Dev

在R中调整SVM-因变量的类型错误

来自分类Dev

SVM模型中的错误:预处理方法限于:R中的BoxCox,YeoJohnson

来自分类Dev

R中的SVM分类图

来自分类Dev

Matlab Stats Svm测试中的错误

来自分类Dev

在R(线性SVM内核)中调整svm参数

来自分类Dev

SVM用于R中的文本分类

来自分类Dev

SVM用于R中的文本分类

来自分类Dev

在R中查询SVM分类器

来自分类Dev

为什么 R.predict.svm 返回错误大小的列表?

来自分类Dev

二进制SVM中的分类错误

来自分类Dev

在opencv中向SVM提供输入时出现错误

来自分类Dev

如何修复 SVM 中的“非一致数组”错误?

来自分类Dev

SVM训练数据错误

来自分类Dev

使用R中的SVM进行一类分类

来自分类Dev

在R中由插入符号包训练的SVM线性模型

来自分类Dev

如何在R中构建多类SVM?

来自分类Dev

R:在数据框中为分组应用SVM功能

来自分类Dev

在R中为svm创建数据集(e1071)

来自分类Dev

在R中绘制SVM线性分隔符

来自分类Dev

R SVM预测

来自分类Dev

sklearn CalibratedClassifierCV和SVM错误

来自分类Dev

sklearn CalibratedClassifierCV和SVM错误

来自分类Dev

SVM正在R中的训练集上工作,但不在R中的测试集上工作

来自分类Dev

R中的子集错误

来自分类Dev

R中的setwd错误

来自分类Dev

R中的TermDocumentMatrix错误

来自分类Dev

R中的Heatmap错误