Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time Quantization PipeOp #211

Open
pfistfl opened this issue Sep 7, 2021 · 0 comments
Open

Time Quantization PipeOp #211

pfistfl opened this issue Sep 7, 2021 · 0 comments
Labels
Type: PipeOp Issue that wants a new PipeOp

Comments

@pfistfl
Copy link
Member

pfistfl commented Sep 7, 2021

I am just gonna leave some code here until I have time to improve and create a PR.
The goal is to reduce the number of unique time points during training in order to make prediction faster.
Would guess this should perhaps live here instead of mlr3pipelines?

PipeOpTimeQuantiles = R6::R6Class("PipeOpTimeQuantiles",
  inherit = mlr3pipelines::PipeOpTaskPreproc,
  public = list(

    #' @description
    #'   Initialize a Multi-Calibration PipeOp.
    #'
    #' @param id [`character`] \cr
    #'   The `PipeOp`'s id. Defaults to "mcboost".
    #' @param param_vals [`list`] \cr
    #'   List of hyperparameters for the `PipeOp`.
    initialize = function(id = "timequantiles", param_vals = list()) {
      param_set = paradox::ParamSet$new(list(paradox::ParamInt$new("num_uniques", lower = 0L, upper = Inf, default =500L, tags = "train")))
      param_set$valus = list("num_uniques" = 500L)

      super$initialize(id, param_set = param_set, param_vals = param_vals)
    }
  ),
  private = list(
    .train_task = function(task) {
      times = task$times()
      qs = self$param_set$get_values()$num_uniques
      if (length(unique(times)) < qs) {
        newtimes = times
      } else {
        qt = round(unique(quantile(unique(times), seq(from = 0, to = 1, length.out = qs), type=2)))
      }
      # assign closest match
      new_target = data.table()
      set(new_target, j = task$target_names[[1]], value = qt[apply(abs(outer(qt, times, "-")), 2, which.min)])
      task$cbind(new_target)
    }
  )
  )
)
@bblodfon bblodfon added the Type: PipeOp Issue that wants a new PipeOp label Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: PipeOp Issue that wants a new PipeOp
Projects
None yet
Development

No branches or pull requests

2 participants