compute precision, recall, F1 values for rpart package result
下面的数据框是我的全部数据集的示例:
1 2 3 4 5 | A B C D E target 0.2 0.5 0.6 -0.5 -0.7 1 0.9 0.7 0.4 -0.3 -0.8 0 0.1 0.3 0.5 -0.9 -0.2 0 0.2 0.5 0.6 -0.5 -0.6 1 |
我想在其上应用分类树,因此我使用了以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | data$target<-factor(data$target) # Create Training Data train.ind <- sample(nrow(data), 0.7*nrow(data)) trainData<-data[train.ind,] testData<-data[-train.ind,] library("rpart") tree <- rpart(target ~.,data=trainData) ypred=predict(tree,testData) library(caret) #Print a confusion matrix result <- confusionMatrix(ypred,testData$target) |
Error:
data andreference should be factors with the same levels.
1 2 3 4 5 | precision <- result$byClass['Pos Pred Value'] recall <- result$byClass['Sensitivity'] f_measure <- 2 * ((precision * recall) / (precision + recall)) #OR f_measure <-result$byClass['F1'] |
但是它没有用。我需要精度,召回率和F1值,但我不知道如何使用" rpart "包结果进行计算。
函数
这应该可以解决问题:
1 2 3 4 | ypred <- factor(predict(tree, testData[, -6], type="vector"), levels = levels(testData$target)) library(caret) confusionMatrix(ypred, testData$target) |
使用
Warning message: In confusionMatrix.default(ypred, testData$target) :
Levels are not in the same order for reference and data. Refactoring
data to match.