r - R 中 SVM 的错误

标签 r machine-learning statistics 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

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

最佳答案

来自文档:

对于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

关于r - R 中 SVM 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27864446/

相关文章:

python - 检查 DV 的已解释和未解释方差

Python + Scikit-learn :How to plot the curves of training score and validation score against the additive smoothing parameter alpha

statistics - 如何从数据表中计算多变量公式

r - 在 R 基本图中的文本字段周围放置一个框架

c++ - 在 Rcpp 中使用 3rd 方头文件

R 数据框,在控制两个变量的同时进行放回采样

r - 将向量分配给 data.table 列时出现打印错误

opencv - 【CVAT】如何在一项任务中创建多个工作?

Python-Matplotlib : normalize axis when plotting a Probability Density Function

android - 使用 API 从 Google Play 开发者那里获取统计信息