r中的一对一方法多类

时间:2017-11-22 11:07:39

标签: r

假设我的数据集是3个类的虹膜,我想实现一个SVM方法,但是当我按class iclass j对每个分类器的训练集进行子集时,我得到一个空子集(请点击此行#selecting subset of training set where Species equal to class i and class j

Species <-iris$Species
class <- unique(Species)
set.seed(123)
s<- sample (150,100)
data_train<- iris[s,]
data_test<- iris[-s,]
train <-data_train
test <-data_test
for(i in 2:length(unique(Species))-1){
  for(j in (i+1):length(unique(Species))){
    print(paste(class[i],class[j],sep=","))


    #selecting subset of training set and testing set where coronaryEvent equal to class i and class j
    train <-subset(train, Species %in% c(class[i],class[j]))
   str(train)


  }}
[1] "setosa,versicolor"
'data.frame':   0 obs. of  5 variables:
 $ Sepal.Length: num 
 $ Sepal.Width : num 
 $ Petal.Length: num 
 $ Petal.Width : num 
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 
[1] "setosa,virginica"
'data.frame':   0 obs. of  5 variables:
 $ Sepal.Length: num 
 $ Sepal.Width : num 
 $ Petal.Length: num 
 $ Petal.Width : num 
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 
[1] "versicolor,virginica"
'data.frame':   0 obs. of  5 variables:
 $ Sepal.Length: num 
 $ Sepal.Width : num 
 $ Petal.Length: num 
 $ Petal.Width : num 
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 

1 个答案:

答案 0 :(得分:1)

这应该有效:

iris$Species

我所做的是将class值直接分配给subset并稍微更改.ToList()。请告诉我这是否符合预期。