【问题标题】:Function to calculate Precision, Recall and Accuracy for 5 different algorithms in R用于计算 R 中 5 种不同算法的精度、召回率和准确率的函数
【发布时间】:2020-06-30 04:16:57
【问题描述】:

我有 4 个不同的数据集,格式如下:

df <- data.frame(var1 = c(319, 77, 222, 107, 167),
                  var2 = c(137, 290, 237, 52, 192),
                  class = c(1,1,0,1,0))

每个都包含 var1、var2 和一个类变量。我得到了以下指示:

编写一个 R 脚本,将数据表作为输入并返回五种差异算法、决策树 (rpart)、朴素贝叶斯 (naiveBayes)、K 最近邻 (knn) 的性能统计信息(精度、召回率和准确率) ) 、支持向量机 (svm) 和人工神经网络 (nnet)。脚本的返回值将是每个算法的 5 x 3 统计矩阵。对于 knn,使用 k=3,对于 svm,使用线性内核,对于 nnet,使用 4 个隐藏节点。要计算统计数据,您将使用 10 折交叉验证。

基本上,我相信我必须编写一个可以传递数据帧的包罗万象的函数,该函数的返回值是上述说明中概述的 5 种不同算法中的每一种的精度、召回率和准确率。有没有一种简洁的方法来执行这个?任何帮助将不胜感激。

【问题讨论】:

  • 你需要训练模型吗?如果是这样,您基本上是在要求某人为您完成整个作业。有很多资源可以在kfold cross validation 上找到帮助编码。尝试编写一个函数,例如function(validation_data, model),并提取模型的预测值并计算您想要的指标。对于不同的模型,提取的预测值是不同的,所以这很可能是你的任务正在测试你的。研究使用 S3 方法和泛型函数
  • @astrofunkswag 对不起,我不清楚。是的,我必须训练模型并根据测试数据进行预测。我正在寻找一个可以在我的用户为这 5 种算法创建的函数中使用的函数。我想我找到了。我相信 caret 包中的 train 函数可以完成所有必要的算法。我想出了一个只有一个参数 df 的函数,它在函数内部将 df 拆分为训练/测试集,为 5 种算法中的每一种执行模型拟合,在测试数据上使用每个模型进行预测,然后增加精度,召回和准确度到一个矩阵并返回矩阵。
  • 我只是从每个算法的混淆矩阵函数中提取精度、召回率和准确率。

标签: r algorithm function precision-recall


【解决方案1】:

假设类变量是第 3 列并命名为“类”,它与我的所有示例数据集一起使用,这是我想出的:

