Preprocess Date FeaturesSource:
POSIXct columns of the data, a set of date related features is computed and added to
the feature set of the output task. If no
POSIXct column is found, the original task is
returned unaltered. This functionality is based on the
add_cyclic_datepart() functions from the
fastai library. If operation on only particular
POSIXct columns is requested, use the
affect_columns parameter inherited from
cyclic = TRUE, cyclic features are computed for the features
means that for each feature
x, two additional features are computed, namely the sine and cosine
2 * pi * x / max_x (here
max_x is the largest possible value the feature
could take on
+ 1, assuming the lowest possible value is given by 0, e.g., for hours from 0 to
23, this is 24). This is useful to respect the cyclical nature of features such as seconds, i.e.,
second 21 and second 22 are one second apart, but so are second 60 and second 1 of the next
$new(id = "datefeatures", param_vals = list())PipeOpDateFeatures
Identifier of resulting object, default
List of hyperparameter settings, overwriting the hyperparameter settings that would otherwise be set during construction. Default
Input and output channels are inherited from
The output is the input
Task with date-related features computed and added to the
feature set of the output task and the
POSIXct columns of the data removed from the
feature set (depending on the value of
$state is a named
list with the
$state elements inherited from
The parameters are the parameters inherited from
PipeOpTaskPreprocSimple, as well as:
POSIXctcolumns be kept as features? Default FALSE.
Should cyclic features be computed? See Internals. Default FALSE.
Should the year be extracted as a feature? Default TRUE.
Should the month be extracted as a feature? Default TRUE.
Should the week of the year be extracted as a feature? Default TRUE.
Should the day of the year be extracted as a feature? Default TRUE.
Should the day of the month be extracted as a feature? Default TRUE.
Should the day of the week be extracted as a feature? Default TRUE.
Should the hour be extracted as a feature? Default TRUE.
Should the minute be extracted as a feature? Default TRUE.
Should the second be extracted as a feature? Default TRUE.
Should a feature be extracted indicating whether it is day time (06:00am - 08:00pm)? Default TRUE.
The cyclic feature transformation always assumes that values range from 0, so some values (e.g. day of the month) are shifted before sine/cosine transform.
library("mlr3") dat = iris set.seed(1) dat$date = sample(seq(as.POSIXct("2020-02-01"), to = as.POSIXct("2020-02-29"), by = "hour"), size = 150L) task = TaskClassif$new("iris_date", backend = dat, target = "Species") pop = po("datefeatures", param_vals = list(cyclic = FALSE, minute = FALSE, second = FALSE)) pop$train(list(task)) #> $output #> <TaskClassif:iris_date> (150 x 13) #> * Target: Species #> * Properties: multiclass #> * Features (12): #> - dbl (11): Petal.Length, Petal.Width, Sepal.Length, Sepal.Width, #> date.day_of_month, date.day_of_week, date.day_of_year, date.hour, #> date.month, date.week_of_year, date.year #> - lgl (1): date.is_day #> pop$state #> $dt_columns #>  "date" #> #> $affected_cols #>  "Petal.Length" "Petal.Width" "Sepal.Length" "Sepal.Width" "date" #> #> $intasklayout #> id type #> 1: Petal.Length numeric #> 2: Petal.Width numeric #> 3: Sepal.Length numeric #> 4: Sepal.Width numeric #> 5: date POSIXct #> #> $outtasklayout #> id type #> 1: Petal.Length numeric #> 2: Petal.Width numeric #> 3: Sepal.Length numeric #> 4: Sepal.Width numeric #> 5: date.day_of_month numeric #> 6: date.day_of_week numeric #> 7: date.day_of_year numeric #> 8: date.hour numeric #> 9: date.is_day logical #> 10: date.month numeric #> 11: date.week_of_year numeric #> 12: date.year numeric #> #> $outtaskshell #> Empty data.table (0 rows and 13 cols): Species,Petal.Length,Petal.Width,Sepal.Length,Sepal.Width,date.year... #>