简体   繁体   中英

I am trying to write a function in R that finds the confusion matrix using a svm for classification.

I am trying to write a program where the data and the place holder for the y (output) variable are given to the function. The function produces the confusion matrix for the data set and the test data. This is in fact my 5th attempt at this sort of function- which is why most of this function is from a manual using the iris data as the data set- but I seem to get stuck on the y.vec input for the function. Is my method for inserting the y variable into the function correct?

Any help would be appreciated and thank you in advance.

Here is my function.

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])    

}

Hope this helps!

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM