Learn R Programming

UBL (version 0.0.3)

metacostClassif: METACOST algorithm for cost-sensitive classification problems

Description

This function obtains the predicted classes that optimize the utility given a set of probabilities of each class for each test cases, and a cost-benefit matrix

Usage

metacostClassif(preds, cb.matrix)

Arguments

preds
A matrix or data.frame of probabilistic predictions of a classifier for each test case, i.e. a nTestCases x nClasses matrix
cb.matrix
A square matrix with dimensions nClasses x nClasses where entry X_i,j corresponds to the cost/benefit of predicting a test case as belonging to class j when it is of class i. The diagonal of this matrix (correct predictions) should contain positive

Value

  • The function returns a factor of class labels.

Details

This function applies the well-know METACOST (Domingos, 1999) algorithm to obtain the predictions of a probabilistic classifier that maximize the utility according to a provided cost-benefit matrix.

References

Domingos,P. (1999), MetaCost: a general method for making classifiers cost-sensitive. Proceedings of KDD'1999. http://homes.cs.washington.edu/~pedrod/papers/kdd99.pdf

Examples

Run this code
data(iris)

## First lets create the cost-benefit matrix
cb <- matrix(c(30,-100,-100,-100,60,-200,-100,-200,60),byrow=TRUE,ncol=3)
colnames(cb) <- paste("p",levels(iris$Species),sep=".")
rownames(cb) <- paste("t",levels(iris$Species),sep=".")

## This leads to the following cost-benefit matrix
##              p.setosa p.versicolor p.virginica
## t.setosa           30         -100        -100
## t.versicolor     -100           60        -200
## t.virginica      -100         -200          60

## Now learning a model
library(rpart)
idx <- sample(1:nrow(iris),as.integer(0.7*nrow(iris)))
tr <- iris[idx,]
ts <- iris[-idx,]

ct <- rpart(Species ~ ., tr)
prob.preds <- predict(ct,ts)
noprob.preds <- predict(ct,ts,type="class")

metacost.preds <- metacostClassif(prob.preds,cb)

table(noprob.preds,metacost.preds)
table(noprob.preds,ts$Species)
table(metacost.preds,ts$Species)

Run the code above in your browser using DataLab