【问题标题】:Writing a function that finds the confusion matrix using a svm for classification编写一个函数,使用 svm 找到混淆矩阵进行分类
【发布时间】:2018-02-20 02:35:52
【问题描述】:

我正在尝试编写一个程序,其中将 y(输出)变量的数据和占位符提供给函数。该函数为数据集和测试数据生成混淆矩阵。这实际上是我对这种函数的第五次尝试——这就是为什么这个函数的大部分来自使用虹膜数据作为数据集的手册——但我似乎卡在函数的 y.vec 输入上。我将 y 变量插入函数的方法是否正确?

这是我的功能。

function(data,y.vec)

{       
    library(e1071)
    library(rpart)
    data=data
  
    index <- 1:nrow(data)
    testindex <- sample(index, trunc(length(index)/3))
    testset <- data[testindex,]
    trainset <- data[-testindex,]
    
    svm.model <- svm(as.factor(data[y.vec]) ~ ., data = trainset, cost = 100, gamma = 1)
    svm.pred <- predict(svm.model, testset[,-y.vec])
    
    table(pred = svm.pred, true = testset[,y.vec])    
}

【问题讨论】:

    标签: r svm confusion-matrix


    【解决方案1】:
    myFunc <- function(df, y.vec)
      {
        library(e1071) 
      
        df[,y.vec] <- as.factor(df[,y.vec])
        
        set.seed(1)
        index <- 1:nrow(df)
        testindex <- sample(index, trunc(length(index)/3))
        testset <- df[testindex,]
        trainset <- df[-testindex,]
        
        svm.model <- svm(as.formula(paste(y.vec, "~ .")), data = trainset, cost = 100, gamma = 1)
        svm.pred <- predict(svm.model, testset[,!(names(testset) %in% y.vec)])
        
        return(table(pred = svm.pred, true = testset[,y.vec]))
      }
    
    myFunc(iris, "Species")
    

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 2016-03-27
      • 2017-01-13
      • 1970-01-01
      • 2020-04-11
      • 2020-01-20
      • 2016-08-25
      • 2013-09-17
      • 2020-08-21
      相关资源
      最近更新 更多