Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

mlr_measures_classif.costs

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.

For calculation of the score, the confusion matrix is multiplied element-wise with the cost matrix. The costs are then summed up (and potentially divided by the number of observations if normalize is set to TRUE).

This measure requires the Task during scoring to ensure that the rows and columns of the cost matrix are in the same order as in the confusion matrix.

Dictionary

This Measure can be instantiated via the dictionary mlr_measures or with the associated sugar function msr():

mlr_measures$get("classif.costs")
msr("classif.costs")

Meta Information

  • Type: "classif"

  • Range: [0, Inf)

  • Minimize: TRUE

  • Required prediction: 'response'

Super classes

mlr3::Measure -> mlr3::MeasureClassif -> MeasureClassifCosts

Public fields

normalize

(logical(1))
Normalize the costs?

Active bindings

costs

(numeric matrix())
Matrix of costs (truth in columns, predicted response in rows).

Methods

Public methods


Method new()

Creates a new instance of this R6 class.

Usage
MeasureClassifCosts$new()

Method clone()

The objects of this class are cloneable with this method.

Usage
MeasureClassifCosts$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

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

Examples

# 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)

mlr3

Machine Learning in R - Next Generation

v0.11.0
LGPL-3
Authors
Michel Lang [cre, aut] (<https://orcid.org/0000-0001-9754-0393>), Bernd Bischl [aut] (<https://orcid.org/0000-0001-6002-6980>), Jakob Richter [aut] (<https://orcid.org/0000-0003-4481-5554>), Patrick Schratz [aut] (<https://orcid.org/0000-0003-0748-6624>), Giuseppe Casalicchio [ctb] (<https://orcid.org/0000-0001-5324-5966>), Stefan Coors [ctb] (<https://orcid.org/0000-0002-7465-2146>), Quay Au [ctb] (<https://orcid.org/0000-0002-5252-8902>), Martin Binder [aut], Marc Becker [ctb] (<https://orcid.org/0000-0002-8115-0400>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.