| Title: | Abstract Classes for Building 'scikit-learn' Like API |
|---|---|
| Description: | Provides 'R6' abstract classes for building machine learning models with 'scikit-learn' like API. <https://scikit-learn.org/> is a popular module for 'Python' programming language which design became de facto a standard in industry for machine learning tasks. |
| Authors: | Dmitriy Selivanov |
| Maintainer: | Dmitriy Selivanov <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-05-19 06:38:34 UTC |
| Source: | https://github.com/cran/mlapi |
Generic function to fit models (inherited from mlapiEstimation)
fit(x, model, y = NULL, ...) ## S3 method for class 'Matrix' fit(x, model, y = NULL, ...) ## S3 method for class 'matrix' fit(x, model, y = NULL, ...)fit(x, model, y = NULL, ...) ## S3 method for class 'Matrix' fit(x, model, y = NULL, ...) ## S3 method for class 'matrix' fit(x, model, y = NULL, ...)
x |
A matrix like object, should inherit from |
model |
instance of class |
y |
|
... |
additional data/model dependent arguments to downstream functions. |
invisible(object$self())
Generic function to fit transformers (inherits from mlapiTransformation)
fit_transform(x, model, y = NULL, ...) ## S3 method for class 'Matrix' fit_transform(x, model, y = NULL, ...) ## S3 method for class 'matrix' fit_transform(x, model, y = NULL, ...)fit_transform(x, model, y = NULL, ...) ## S3 method for class 'Matrix' fit_transform(x, model, y = NULL, ...) ## S3 method for class 'matrix' fit_transform(x, model, y = NULL, ...)
x |
A matrix like object, should inherit from |
model |
instance of class |
y |
|
... |
additional data/model dependent arguments to downstream functions. |
Transformed version of the x
Base class for all decompositions which are methods which can decompose matrix into
2 low-dimensional matrices x = f(A, B).
(Think of this Latent Dirichlet Allocation, Non-negative Matrix Factorization, etc).
It iherits from mlapiTransformation and additionally requires to implement components member.
Base class for all decompositions which are methods which can decompose matrix into
2 low-dimensional matrices x = f(A, B) incrementally.
It iherits from mlapiDecomposition and additionally requires
to implement partial_fit method which can learn components incrementally.
mlapiDecomposition mlapiDecompositionOnlinemlapiDecomposition mlapiDecompositionOnline
R6Class object.
componentsfeatures embeddings. So if matrix is decomposed in a form x = f(A, B) where
X = n\*m, A = n\*k, B = k\*m them B = components
componentsfeatures embeddings. So if matrix is decomposed in a form x = f(A, B) where
X = n\*m, A = n\*k, B = k\*m them B = components
$fit_transform(x, y = NULL, ...)$transform(x, ...)Performs transformation of the new data (after model was trained)
$fit_transform(x, y = NULL, ...)$partial_fit(x, y = NULL, ...)$transform(x, ...)Performs transformation of the new data (after model was trained)
A matrix like object, should inherit from Matrix or matrix.
Allowed classes should be defined in child classes.
NULL. Optional taget variable. Usually this should be NULL.
There few cases when it could be used.
additional parameters with default values
A matrix like object, should inherit from Matrix or matrix.
Allowed classes should be defined in child classes.
NULL. Optional taget variable. Usually this should be NULL.
There few cases when it could be used.
additional parameters with default values
TruncatedSVD = R6::R6Class( classname = "TruncatedSVD", inherit = mlapi::mlapiDecomposition, public = list( initialize = function(rank = 10) { private$rank = rank super$set_internal_matrix_formats(dense = "matrix", sparse = NULL) }, fit_transform = function(x, ...) { x = super$check_convert_input(x) private$n_features = ncol(x) svd_fit = svd(x, nu = private$rank, nv = private$rank, ...) sing_values = svd_fit$d[seq_len(private$rank)] result = svd_fit$u %*% diag(x = sqrt(sing_values)) private$components_ = t(svd_fit$v %*% diag(x = sqrt(sing_values))) rm(svd_fit) rownames(result) = rownames(x) colnames(private$components_) = colnames(x) private$fitted = TRUE invisible(result) }, transform = function(x, ...) { if (private$fitted) { stopifnot(ncol(x) == ncol(private$components_)) lhs = tcrossprod(private$components_) rhs = as.matrix(tcrossprod(private$components_, x)) t(solve(lhs, rhs)) } else stop("Fit the model first woth model$fit_transform()!") } ), private = list( rank = NULL, n_features = NULL, fitted = NULL ) ) set.seed(1) model = TruncatedSVD$new(2) x = matrix(sample(100 * 10, replace = TRUE), ncol = 10) x_trunc = model$fit_transform(x) dim(x_trunc) x_trunc_2 = model$transform(x) sum(x_trunc_2 - x_trunc) #' check pipe-compatible S3 interface x_trunc_2_s3 = transform(x, model) identical(x_trunc_2, x_trunc_2_s3)TruncatedSVD = R6::R6Class( classname = "TruncatedSVD", inherit = mlapi::mlapiDecomposition, public = list( initialize = function(rank = 10) { private$rank = rank super$set_internal_matrix_formats(dense = "matrix", sparse = NULL) }, fit_transform = function(x, ...) { x = super$check_convert_input(x) private$n_features = ncol(x) svd_fit = svd(x, nu = private$rank, nv = private$rank, ...) sing_values = svd_fit$d[seq_len(private$rank)] result = svd_fit$u %*% diag(x = sqrt(sing_values)) private$components_ = t(svd_fit$v %*% diag(x = sqrt(sing_values))) rm(svd_fit) rownames(result) = rownames(x) colnames(private$components_) = colnames(x) private$fitted = TRUE invisible(result) }, transform = function(x, ...) { if (private$fitted) { stopifnot(ncol(x) == ncol(private$components_)) lhs = tcrossprod(private$components_) rhs = as.matrix(tcrossprod(private$components_, x)) t(solve(lhs, rhs)) } else stop("Fit the model first woth model$fit_transform()!") } ), private = list( rank = NULL, n_features = NULL, fitted = NULL ) ) set.seed(1) model = TruncatedSVD$new(2) x = matrix(sample(100 * 10, replace = TRUE), ncol = 10) x_trunc = model$fit_transform(x) dim(x_trunc) x_trunc_2 = model$transform(x) sum(x_trunc_2 - x_trunc) #' check pipe-compatible S3 interface x_trunc_2_s3 = transform(x, model) identical(x_trunc_2, x_trunc_2_s3)
Base class for all estimators. Defines minimal set of members and methods(with signatires) which have to be implemented in child classes.
mlapiEstimationmlapiEstimation
R6Class object.
$fit(x, y, ...)$predict(x, ...)Makes predictions on new data (after model was trained)
A matrix like object, should inherit from Matrix or matrix.
Allowed classes should be defined in child classes.
target - usually vector, but also can be a matrix like object.
Allowed classes should be defined in child classes.
additional parameters with default values
SimpleLinearModel = R6::R6Class( classname = "mlapiSimpleLinearModel", inherit = mlapi::mlapiEstimation, public = list( initialize = function(tol = 1e-7) { private$tol = tol super$set_internal_matrix_formats(dense = "matrix", sparse = NULL) }, fit = function(x, y, ...) { x = super$check_convert_input(x) stopifnot(is.vector(y)) stopifnot(is.numeric(y)) stopifnot(nrow(x) == length(y)) private$n_features = ncol(x) private$coefficients = .lm.fit(x, y, tol = private$tol)[["coefficients"]] }, predict = function(x) { stopifnot(ncol(x) == private$n_features) x %*% matrix(private$coefficients, ncol = 1) } ), private = list( tol = NULL, coefficients = NULL, n_features = NULL )) set.seed(1) model = SimpleLinearModel$new() x = matrix(sample(100 * 10, replace = TRUE), ncol = 10) y = sample(c(0, 1), 100, replace = TRUE) model$fit(as.data.frame(x), y) res1 = model$predict(x) # check pipe-compatible S3 interface res2 = predict(x, model) identical(res1, res2)SimpleLinearModel = R6::R6Class( classname = "mlapiSimpleLinearModel", inherit = mlapi::mlapiEstimation, public = list( initialize = function(tol = 1e-7) { private$tol = tol super$set_internal_matrix_formats(dense = "matrix", sparse = NULL) }, fit = function(x, y, ...) { x = super$check_convert_input(x) stopifnot(is.vector(y)) stopifnot(is.numeric(y)) stopifnot(nrow(x) == length(y)) private$n_features = ncol(x) private$coefficients = .lm.fit(x, y, tol = private$tol)[["coefficients"]] }, predict = function(x) { stopifnot(ncol(x) == private$n_features) x %*% matrix(private$coefficients, ncol = 1) } ), private = list( tol = NULL, coefficients = NULL, n_features = NULL )) set.seed(1) model = SimpleLinearModel$new() x = matrix(sample(100 * 10, replace = TRUE), ncol = 10) y = sample(c(0, 1), 100, replace = TRUE) model$fit(as.data.frame(x), y) res1 = model$predict(x) # check pipe-compatible S3 interface res2 = predict(x, model) identical(res1, res2)
Base class for all online estimators. This class inherits from mlapiEstimation and
additionally requires to implement $partial_fit(x, y, ...) method. Idea is that user can pass
x, y in chunks and model will be updated/refined incrementally.
mlapiEstimationOnlinemlapiEstimationOnline
R6Class object.
$fit(x, y, ...)$partial_fit(x, y, ...)$predict(x, ...)Makes predictions on new data (after model was trained)
A matrix like object, should inherit from Matrix or matrix.
Allowed classes should be defined in child classes.
target - usually vector, but also can be a matrix like object.
Allowed classes should be defined in child classes.
additional parameters with default values
Base class for all online transformations.
mlapiTransformationmlapiTransformation
R6Class object.
$fit_transform(x, y = NULL, ...)$transform(x, ...)Performs transformation of the new data (after model was trained)
A matrix like object, should inherit from Matrix or matrix.
Allowed classes should be defined in child classes.
NULL. Optional taget variable. Usually this should be NULL.
There few cases when it could be used.
additional parameters with default values
Base class for all online transformations. This class inherits from mlapiTransformation and
additionally requires to implement $partial_fit(x, y, ...) method. Idea is that user can pass
x, y in chunks and model will be updated/refined incrementally.
mlapiTransformationOnlinemlapiTransformationOnline
R6Class object.
$fit_transform(x, y = NULL, ...)$transform(x, ...)Performs transformation of the new data (after model was trained)
A matrix like object, should inherit from Matrix or matrix.
Allowed classes should be defined in child classes.
NULL. Optional taget variable. Usually this should be NULL.
There few cases when it could be used.
additional parameters with default values
Makes predictions on new data using pre-trained model (inherits from mlapiEstimation)
## S3 method for class 'matrix' predict(object, model, ...) ## S3 method for class 'Matrix' predict(object, model, ...)## S3 method for class 'matrix' predict(object, model, ...) ## S3 method for class 'Matrix' predict(object, model, ...)
object |
= x in other methods.
A matrix like object, should inherit from |
model |
object which inherits class mlapiEstimation which
implements method |
... |
additional data/model dependent arguments to downstream functions |
Generic function to transform data with pre-trained model (inherits from mlapiTransformation)
## S3 method for class 'Matrix' transform(`_data`, model, ...) ## S3 method for class 'matrix' transform(`_data`, model, ...)## S3 method for class 'Matrix' transform(`_data`, model, ...) ## S3 method for class 'matrix' transform(`_data`, model, ...)
_data |
= x in other methods.
A matrix like object, should inherit from |
model |
object of class |
... |
additional data/model dependent arguments to downstream functions. |