[英]I am not able to generate the confusion matrix of a classification with One Class in R
[英]I am trying to write a function in R that finds the confusion matrix using a svm for classification.
我正在嘗試編寫一個程序,其中將y(輸出)變量的數據和占位符賦予函數。 該函數為數據集和測試數據生成混淆矩陣。 實際上,這是我對此類功能的第五次嘗試-這就是為什么該功能大部分來自使用虹膜數據作為數據集的手冊的原因,但是我似乎陷入了該功能的y.vec輸入中。 我將y變量插入函數的方法正確嗎?
任何幫助將不勝感激,並提前感謝您。
這是我的功能。
功能(數據,y.vec)
{
庫(e1071)庫(rpart)數據=數據
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])
}
希望這可以幫助!
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.