mlpredict: Prediction transformation problems

Description Usage Arguments Value How to create a new prediction base method Examples

View source: R/base_learner.R

Description

Base classifiers are used to build models to solve the the transformation problems. To create a new base classifier, two steps are necessary:

  1. Create a train method

  2. Create a prediction method

This section is about how to create the second step: a prediction method. To create a new train method see mltrain documentation.

Usage

1
mlpredict(model, newdata, ...)

Arguments

model

An object model returned by some mltrain method, its class determine the name of this method.

newdata

A data.frame with the new data to be predicted.

...

Others arguments passed to the predict method.

Value

A matrix with the probabilities of each class value/example, where the rows are the examples and the columns the class values.

How to create a new prediction base method

Fist is necessary to know the class of model generate by the respective train method, because this name determines the method name. It must start with 'mlpredict.', followed by the model class name, e.g. a model with class 'fooModel' must be called as mlpredict.fooModel.

After defined the name, you need to implement your prediction base method. The model built on mltrain is available on model parameter and the newdata is the data to be predict.

The return of this method must be a data.frame with two columns called "prediction" and "probability". The first column contains the predicted class and the second the probability/score/confidence of this prediction. The rows represents the examples.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Create a method that predict always the first class
# The model must be of the class 'fooModel'
mlpredict.fooModel <- function (model, newdata, ...) {
   # Predict the first class with a random confidence
   data.frame(
     prediction = rep(model$classes[1], nrow(newdata)),
     probability = sapply(runif(nrow(newdata)), function (score) {
       max(score, 1 - score)
     }),
     row.names = rownames(newdata)
   )
}


# Create a SVM predict method using the e1071 package (the class of SVM model
# from e1071 package is 'svm')
library(e1071)
mlpredict.svm <- function (dataset, newdata, ...) {
   result <- predict(model, newdata, probability = TRUE, ...)
   attr(result, 'probabilities')
}

utiml documentation built on May 31, 2021, 9:09 a.m.