algStats <- function(dataset){
  # Split data into test and train (80/20 train to test)
  trainindex  <- sample(1:nrow(dataset), 0.8 * nrow(dataset))
  TrainData  <- dataset[trainindex, ] # Train data length same for all datasets
  TestData <- dataset[-trainindex, ] # Test data length same for all datasets
  # Declare 10-fold CV (same for all datasets)
  train_control <- trainControl(method="cv", number=10)
  # Train a decision tree model
  DecTreeMod <- train(as.factor(class)~., data=TrainData, 
                       trControl=train_control, method="rpart")
  # Predict on test data using Decision Tree model
  DecTreepred <- predict(DecTreeMod, TestData[,1:2])
  # Create confusion matrix for Decision Tree classifier
  DecTreecf <- confusionMatrix(DecTreepred, as.factor(TestData[,3]), mode = "prec_recall", positive = "1")
  # Extract Precision, Recall and Accuracy from confusion matrix
  DecTreePrecision <- DecTreecf$byClass[5] # <-----Precision
  DecTreeRecall <- DecTreecf$byClass[6] # <-----Recall
  DecTreeAcc <- DecTreecf$overall[1] # <-----Accuracy

  # Create an empty matrix to hold performance measures of each algorithm
  rownames = c("Decision Tree", "Naive Bayes", "KNN", "SVM", "ANN")
  colnames = c("Precision", "Recall", "Accuracy")

  performance <- matrix(ncol = 3, nrow = 5,
     byrow = T, dimnames = list(rownames, colnames))

  # Append the metrics from the Decision Tree classifier into the matrix
  # performance <- rbind(performance, c(DecTreePrecision,DecTreeRecall,DecTreeAcc))
  performance[1,] <- c(DecTreePrecision,DecTreeRecall,DecTreeAcc)

  # Train a Naive Bayes model
  NBMod <- train(as.factor(class)~., data=TrainData, 
                      trControl=train_control, method="nb")
  # Predict on test data using Naive Bayes model
  NBpred <- predict(NBMod, TestData[,1:2])
  # Create confusion matrix for Naive Bayes classifier
  NBcf <- confusionMatrix(NBpred, as.factor(TestData[,3]), mode = "prec_recall", positive = "1")
  # Extract Precision, Recall and Accuracy from confusion matrix
  NBPrecision <- NBcf$byClass[5] # <-----Precision
  NBRecall <- NBcf$byClass[6] # <-----Recall
  NBAcc <- NBcf$overall[1] # <-----Accuracy

  # Append the metrics from the Naive Bayes classifier into the matrix
  performance[2,] <- c(NBPrecision,NBRecall,NBAcc)

  # Train a KNN model
  KNNMod <- train(as.factor(class)~., data=TrainData, tuneGrid = expand.grid(k = 3), 
                 trControl=train_control, method="knn", preProcess = c("center","scale"))
  # Predict on test data using KNN model
  KNNpred <- predict(KNNMod, TestData[,1:2])
  # Create confusion matrix for KNN classifier
  KNNcf <- confusionMatrix(KNNpred, as.factor(TestData[,3]), mode = "prec_recall", positive = "1")
  # Extract Precision, Recall and Accuracy from confusion matrix
  KNNPrecision <- KNNcf$byClass[5] # <-----Precision
  KNNRecall <- KNNcf$byClass[6] # <-----Recall
  KNNAcc <- KNNcf$overall[1] # <-----Accuracy

  # Append the metrics from the KNN classifier into the matrix
  performance[3,] <- c(KNNPrecision,KNNRecall,KNNAcc)

  # Train an SVM model
  SVMMod <- train(as.factor(class)~., data=TrainData, 
                  trControl=train_control, method="svmLinear", preProcess = c("center","scale"))
  # Predict on test data using the SVM model
  SVMpred <- predict(SVMMod, TestData[,1:2])
  # Create confusion matrix for SVM classifier
  SVMcf <- confusionMatrix(SVMpred, as.factor(TestData[,3]), mode = "prec_recall", positive = "1")
  # Extract Precision, Recall and Accuracy from confusion matrix
  SVMPrecision <- SVMcf$byClass[5] # <-----Precision
  SVMRecall <- SVMcf$byClass[6] # <-----Recall
  SVMAcc <- SVMcf$overall[1] # <-----Accuracy

  # Append the metrics from the SVM classifier into the matrix
  performance[4,] <- c(SVMPrecision,SVMRecall,SVMAcc)

  # Train an ANN model
  ANNMod <- train(as.factor(class)~., data=TrainData, tuneGrid = expand.grid(
            size = 4, decay = 0.1), linear.output = F, trControl=train_control, method="nnet",
            preProcess = c("center","scale"))
  # Predict on test data using the ANN model
  ANNpred <- predict(ANNMod, TestData[,1:2])
  # Create confusion matrix for ANN classifier
  ANNcf <- confusionMatrix(ANNpred, as.factor(TestData[,3]), mode = "prec_recall", positive = "1")
  # Extract Precision, Recall and Accuracy from confusion matrix
  ANNPrecision <- ANNcf$byClass[5] # <-----Precision
  ANNRecall <- ANNcf$byClass[6] # <-----Recall
  ANNAcc <- ANNcf$overall[1] # <-----Accuracy

  # Append the metrics from the ANN classifier into the matrix
  performance[5,] <- c(ANNPrecision,ANNRecall,ANNAcc)

  # Return the performance matrix
  return(performance)
}

【讨论】:

    猜你喜欢
    • 2015-12-05
    • 1970-01-01
    • 2020-02-25
    • 2012-11-26
    • 2016-03-26
    • 2017-06-21
    • 2019-07-31
    • 1970-01-01
    • 2015-02-06
    相关资源
    最近更新 更多