Description Usage Arguments Details Value Examples
Used to define a custom pipeline component. Users can define functions fit
, transform
, predict
, or incfit
with desired behavior.
1 2 3 |
classname |
string of class name |
fit |
fit function |
transform |
transform function |
predict |
predict function |
incr_fit |
incremantal fit function |
inv_transform |
inverse-transform function |
predict_proba |
probability predict function |
initialize |
intialize function |
... |
additional class attributes, they must be given with names and there must be no name conflict |
as_private |
names of additional parameters to be stored as private fields |
This function is used to define a custom pipeline component. Users can define functions with desired behavior, together with addional public and private fields.
To be properly incorporated to a pipeline framework, the functions should satisfy the following properties:
fit
, incr_fit
, transform
, predict
, inv_transform
and predict_proba
functions should take exactly two arguments x
and y
initialize
, fit
and incr_fit
functions should return invisible(self)
transform
and inv_transform
functions should return a list of x
and y
Typically, self$object
is used to store the fitted model object, and is updated by fit
and incr_fit
functions. Alternatively, one may also define additional class attributes to store relevant information.
an R6ClassGenerator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | OLSPipe <- custom_pipe_component(
fit = function(x, y) {
x <- cbind(as.matrix(x), 1)
self$object <- solve(crossprod(x), crossprod(x,y))
invisible(self)
},
predict = function(x, y=NULL) {
cbind(as.matrix(x), 1) %*% self$object
}
)
o <- OLSPipe$new()
data(mtcars)
x <- mtcars[, c('wt', 'am')]
y <- mtcars[['mpg']]
o$fit(x, y)
o$predict(x)
MeanCalculator <- custom_pipe_component(
fit = function(x, y=NULL) {
self$sum <- sum(x)
self$n <- length(x)
invisible(self)
},
incr_fit = function(x, y=NULL) {
self$n <- self$n + length(x)
self$sum <- self$sum + sum(x)
invisible(self)
},
predict = function(x=NULL, y=NULL) {
self$sum / self$n
},
initialize = function(...) {
invisible(self)
},
sum=0, n=0
)
m <- MeanCalculator$new()
m$fit(1:9)
m$predict()
m$incr_fit(10)
m$predict()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.