mlr3 (version 0.1.4)

MeasureClassifCosts: Cost-sensitive Classification Measure

Description

Uses a cost matrix to create a classification measure. True labels must be arranged in columns, predicted labels must be arranged in rows. The cost matrix is stored as slot $costs. Costs are aggregated with the mean.

Arguments

Format

R6::R6Class() inheriting from MeasureClassif.

Construction

MeasureClassifCosts$new(costs = NULL, normalize = TRUE)
mlr_measures$get("classif.costs")
msr("classif.costs")
  • costs :: matrix() Numeric matrix of costs (truth in columns, predicted response in rows).

  • normalize :: logical(1) If TRUE, calculate the mean costs instead of the total costs.

See Also

Dictionary of Measures: mlr_measures

as.data.table(mlr_measures) for a complete table of all (also dynamically created) Measure implementations.

Examples

Run this code
# NOT RUN {
# get a cost sensitive task
task = tsk("german_credit")

# cost matrix as given on the UCI page of the german credit data set
# https://archive.ics.uci.edu/ml/datasets/statlog+(german+credit+data)
costs = matrix(c(0, 5, 1, 0), nrow = 2)
dimnames(costs) = list(truth = task$class_names, predicted = task$class_names)
print(costs)

# mlr3 needs truth in columns, predictions in rows
costs = t(costs)

# create measure which calculates the absolute costs
m = msr("classif.costs", id = "german_credit_costs", costs = costs, normalize = FALSE)

# fit models and calculate costs
learner = lrn("classif.rpart")
rr = resample(task, learner, rsmp("cv", folds = 3))
rr$aggregate(m)
# }

Run the code above in your browser using DataLab