You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working with a dataset where I use importance weights to specify the misclassification costs of instances. Because the target class in the dataset is severly unbalanced, I would like to use some resample (e.g. SMOTE) to mitigate this issue. However, step_smote() and friends do not impute the importance weights, and I cannot impute them later because this is not allowed by other step_impute methods.
I can understand that the default behaviour should not be to generate new weights as well as this might lead to unexpected behaviour, but I do not see why the algorithms in this package would be unable to do this at all.
Reproducible example
Here is an example using hpc_data:
library(tidymodels)
library(themis)
# First, get rid of the nominal predictors as these cannot be used by `step_smote`hpc_data<- select(hpc_data, -c(protocol, day))
# Now specify the importance weights, for example input_fieldshpc_data<- mutate(hpc_data, input_fields= importance_weights(input_fields))
# Specify a simple recipe to use with `step_smote`rec<- recipe(class~., data=hpc_data) |>
step_smote(class)
# Now prep and bake as training data to see the resultrec|>
prep() |>
bake(NULL)
#> # A tibble: 8,844 × 6#> compounds input_fields iterations num_pending hour class#> <dbl> <imp_wts> <dbl> <dbl> <dbl> <fct>#> 1 997 137 20 0 14 F #> 2 97 103 20 0 13.8 VF #> 3 101 75 10 0 13.8 VF #> 4 93 76 20 0 10.1 VF #> 5 100 82 20 0 10.4 VF #> 6 100 82 20 0 16.5 VF #> 7 105 88 20 0 16.4 VF #> 8 98 95 20 0 16.7 VF #> 9 101 91 20 0 16.2 VF #> 10 95 92 20 0 10.8 VF #> # ℹ 8,834 more rows# This would leave us with 8844 rows, but there are many missing values in input_fieldsrec|>
prep() |>
bake(NULL) |>
drop_na(input_fields) # Only 4331 rows left, the same amount as the original dataset#> # A tibble: 4,331 × 6#> compounds input_fields iterations num_pending hour class#> <dbl> <imp_wts> <dbl> <dbl> <dbl> <fct>#> 1 997 137 20 0 14 F #> 2 97 103 20 0 13.8 VF #> 3 101 75 10 0 13.8 VF #> 4 93 76 20 0 10.1 VF #> 5 100 82 20 0 10.4 VF #> 6 100 82 20 0 16.5 VF #> 7 105 88 20 0 16.4 VF #> 8 98 95 20 0 16.7 VF #> 9 101 91 20 0 16.2 VF #> 10 95 92 20 0 10.8 VF #> # ℹ 4,321 more rows# On the other hand, `step_upsample()` does workrec<- recipe(class~., data=hpc_data) |>
step_upsample(class)
rec|>
prep() |>
bake(NULL) |>
drop_na(input_fields)
#> # A tibble: 8,844 × 6#> compounds input_fields iterations num_pending hour class#> <dbl> <imp_wts> <dbl> <dbl> <dbl> <fct>#> 1 97 103 20 0 13.8 VF #> 2 101 75 10 0 13.8 VF #> 3 93 76 20 0 10.1 VF #> 4 100 82 20 0 10.4 VF #> 5 100 82 20 0 16.5 VF #> 6 105 88 20 0 16.4 VF #> 7 98 95 20 0 16.7 VF #> 8 101 91 20 0 16.2 VF #> 9 95 92 20 0 10.8 VF #> 10 102 96 20 0 9.97 VF #> # ℹ 8,834 more rows
I'm wondering whether this behaviour can be implemented in the functions in this work package, if necessary not as the default behaviour. If there is some other solution that I've missed, I'd be more than happy to learn more about it.
The text was updated successfully, but these errors were encountered:
The main reason why steps such as step_smote() doesn't work with importance weights, is because there isn't information as to how the weights should be inputed.
Propose for example that the importance weight is a measure of oldness. how should step_smote() fill in the weights? there is no assumption that the weights have any relation to the predictors. So it is doing the best it can and fill in with NA.
If you have prior knowledge, you could use step_mutate(w = if_else(is.na(w), importance_weights(52), w)) but you should be very careful when doing it.
And honestly, you would be better off using step_upsample() or step_downsample() as they work with weights.
The problem
I'm working with a dataset where I use importance weights to specify the misclassification costs of instances. Because the target class in the dataset is severly unbalanced, I would like to use some resample (e.g. SMOTE) to mitigate this issue. However,
step_smote()
and friends do not impute the importance weights, and I cannot impute them later because this is not allowed by otherstep_impute
methods.I can understand that the default behaviour should not be to generate new weights as well as this might lead to unexpected behaviour, but I do not see why the algorithms in this package would be unable to do this at all.
Reproducible example
Here is an example using
hpc_data
:Created on 2024-04-15 with reprex v2.1.0
Session info
Proposed solution
I'm wondering whether this behaviour can be implemented in the functions in this work package, if necessary not as the default behaviour. If there is some other solution that I've missed, I'd be more than happy to learn more about it.
The text was updated successfully, but these errors were encountered: