binary_class_cm {ConfusionTableR} | R Documentation |
Binary Confusion Matrix data frame
Description
a confusion matrix object for binary classification machine learning problems.
Usage
binary_class_cm(train_labels, truth_labels, ...)
Arguments
train_labels |
the classification labels from the training set |
truth_labels |
the testing set ground truth labels for comparison |
... |
function forwarding for additional 'caret' confusion matrix parameters to be passed such as mode="everything" and positive="class label" |
Value
A list containing the outputs highlighted hereunder:
"confusion_matrix" a confusion matrix list item with all the associated confusion matrix statistics
"record_level_cm" a row by row data.frame version of the above output, to allow for storage in databases and row by row for tracking ML model performance
"cm_tbl" a confusion matrix raw table of the values in the matrix
"last_run"datetime object storing when the function was run
Examples
library(dplyr)
library(ConfusionTableR)
library(caret)
library(tidyr)
library(mlbench)
# Load in the data
data("BreastCancer", package = "mlbench")
breast <- BreastCancer[complete.cases(BreastCancer), ] #Create a copy
breast <- breast[, -1]
breast <- breast[1:100,]
breast$Class <- factor(breast$Class) # Create as factor
for(i in 1:9) {
breast[, i] <- as.numeric(as.character(breast[, i]))
}
#Perform train / test split on the data
train_split_idx <- caret::createDataPartition(breast$Class, p = 0.75, list = FALSE)
train <- breast[train_split_idx, ]
test <- breast[-train_split_idx, ]
rf_fit <- caret::train(Class ~ ., data=train, method="rf")
#Make predictions to expose class labels
preds <- predict(rf_fit, newdata=test, type="raw")
predicted <- cbind(data.frame(class_preds=preds), test)
#ConfusionTableR to produce record level output
cm <- ConfusionTableR::binary_class_cm(predicted$class_preds,predicted$Class)
# Other modes here are mode="prec_recall", mode="sens_spec" and mode="everything"
# Record level output
cm$record_level_cm #Primed for storage in a database table
# List confusion matrix
cm$confusion_matrix
[Package ConfusionTableR version 1.0.4 Index]