# \donttest{
# Attach cvms
library(cvms)
library(ggplot2)
# Two classes
# Create targets and predictions data frame
data <- data.frame(
"target" = c("A", "B", "A", "B", "A", "B", "A", "B",
"A", "B", "A", "B", "A", "B", "A", "A"),
"prediction" = c("B", "B", "A", "A", "A", "B", "B", "B",
"B", "B", "A", "B", "A", "A", "A", "A"),
stringsAsFactors = FALSE
)
# Evaluate predictions and create confusion matrix
eval <- evaluate(
data = data,
target_col = "target",
prediction_cols = "prediction",
type = "binomial"
)
# Inspect confusion matrix tibble
eval[["Confusion Matrix"]][[1]]
# Plot confusion matrix
# Supply confusion matrix tibble directly
plot_confusion_matrix(eval[["Confusion Matrix"]][[1]])
# Plot first confusion matrix in evaluate() output
plot_confusion_matrix(eval)
# Add sum tiles
plot_confusion_matrix(eval, add_sums = TRUE)
# Three (or more) classes
# Create targets and predictions data frame
data <- data.frame(
"target" = c("A", "B", "C", "B", "A", "B", "C",
"B", "A", "B", "C", "B", "A"),
"prediction" = c("C", "B", "A", "C", "A", "B", "B",
"C", "A", "B", "C", "A", "C"),
stringsAsFactors = FALSE
)
# Evaluate predictions and create confusion matrix
eval <- evaluate(
data = data,
target_col = "target",
prediction_cols = "prediction",
type = "multinomial"
)
# Inspect confusion matrix tibble
eval[["Confusion Matrix"]][[1]]
# Plot confusion matrix
# Supply confusion matrix tibble directly
plot_confusion_matrix(eval[["Confusion Matrix"]][[1]])
# Plot first confusion matrix in evaluate() output
plot_confusion_matrix(eval)
# Add sum tiles
plot_confusion_matrix(eval, add_sums = TRUE)
# Counts only
plot_confusion_matrix(
eval[["Confusion Matrix"]][[1]],
add_normalized = FALSE,
add_row_percentages = FALSE,
add_col_percentages = FALSE
)
# Change color palette to green
# Change theme to \code{theme_light}.
plot_confusion_matrix(
eval[["Confusion Matrix"]][[1]],
palette = "Greens",
theme_fn = ggplot2::theme_light
)
# The output is a ggplot2 object
# that you can add layers to
# Here we change the axis labels
plot_confusion_matrix(eval[["Confusion Matrix"]][[1]]) +
ggplot2::labs(x = "True", y = "Guess")
# }
Run the code above in your browser using DataLab