Carry out dimensionality reduction using Uniform Manifold Approximation and Projection (UMAP).
Training uses uwot::umap2() and prediction embeds new observations using uwot::umap_transform().
Format
R6Class object inheriting from PipeOpTaskPreproc/PipeOp.
Construction
id::character(1)
Identifier of resulting object, default"umap".param_vals:: namedlist
List of hyperparameter settings, overwriting the hyperparameter settings that would otherwise be set during construction. Defaultlist().
Input and Output Channels
Input and output channels are inherited from PipeOpTaskPreproc.
The output is the input Task with all affected numeric and integer features replaced by their
UMAP embedding coordinates.
State
The $state is a named list with the $state elements inherited from PipeOpTaskPreproc, as well as the
elements of the model returned from uwot::umap2().
The model's internal structure is determined by uwot and may change between versions.
These are in particular:
embedding::matrix
Matrix of embedded coordinates.scale_info:: namedlist()
IfscaleisTRUE, this gives the scaling attributes (center,scale,nzvcols) of the scaled data.search_k::numeric(1)
Number of nodes searched during the neighbor retrieval. Only used if thenn_methodis"annoy". For details, seeuwot::umap2().local_connectivity::numeric(1)
Used local connectivity – i.e. the number of nearest neighbors that should be assumed to be connected at a local level. For details, seeuwot::umap2().n_epochs::numeric(1)
Number of epochs used during the optimization of the embedded coordinates. For details, seeuwot::umap2().alpha::numeric(1)
Initial learning rate. For details, seeuwot::umap2().negative_sample_rate::numeric(1)
The number of negative edge/1-simplex samples used per positive edge/1-simplex sample in optimizing the low dimensional embedding. For details, seeuwot::umap2().method::character(1)
Method used for dimensionality reduction. Usually"umap";uwot::umap2()uses"tumap"whena = 1,b = 1, anddens_scale = NULL.a:: namednumeric(1)
More specific parameters controlling the embedding. For details, seeuwot::umap2().b:: namednumeric(1)
More specific parameters controlling the embedding. For details, seeuwot::umap2().gamma::numeric(1)
Repulsion strength. Weighting applied to negative samples in low dimensional embedding optimization. For details, seeuwot::umap2().approx_pow::logical(1)
IfTRUE, use an approximation to the power function in the UMAP gradient. For details, seeuwot::umap2().metric:: namedlist()
Type of distance metric used to find nearest neighbors. For details, seeuwot::umap2().norig_col::integer(1)
Number of original columns.pcg_rand::logical(1)
Value of the legacypcg_randparameter. For details, seeuwot::umap2().batch::logical(1)TRUE, if embedding coordinates were updated at the end of each epoch rather than during the epoch. For details, seeuwot::umap2().opt_args:: namedlist()
Optimizer parameters, used whenbatch = TRUE. For details, seeuwot::umap2().num_precomputed_nns::numeric(1)
Number of precomputed nearest neighbors, viann_method.min_dist::numeric(1)
The effective minimum distance between embedded points. For details, seeuwot::umap2().spread::numeric(1)
The effective scale of embedded points. For details, seeuwot::umap2().binary_edge_weights::logical(1)
IfTRUEthen edge weights in the input graph were treated as binary (0/1) rather than real valued. For details, seeuwot::umap2().seed::integer(1)
Integer seed to use to initialize the random number generator state. For details, seeuwot::umap2().nn_method::any
Method for finding nearest neighbors. For details, seeuwot::umap2().nn_args::list()
A list containing additional arguments to pass to the nearest neighbor method. For details, seeuwot::umap2().n_neighbors::numeric(1)
The size of the neighborhood used for manifold approximation. For details, seeuwot::umap2().nn_index:: namedlist()
Nearest neighbor index that can be used for transformation of new data points. Not copied by$clone(deep = TRUE), see below.pca_models::list()
PCA models used for preprocessing whenpcais specified. For details, seeuwot::umap2().
If nn_method is "annoy" or "hnsw", nn_index$ann is an external C++ object that cannot be copied in memory.
Therefore, $clone(deep = TRUE) does not copy it, and the clone shares the nearest neighbor index with the original.
This is safe for regular use, because prediction only reads the index and training replaces the $state as a whole.
However, modifying the shared index directly, e.g. with uwot::unload_uwot(), affects all clones.
This is the same behavior as for the $model of a Learner,
which $clone(deep = TRUE) does not copy either.
Parameters
The parameters are the parameters inherited from PipeOpTaskPreproc, as well as:
n_neighbors::integer(1)
The size of the neighborhood used for manifold approximation. Default is15. For details, seeuwot::umap2().n_components::integer(1)
The dimension of the space to embed into. Default is2. For details, seeuwot::umap2().metric::character(1)
Type of distance metric to use to find nearest neighbors. Default is"euclidean". For details, seeuwot::umap2().n_epochs::integer(1)
Number of epochs to use during the optimization of the embedded coordinates. Default isNULL, whichuwot::umap2()resolves to500. Set to0to return the initialized coordinates without optimization. For details, seeuwot::umap2().learning_rate::numeric(1)
Initial learning rate used in optimization of the coordinates. Default is1. For details, seeuwot::umap2().scale::logical(1)/character(1)
Scaling to apply to the data. IfTRUE, data is standardized. Default isFALSE. For details, seeuwot::umap2().init::character(1)
Type of initialization for the coordinates. May be set to"custom", in which case thematrixof initial coordinates passed toinit_customis used. Default is"spectral". For details, seeuwot::umap2().init_custom::matrix
Matrix of initial coordinates. Only used, ifinitis"custom".init_sdev::character(1)|numeric(1)
Scales each dimension of the initialized coordinates to this standard deviation. Default is"range", which scales each column to the range 0 to 10. Set toNULLto disable scaling of the initial coordinates. For details, seeuwot::umap2().spread::numeric(1)
The effective scale of embedded points. Default is1. For details, seeuwot::umap2().min_dist::numeric(1)
The effective minimum distance between embedded points. Default is0.1. For details, seeuwot::umap2().set_op_mix_ratio::numeric(1)
Interpolate between (fuzzy) union and intersection as the set operation used to combine local fuzzy simplicial sets to obtain a global fuzzy simplicial sets. Default is1. For details, seeuwot::umap2().local_connectivity::numeric(1)
The local connectivity required – i.e. the number of nearest neighbors that should be assumed to be connected at a local level. Default is1. For details, seeuwot::umap2().bandwidth::numeric(1)
The effective bandwidth of the kernel if we view the algorithm as similar to Laplacian Eigenmaps. Default is1. For details, seeuwot::umap2().repulsion_strength::numeric(1)
Weighting applied to negative samples in low dimensional embedding optimization. Default is1. For details, seeuwot::umap2().negative_sample_rate::numeric(1)
The number of negative edge/1-simplex samples to use per positive edge/1-simplex sample in optimizing the low dimensional embedding. Default is5. For details, seeuwot::umap2().a::numeric(1)
More specific parameters controlling the embedding. Default isNULL. For details, seeuwot::umap2().b::numeric(1)
More specific parameters controlling the embedding. Default isNULL. For details, seeuwot::umap2().nn_method::character(1)
Method for finding nearest neighbors. Note that only values compatible withuwot::umap_transform()are allowed. Choose"annoy","hnsw", or"nndescent". Default isNULL, which letsuwot::umap2()select a method based on the installed packages and metrics. The"hnsw"and"nndescent"methods require the optional packagesRcppHNSWandrnndescent, respectively.n_trees::integer(1)
Number of trees to build when constructing the nearest neighbor index. Default is50. For details, seeuwot::umap2().search_k::integer(1)
Number of nodes to search during the neighbor retrieval. Only used if thenn_methodis"annoy". Defaults to2 * n_neighbors * n_treesinuwot::umap2(). For details, seeuwot::umap2().approx_pow::logical(1)
IfTRUE, use an approximation to the power function in the UMAP gradient. Default isFALSE. Ignored whendens_scaleis notNULL. For details, seeuwot::umap2().use_supervised::logical(1)
IfTRUE, perform supervised dimension reduction by passing the task's target touwot::umap2()'syargument. For details, see there. Initialized toFALSE.target_n_neighbors::integer(1)
Number of nearest neighbors to use to construct the target simplicial set. Only used when performing supervised dimension reduction. Default isn_neighbors. For details, seeuwot::umap2().target_metric::character(1)
The metric used to measure distance for a numeric target when performing supervised dimension reduction. Factor targets are handled as categorical data byuwot. Default is"euclidean". For details, seeuwot::umap2().target_weight::numeric(1)
Weighting factor between data topology and target topology. Only used when performing supervised dimension reduction. Default is0.5. For details, seeuwot::umap2().pca::integer(1)
Reduce data to this number of columns using PCA. Default isNULL. For details, seeuwot::umap2().pca_center::logical(1)
IfTRUE, center the columns of X before carrying out PCA. Default isTRUE. For details, seeuwot::umap2().rng_type::character(1)
Random number generator used during optimization:"pcg","tausworthe", or"deterministic". Default isNULL, which uses the upstream default of"pcg", or"tausworthe"whenfast_sgd = TRUE. The"deterministic"setting applies to optimization, not to neighbor search or initialization. For details, seeuwot::umap2().fast_sgd::logical(1)
IfTRUE, usesn_sgd_threads = "auto",approx_pow = TRUE, and the Tausworthe generator unlessrng_typeis set explicitly. Default isFALSE. For details, seeuwot::umap2().n_threads::integer(1)
Number of threads used for neighbor search and other work outside embedding optimization. Default isNULL, which letsuwotchoose the thread count. For details, seeuwot::umap2().n_build_threads::integer(1)
Number of threads used to build the neighbor index for"hnsw"and"nndescent". Default isNULL, which usesn_threads. Set to1to improve reproducibility of index construction. Annoy always builds its index with one thread. For details, seeuwot::umap2().n_sgd_threads::integer(1)
Number of threads to use during stochastic gradient descent. Default is0. During training,uwot::umap2()usesn_threadswhenbatch = TRUEandn_sgd_threads = 0. The special value"auto"also usesn_threads. For details, seeuwot::umap2().grain_size::integer(1)
The minimum amount of work to do on each thread. Default is1. For details, seeuwot::umap2().verbose::logical(1)
Should details be printed? Initialized toFALSE. For details, seeuwot::umap2().batch::logical(1)
IfTRUE, then embedding coordinates are updated at the end of each epoch rather than during the epoch. Default isTRUE. For details, seeuwot::umap2().opt_args:: namedlist()
A list of optimizer parameters, used whenbatch = TRUE. Default isNULL. For details, seeuwot::umap2().epoch_callback::function
A function which will be invoked at the end of every epoch. Default isNULL. For details, seeuwot::umap2().pca_method::character(1)
Method to carry out any PCA dimensionality reduction when thepcais specified. Choose"irlba","svdr","bigstatsr","svd", or"auto". Default isNULL. For details, seeuwot::umap2().binary_edge_weights::logical(1)
IfTRUEthen edge weights in the input graph are treated as binary (0/1) rather than real valued. Default isFALSE. For details, seeuwot::umap2().dens_scale::numeric(1)
A scaling factor to apply to the density of the input data. Default isNULL. For details, seeuwot::umap2().seed::integer(1)
Integer seed to use to initialize the random number generator state. Default isNULL. For details, seeuwot::umap2().nn_args:: namedlist()
A list containing additional arguments to pass to the nearest neighbor method. Default islist(). For details, seeuwot::umap2().
Additionally, there are several parameters that may be used to overwrite parameter values for prediction:
search_k_transform::integer(1)
Number of nodes to search during the neighbor retrieval when predicting. Only used ifnn_methodis"annoy". IfNULL,search_kis used instead. Default isNULL. For details, seeuwot::umap_transform().n_epochs_transform::integer(1)
Number of epochs used during the optimization of the embedded coordinates when predicting. Default isNULL, which uses one third of the training epochs, rounded and with a minimum of two. Set to0to return the initialized coordinates without optimization. For details, seeuwot::umap_transform().init_transform::character(1)
Type of initialization for the coordinates when predicting. May be set to"custom", in which case the matrix passed toinit_transform_customis used. Default is"weighted". For details, seeuwot::umap_transform().init_transform_custom::matrix
Matrix of initial coordinates when predicting. Only used ifinit_transformis"custom".batch_transform::logical(1)
IfTRUE, embedding coordinates are updated at the end of each epoch rather than during the epoch when predicting. Default isNULL, which uses the value ofbatchstored in the trained model. For details, seeuwot::umap_transform().learning_rate_transform::numeric(1)
Initial learning rate used in optimization of the coordinates when predicting. IfNULL,learning_rateis used instead. Default isNULL. For details, seeuwot::umap_transform().opt_args_transform:: namedlist()
Optimizer parameters used when predicting in batch mode. Default isNULL, which uses the optimizer parameters stored in the trained model. For details, seeuwot::umap_transform().epoch_callback_transform::function
A function which will be invoked at the end of every epoch when predicting. Default isNULL. For details, seeuwot::umap_transform().seed_transform::integer(1)
Random seed used when predicting. Default isNULL, which uses the seed stored in the trained model, if any. Set toFALSEto use the current random number generator state without resetting the seed. For details, seeuwot::umap_transform().
Internals
Uses uwot::umap2() with ret_model = TRUE for training and uwot::umap_transform() for prediction.
Fields
Only fields inherited from PipeOp.
Methods
Only methods inherited from PipeOpTaskPreproc/PipeOp.
References
McInnes L, Healy J, Melville J, Grossberger L (2018). “UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction.” Journal of Open Source Software, 3(29). doi:10.21105/joss.00861 .
See also
https://mlr-org.com/pipeops.html
Other PipeOps:
PipeOp,
PipeOpEncodePL,
PipeOpEnsemble,
PipeOpImpute,
PipeOpTargetTrafo,
PipeOpTaskPreproc,
PipeOpTaskPreprocSimple,
mlr_pipeops,
mlr_pipeops_adas,
mlr_pipeops_blsmote,
mlr_pipeops_boxcox,
mlr_pipeops_branch,
mlr_pipeops_chunk,
mlr_pipeops_classbalancing,
mlr_pipeops_classifavg,
mlr_pipeops_classweights,
mlr_pipeops_classweightsex,
mlr_pipeops_colapply,
mlr_pipeops_collapsefactors,
mlr_pipeops_colroles,
mlr_pipeops_copy,
mlr_pipeops_datefeatures,
mlr_pipeops_decode,
mlr_pipeops_encode,
mlr_pipeops_encodeimpact,
mlr_pipeops_encodelmer,
mlr_pipeops_encodeplquantiles,
mlr_pipeops_encodepltree,
mlr_pipeops_featureunion,
mlr_pipeops_filter,
mlr_pipeops_fixfactors,
mlr_pipeops_histbin,
mlr_pipeops_ica,
mlr_pipeops_imputeconstant,
mlr_pipeops_imputehist,
mlr_pipeops_imputelearner,
mlr_pipeops_imputemean,
mlr_pipeops_imputemedian,
mlr_pipeops_imputemode,
mlr_pipeops_imputeoor,
mlr_pipeops_imputesample,
mlr_pipeops_info,
mlr_pipeops_isomap,
mlr_pipeops_kernelpca,
mlr_pipeops_learner,
mlr_pipeops_learner_pi_cvplus,
mlr_pipeops_learner_quantiles,
mlr_pipeops_materialize,
mlr_pipeops_missind,
mlr_pipeops_modelmatrix,
mlr_pipeops_multiplicityexply,
mlr_pipeops_multiplicityimply,
mlr_pipeops_mutate,
mlr_pipeops_nearmiss,
mlr_pipeops_nmf,
mlr_pipeops_nop,
mlr_pipeops_ovrsplit,
mlr_pipeops_ovrunite,
mlr_pipeops_pca,
mlr_pipeops_proxy,
mlr_pipeops_quantilebin,
mlr_pipeops_randomprojection,
mlr_pipeops_randomresponse,
mlr_pipeops_regravg,
mlr_pipeops_removeconstants,
mlr_pipeops_renamecolumns,
mlr_pipeops_replicate,
mlr_pipeops_rowapply,
mlr_pipeops_scale,
mlr_pipeops_scalemaxabs,
mlr_pipeops_scalerange,
mlr_pipeops_select,
mlr_pipeops_smote,
mlr_pipeops_smotenc,
mlr_pipeops_spatialsign,
mlr_pipeops_splines,
mlr_pipeops_subsample,
mlr_pipeops_targetinvert,
mlr_pipeops_targetmutate,
mlr_pipeops_targettrafoscalerange,
mlr_pipeops_textvectorizer,
mlr_pipeops_threshold,
mlr_pipeops_tomek,
mlr_pipeops_tunethreshold,
mlr_pipeops_unbranch,
mlr_pipeops_updatetarget,
mlr_pipeops_vtreat,
mlr_pipeops_yeojohnson
Examples
library("mlr3")
task = tsk("iris")
train_rows = c(1:40, 51:90, 101:140)
test_rows = setdiff(task$row_ids, train_rows)
pop = po("umap", nn_method = "annoy", seed = 42L, n_threads = 1L)
embedded_train = pop$train(list(task$clone()$filter(train_rows)))[[1L]]
embedded_test = pop$predict(list(task$clone()$filter(test_rows)))[[1L]]
head(embedded_train$data())
#> Species V1 V2
#> <fctr> <num> <num>
#> 1: setosa -6.811571 0.7999146
#> 2: setosa -8.576679 1.2044388
#> 3: setosa -8.277728 0.7480515
#> 4: setosa -8.430686 0.8573458
#> 5: setosa -7.048654 0.5664133
#> 6: setosa -6.036652 0.4650353
head(embedded_test$data())
#> Species V1 V2
#> <fctr> <num> <num>
#> 1: setosa -6.937847 1.4724518
#> 2: setosa -8.967123 1.3046246
#> 3: setosa -9.136282 0.9788051
#> 4: setosa -6.885417 1.6396841
#> 5: setosa -6.812132 0.1067286
#> 6: setosa -8.946117 1.4151392
