Title: | Extra Recipes Steps for Dealing with Unbalanced Data |
---|---|
Description: | A dataset with an uneven number of cases in each class is said to be unbalanced. Many models produce a subpar performance on unbalanced datasets. A dataset can be balanced by increasing the number of minority cases using SMOTE 2011 <arXiv:1106.1813>, BorderlineSMOTE 2005 <doi:10.1007/11538059_91> and ADASYN 2008 <https://ieeexplore.ieee.org/document/4633969>. Or by decreasing the number of majority cases using NearMiss 2003 <https://www.site.uottawa.ca/~nat/Workshop2003/jzhang.pdf> or Tomek link removal 1976 <https://ieeexplore.ieee.org/document/4309452>. |
Authors: | Emil Hvitfeldt [aut, cre] , Posit Software, PBC [cph, fnd] |
Maintainer: | Emil Hvitfeldt <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.2.9000 |
Built: | 2024-11-09 19:13:17 UTC |
Source: | https://github.com/tidymodels/themis |
Generates synthetic positive instances using ADASYN algorithm.
adasyn(df, var, k = 5, over_ratio = 1)
adasyn(df, var, k = 5, over_ratio = 1)
df |
data.frame or tibble. Must have 1 factor variable and remaining numeric variables. |
var |
Character, name of variable containing factor variable. |
k |
An integer. Number of nearest neighbor that are used to generate the new examples of the minority class. |
over_ratio |
A numeric value for the ratio of the minority-to-majority frequencies. The default value (1) means that all other levels are sampled up to have the same frequency as the most occurring level. A value of 0.5 would mean that the minority levels will have (at most) (approximately) half as many rows than the majority level. |
All columns used in this function must be numeric with no missing data.
A data.frame or tibble, depending on type of df
.
Chawla, N. V., Bowyer, K. W., Hall, L. O., and Kegelmeyer, W. P. (2002). Smote: Synthetic minority over-sampling technique. Journal of Artificial Intelligence Research, 16:321-357.
step_adasyn()
for step function of this method
Other Direct Implementations:
bsmote()
,
nearmiss()
,
smote()
,
smotenc()
,
tomek()
circle_numeric <- circle_example[, c("x", "y", "class")] res <- adasyn(circle_numeric, var = "class") res <- adasyn(circle_numeric, var = "class", k = 10) res <- adasyn(circle_numeric, var = "class", over_ratio = 0.8)
circle_numeric <- circle_example[, c("x", "y", "class")] res <- adasyn(circle_numeric, var = "class") res <- adasyn(circle_numeric, var = "class", k = 10) res <- adasyn(circle_numeric, var = "class", over_ratio = 0.8)
BSMOTE generates generate new examples of the minority class using nearest neighbors of these cases in the border region between classes.
bsmote(df, var, k = 5, over_ratio = 1, all_neighbors = FALSE)
bsmote(df, var, k = 5, over_ratio = 1, all_neighbors = FALSE)
df |
data.frame or tibble. Must have 1 factor variable and remaining numeric variables. |
var |
Character, name of variable containing factor variable. |
k |
An integer. Number of nearest neighbor that are used to generate the new examples of the minority class. |
over_ratio |
A numeric value for the ratio of the minority-to-majority frequencies. The default value (1) means that all other levels are sampled up to have the same frequency as the most occurring level. A value of 0.5 would mean that the minority levels will have (at most) (approximately) half as many rows than the majority level. |
all_neighbors |
Type of two borderline-SMOTE method. Defaults to FALSE. See details. |
This methods works the same way as smote()
, expect that instead of
generating points around every point of of the minority class each point is
first being classified into the boxes "danger" and "not". For each point the
k nearest neighbors is calculated. If all the neighbors comes from a
different class it is labeled noise and put in to the "not" box. If more then
half of the neighbors comes from a different class it is labeled "danger.
If all_neighbors = FALSE
then points will be generated between nearest
neighbors in its own class. If all_neighbors = TRUE
then points will be
generated between any nearest neighbors. See examples for visualization.
The parameter neighbors
controls the way the new examples are created.
For each currently existing minority class example X new examples will be
created (this is controlled by the parameter over_ratio
as mentioned
above). These examples will be generated by using the information from the
neighbors
nearest neighbor of each example of the minority class.
The parameter neighbors
controls how many of these neighbor are used.
All columns used in this step must be numeric with no missing data.
A data.frame or tibble, depending on type of df
.
Hui Han, Wen-Yuan Wang, and Bing-Huan Mao. Borderline-smote: a new over-sampling method in imbalanced data sets learning. In International Conference on Intelligent Computing, pages 878–887. Springer, 2005.
step_bsmote()
for step function of this method
Other Direct Implementations:
adasyn()
,
nearmiss()
,
smote()
,
smotenc()
,
tomek()
circle_numeric <- circle_example[, c("x", "y", "class")] res <- bsmote(circle_numeric, var = "class") res <- bsmote(circle_numeric, var = "class", k = 10) res <- bsmote(circle_numeric, var = "class", over_ratio = 0.8) res <- bsmote(circle_numeric, var = "class", all_neighbors = TRUE)
circle_numeric <- circle_example[, c("x", "y", "class")] res <- bsmote(circle_numeric, var = "class") res <- bsmote(circle_numeric, var = "class", k = 10) res <- bsmote(circle_numeric, var = "class", over_ratio = 0.8) res <- bsmote(circle_numeric, var = "class", all_neighbors = TRUE)
A random dataset with two classes one of which is inside a circle. Used for examples to show how the different methods handles borders.
circle_example
circle_example
A data frame with 200 rows and 4 variables:
Numeric.
Numeric.
Factor, values "Circle" and "Rest".
character, ID variable.
Generates synthetic positive instances using nearmiss algorithm.
nearmiss(df, var, k = 5, under_ratio = 1)
nearmiss(df, var, k = 5, under_ratio = 1)
df |
data.frame or tibble. Must have 1 factor variable and remaining numeric variables. |
var |
Character, name of variable containing factor variable. |
k |
An integer. Number of nearest neighbor that are used to generate the new examples of the minority class. |
under_ratio |
A numeric value for the ratio of the majority-to-minority frequencies. The default value (1) means that all other levels are sampled down to have the same frequency as the least occurring level. A value of 2 would mean that the majority levels will have (at most) (approximately) twice as many rows than the minority level. |
All columns used in this function must be numeric with no missing data.
A data.frame or tibble, depending on type of df
.
Inderjeet Mani and I Zhang. knn approach to unbalanced data distributions: a case study involving information extraction. In Proceedings of workshop on learning from imbalanced datasets, 2003.
step_nearmiss()
for step function of this method
Other Direct Implementations:
adasyn()
,
bsmote()
,
smote()
,
smotenc()
,
tomek()
circle_numeric <- circle_example[, c("x", "y", "class")] res <- nearmiss(circle_numeric, var = "class") res <- nearmiss(circle_numeric, var = "class", k = 10) res <- nearmiss(circle_numeric, var = "class", under_ratio = 1.5)
circle_numeric <- circle_example[, c("x", "y", "class")] res <- nearmiss(circle_numeric, var = "class") res <- nearmiss(circle_numeric, var = "class", k = 10) res <- nearmiss(circle_numeric, var = "class", under_ratio = 1.5)
SMOTE generates new examples of the minority class using nearest neighbors of these cases.
smote(df, var, k = 5, over_ratio = 1)
smote(df, var, k = 5, over_ratio = 1)
df |
data.frame or tibble. Must have 1 factor variable and remaining numeric variables. |
var |
Character, name of variable containing factor variable. |
k |
An integer. Number of nearest neighbor that are used to generate the new examples of the minority class. |
over_ratio |
A numeric value for the ratio of the minority-to-majority frequencies. The default value (1) means that all other levels are sampled up to have the same frequency as the most occurring level. A value of 0.5 would mean that the minority levels will have (at most) (approximately) half as many rows than the majority level. |
The parameter neighbors
controls the way the new examples are created.
For each currently existing minority class example X new examples will be
created (this is controlled by the parameter over_ratio
as mentioned
above). These examples will be generated by using the information from the
neighbors
nearest neighbor of each example of the minority class.
The parameter neighbors
controls how many of these neighbor are used.
All columns used in this function must be numeric with no missing data.
A data.frame or tibble, depending on type of df
.
Chawla, N. V., Bowyer, K. W., Hall, L. O., and Kegelmeyer, W. P. (2002). Smote: Synthetic minority over-sampling technique. Journal of Artificial Intelligence Research, 16:321-357.
step_smote()
for step function of this method
Other Direct Implementations:
adasyn()
,
bsmote()
,
nearmiss()
,
smotenc()
,
tomek()
circle_numeric <- circle_example[, c("x", "y", "class")] res <- smote(circle_numeric, var = "class") res <- smote(circle_numeric, var = "class", k = 10) res <- smote(circle_numeric, var = "class", over_ratio = 0.8)
circle_numeric <- circle_example[, c("x", "y", "class")] res <- smote(circle_numeric, var = "class") res <- smote(circle_numeric, var = "class", k = 10) res <- smote(circle_numeric, var = "class", over_ratio = 0.8)
SMOTENC generates new examples of the minority class using nearest neighbors of these cases, and can handle categorical variables
smotenc(df, var, k = 5, over_ratio = 1)
smotenc(df, var, k = 5, over_ratio = 1)
df |
data.frame or tibble. Must have 1 factor variable and remaining numeric variables. |
var |
Character, name of variable containing factor variable. |
k |
An integer. Number of nearest neighbor that are used to generate the new examples of the minority class. |
over_ratio |
A numeric value for the ratio of the minority-to-majority frequencies. The default value (1) means that all other levels are sampled up to have the same frequency as the most occurring level. A value of 0.5 would mean that the minority levels will have (at most) (approximately) half as many rows than the majority level. |
The parameter neighbors
controls the way the new examples are created.
For each currently existing minority class example X new examples will be
created (this is controlled by the parameter over_ratio
as mentioned
above). These examples will be generated by using the information from the
neighbors
nearest neighbor of each example of the minority class.
The parameter neighbors
controls how many of these neighbor are used.
Columns can be numeric and categorical with no missing data.
A data.frame or tibble, depending on type of df
.
Chawla, N. V., Bowyer, K. W., Hall, L. O., and Kegelmeyer, W. P. (2002). Smote: Synthetic minority over-sampling technique. Journal of Artificial Intelligence Research, 16:321-357.
step_smotenc()
for step function of this method
Other Direct Implementations:
adasyn()
,
bsmote()
,
nearmiss()
,
smote()
,
tomek()
circle_numeric <- circle_example[, c("x", "y", "class")] res <- smotenc(circle_numeric, var = "class") res <- smotenc(circle_numeric, var = "class", k = 10) res <- smotenc(circle_numeric, var = "class", over_ratio = 0.8)
circle_numeric <- circle_example[, c("x", "y", "class")] res <- smotenc(circle_numeric, var = "class") res <- smotenc(circle_numeric, var = "class", k = 10) res <- smotenc(circle_numeric, var = "class", over_ratio = 0.8)
step_adasyn()
creates a specification of a recipe step that generates
synthetic positive instances using ADASYN algorithm.
step_adasyn( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("adasyn") )
step_adasyn( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("adasyn") )
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variable is used to sample the data. See |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
column |
A character string of the variable name that will
be populated (eventually) by the |
over_ratio |
A numeric value for the ratio of the minority-to-majority frequencies. The default value (1) means that all other levels are sampled up to have the same frequency as the most occurring level. A value of 0.5 would mean that the minority levels will have (at most) (approximately) half as many rows than the majority level. |
neighbors |
An integer. Number of nearest neighbor that are used to generate the new examples of the minority class. |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
seed |
An integer that will be used as the seed when applied. |
id |
A character string that is unique to this step to identify it. |
All columns in the data are sampled and returned by juice()
and bake()
.
All columns used in this step must be numeric with no missing data.
When used in modeling, users should strongly consider using the
option skip = TRUE
so that the extra sampling is not
conducted outside of the training set.
An updated version of recipe
with the new step
added to the sequence of existing steps (if any). For the
tidy
method, a tibble with columns terms
which is
the variable used to sample.
When you tidy()
this step, a tibble is retruned with
columns terms
and id
:
character, the selectors or variables selected
character, id of this step
This step has 2 tuning parameters:
over_ratio
: Over-Sampling Ratio (type: double, default: 1)
neighbors
: # Nearest Neighbors (type: integer, default: 5)
The underlying operation does not allow for case weights.
He, H., Bai, Y., Garcia, E. and Li, S. 2008. ADASYN: Adaptive synthetic sampling approach for imbalanced learning. Proceedings of IJCNN 2008. (IEEE World Congress on Computational Intelligence). IEEE International Joint Conference. pp.1322-1328.
adasyn()
for direct implementation
Other Steps for over-sampling:
step_bsmote()
,
step_rose()
,
step_smote()
,
step_smotenc()
,
step_upsample()
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_adasyn(class, over_ratio = 0.4523) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without ADASYN") recipe(class ~ x + y, data = circle_example) %>% step_adasyn(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With ADASYN")
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_adasyn(class, over_ratio = 0.4523) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without ADASYN") recipe(class ~ x + y, data = circle_example) %>% step_adasyn(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With ADASYN")
step_bsmote()
creates a specification of a recipe step that generate new
examples of the minority class using nearest neighbors of these cases in the
border region between classes.
step_bsmote( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, all_neighbors = FALSE, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("bsmote") )
step_bsmote( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, all_neighbors = FALSE, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("bsmote") )
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variable is used to sample the data. See |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
column |
A character string of the variable name that will
be populated (eventually) by the |
over_ratio |
A numeric value for the ratio of the minority-to-majority frequencies. The default value (1) means that all other levels are sampled up to have the same frequency as the most occurring level. A value of 0.5 would mean that the minority levels will have (at most) (approximately) half as many rows than the majority level. |
neighbors |
An integer. Number of nearest neighbor that are used to generate the new examples of the minority class. |
all_neighbors |
Type of two borderline-SMOTE method. Defaults to FALSE. See details. |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
seed |
An integer that will be used as the seed when smote-ing. |
id |
A character string that is unique to this step to identify it. |
This methods works the same way as step_smote()
, expect that instead of
generating points around every point of of the minority class each point is
first being classified into the boxes "danger" and "not". For each point the
k nearest neighbors is calculated. If all the neighbors comes from a
different class it is labeled noise and put in to the "not" box. If more then
half of the neighbors comes from a different class it is labeled "danger.
If all_neighbors = FALSE then points will be generated between nearest neighbors in its own class. If all_neighbors = TRUE then points will be generated between any nearest neighbors. See examples for visualization.
The parameter neighbors
controls the way the new examples are created.
For each currently existing minority class example X new examples will be
created (this is controlled by the parameter over_ratio
as mentioned
above). These examples will be generated by using the information from the
neighbors
nearest neighbor of each example of the minority class.
The parameter neighbors
controls how many of these neighbor are used.
All columns in the data are sampled and returned by juice()
and bake()
.
All columns used in this step must be numeric with no missing data.
When used in modeling, users should strongly consider using the
option skip = TRUE
so that the extra sampling is not
conducted outside of the training set.
An updated version of recipe
with the new step
added to the sequence of existing steps (if any). For the
tidy
method, a tibble with columns terms
which is
the variable used to sample.
When you tidy()
this step, a tibble is retruned with
columns terms
and id
:
character, the selectors or variables selected
character, id of this step
This step has 3 tuning parameters:
over_ratio
: Over-Sampling Ratio (type: double, default: 1)
neighbors
: # Nearest Neighbors (type: integer, default: 5)
all_neighbors
: All Neighbors (type: logical, default: FALSE)
The underlying operation does not allow for case weights.
Hui Han, Wen-Yuan Wang, and Bing-Huan Mao. Borderline-smote: a new over-sampling method in imbalanced data sets learning. In International Conference on Intelligent Computing, pages 878–887. Springer, 2005.
bsmote()
for direct implementation
Other Steps for over-sampling:
step_adasyn()
,
step_rose()
,
step_smote()
,
step_smotenc()
,
step_upsample()
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_bsmote(class, over_ratio = 0.4523) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without SMOTE") recipe(class ~ x + y, data = circle_example) %>% step_bsmote(class, all_neighbors = FALSE) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With borderline-SMOTE, all_neighbors = FALSE") recipe(class ~ x + y, data = circle_example) %>% step_bsmote(class, all_neighbors = TRUE) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With borderline-SMOTE, all_neighbors = TRUE")
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_bsmote(class, over_ratio = 0.4523) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without SMOTE") recipe(class ~ x + y, data = circle_example) %>% step_bsmote(class, all_neighbors = FALSE) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With borderline-SMOTE, all_neighbors = FALSE") recipe(class ~ x + y, data = circle_example) %>% step_bsmote(class, all_neighbors = TRUE) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With borderline-SMOTE, all_neighbors = TRUE")
step_downsample()
creates a specification of a recipe step that will
remove rows of a data set to make the occurrence of levels in a specific
factor level equal.
step_downsample( recipe, ..., under_ratio = 1, ratio = deprecated(), role = NA, trained = FALSE, column = NULL, target = NA, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("downsample") )
step_downsample( recipe, ..., under_ratio = 1, ratio = deprecated(), role = NA, trained = FALSE, column = NULL, target = NA, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("downsample") )
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variable is used to sample the data. See |
under_ratio |
A numeric value for the ratio of the majority-to-minority frequencies. The default value (1) means that all other levels are sampled down to have the same frequency as the least occurring level. A value of 2 would mean that the majority levels will have (at most) (approximately) twice as many rows than the minority level. |
ratio |
Deprecated argument; same as |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
column |
A character string of the variable name that will
be populated (eventually) by the |
target |
An integer that will be used to subsample. This
should not be set by the user and will be populated by |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
seed |
An integer that will be used as the seed when downsampling. |
id |
A character string that is unique to this step to identify it. |
Down-sampling is intended to be performed on the training set
alone. For this reason, the default is skip = TRUE
.
If there are missing values in the factor variable that is used to define the sampling, missing data are selected at random in the same way that the other factor levels are sampled. Missing values are not used to determine the amount of data in the minority level
For any data with factor levels occurring with the same frequency as the minority level, all data will be retained.
All columns in the data are sampled and returned by juice()
and bake()
.
Keep in mind that the location of down-sampling in the step may have effects. For example, if centering and scaling, it is not clear whether those operations should be conducted before or after rows are removed.
An updated version of recipe
with the new step
added to the sequence of existing steps (if any). For the
tidy
method, a tibble with columns terms
which is
the variable used to sample.
When you tidy()
this step, a tibble is retruned with
columns terms
and id
:
character, the selectors or variables selected
character, id of this step
This step has 1 tuning parameters:
under_ratio
: Under-Sampling Ratio (type: double, default: 1)
This step performs an unsupervised operation that can utilize case weights.
To use them, see the documentation in recipes::case_weights and the examples on
tidymodels.org
.
Other Steps for under-sampling:
step_nearmiss()
,
step_tomek()
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the majority levels down to about 1000 each # 1000/259 is approx 3.862 step_downsample(class, under_ratio = 3.862) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without downsample") recipe(class ~ x + y, data = circle_example) %>% step_downsample(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With downsample")
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the majority levels down to about 1000 each # 1000/259 is approx 3.862 step_downsample(class, under_ratio = 3.862) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without downsample") recipe(class ~ x + y, data = circle_example) %>% step_downsample(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With downsample")
step_nearmiss()
creates a specification of a recipe step that removes
majority class instances by undersampling points in the majority class based
on their distance to other points in the same class.
step_nearmiss( recipe, ..., role = NA, trained = FALSE, column = NULL, under_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("nearmiss") )
step_nearmiss( recipe, ..., role = NA, trained = FALSE, column = NULL, under_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("nearmiss") )
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variable is used to sample the data. See |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
column |
A character string of the variable name that will
be populated (eventually) by the |
under_ratio |
A numeric value for the ratio of the majority-to-minority frequencies. The default value (1) means that all other levels are sampled down to have the same frequency as the least occurring level. A value of 2 would mean that the majority levels will have (at most) (approximately) twice as many rows than the minority level. |
neighbors |
An integer. Number of nearest neighbor that are used to generate the new examples of the minority class. |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
seed |
An integer that will be used as the seed when applied. |
id |
A character string that is unique to this step to identify it. |
This method retains the points from the majority class which have the smallest mean distance to the k nearest points in the minority class.
All columns in the data are sampled and returned by juice()
and bake()
.
All columns used in this step must be numeric with no missing data.
When used in modeling, users should strongly consider using the
option skip = TRUE
so that the extra sampling is not
conducted outside of the training set.
An updated version of recipe
with the new step
added to the sequence of existing steps (if any). For the
tidy
method, a tibble with columns terms
which is
the variable used to sample.
When you tidy()
this step, a tibble is retruned with
columns terms
and id
:
character, the selectors or variables selected
character, id of this step
This step has 2 tuning parameters:
under_ratio
: Under-Sampling Ratio (type: double, default: 1)
neighbors
: # Nearest Neighbors (type: integer, default: 5)
The underlying operation does not allow for case weights.
Inderjeet Mani and I Zhang. knn approach to unbalanced data distributions: a case study involving information extraction. In Proceedings of workshop on learning from imbalanced datasets, 2003.
nearmiss()
for direct implementation
Other Steps for under-sampling:
step_downsample()
,
step_tomek()
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the majority levels down to about 1000 each # 1000/259 is approx 3.862 step_nearmiss(class, under_ratio = 3.862) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without NEARMISS") + xlim(c(1, 15)) + ylim(c(1, 15)) recipe(class ~ x + y, data = circle_example) %>% step_nearmiss(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With NEARMISS") + xlim(c(1, 15)) + ylim(c(1, 15))
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the majority levels down to about 1000 each # 1000/259 is approx 3.862 step_nearmiss(class, under_ratio = 3.862) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without NEARMISS") + xlim(c(1, 15)) + ylim(c(1, 15)) recipe(class ~ x + y, data = circle_example) %>% step_nearmiss(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With NEARMISS") + xlim(c(1, 15)) + ylim(c(1, 15))
step_rose()
creates a specification of a recipe step that generates
sample of synthetic data by enlarging the features space of minority and
majority class example. Using ROSE::ROSE()
.
step_rose( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, minority_prop = 0.5, minority_smoothness = 1, majority_smoothness = 1, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("rose") )
step_rose( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, minority_prop = 0.5, minority_smoothness = 1, majority_smoothness = 1, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("rose") )
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variable is used to sample the data. See |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
column |
A character string of the variable name that will
be populated (eventually) by the |
over_ratio |
A numeric value for the ratio of the minority-to-majority frequencies. The default value (1) means that all other levels are sampled up to have the same frequency as the most occurring level. A value of 0.5 would mean that the minority levels will have (at most) (approximately) half as many rows than the majority level. |
minority_prop |
A numeric. Determines the of over-sampling of the minority class. Defaults to 0.5. |
minority_smoothness |
A numeric. Shrink factor to be multiplied by the smoothing parameters to estimate the conditional kernel density of the minority class. Defaults to 1. |
majority_smoothness |
A numeric. Shrink factor to be multiplied by the smoothing parameters to estimate the conditional kernel density of the majority class. Defaults to 1. |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
seed |
An integer that will be used as the seed when rose-ing. |
id |
A character string that is unique to this step to identify it. |
The factor variable used to balance around must only have 2 levels.
The ROSE algorithm works by selecting an observation belonging to class k and generates new examples in its neighborhood is determined by some matrix H_k. Smaller values of these arguments have the effect of shrinking the entries of the corresponding smoothing matrix H_k, Shrinking would be a cautious choice if there is a concern that excessively large neighborhoods could lead to blur the boundaries between the regions of the feature space associated with each class.
All columns in the data are sampled and returned by juice()
and bake()
.
When used in modeling, users should strongly consider using the
option skip = TRUE
so that the extra sampling is not
conducted outside of the training set.
An updated version of recipe
with the new step
added to the sequence of existing steps (if any). For the
tidy
method, a tibble with columns terms
which is
the variable used to sample.
When you tidy()
this step, a tibble is retruned with
columns terms
and id
:
character, the selectors or variables selected
character, id of this step
This step has 1 tuning parameters:
over_ratio
: Over-Sampling Ratio (type: double, default: 1)
The underlying operation does not allow for case weights.
Lunardon, N., Menardi, G., and Torelli, N. (2014). ROSE: a Package for Binary Imbalanced Learning. R Jorunal, 6:82–92.
Menardi, G. and Torelli, N. (2014). Training and assessing classification rules with imbalanced data. Data Mining and Knowledge Discovery, 28:92–122.
Other Steps for over-sampling:
step_adasyn()
,
step_bsmote()
,
step_smote()
,
step_smotenc()
,
step_upsample()
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% mutate(class = factor(class == "VF", labels = c("not VF", "VF"))) %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% step_rose(class) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without ROSE") recipe(class ~ x + y, data = circle_example) %>% step_rose(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With ROSE")
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% mutate(class = factor(class == "VF", labels = c("not VF", "VF"))) %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% step_rose(class) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without ROSE") recipe(class ~ x + y, data = circle_example) %>% step_rose(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With ROSE")
step_smote()
creates a specification of a recipe step that generate new
examples of the minority class using nearest neighbors of these cases.
step_smote( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("smote") )
step_smote( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("smote") )
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variable is used to sample the data. See |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
column |
A character string of the variable name that will
be populated (eventually) by the |
over_ratio |
A numeric value for the ratio of the minority-to-majority frequencies. The default value (1) means that all other levels are sampled up to have the same frequency as the most occurring level. A value of 0.5 would mean that the minority levels will have (at most) (approximately) half as many rows than the majority level. |
neighbors |
An integer. Number of nearest neighbor that are used to generate the new examples of the minority class. |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
seed |
An integer that will be used as the seed when smote-ing. |
id |
A character string that is unique to this step to identify it. |
The parameter neighbors
controls the way the new examples are created.
For each currently existing minority class example X new examples will be
created (this is controlled by the parameter over_ratio
as mentioned
above). These examples will be generated by using the information from the
neighbors
nearest neighbor of each example of the minority class.
The parameter neighbors
controls how many of these neighbor are used.
All columns in the data are sampled and returned by juice()
and bake()
.
All columns used in this step must be numeric with no missing data.
When used in modeling, users should strongly consider using the
option skip = TRUE
so that the extra sampling is not
conducted outside of the training set.
An updated version of recipe
with the new step
added to the sequence of existing steps (if any). For the
tidy
method, a tibble with columns terms
which is
the variable used to sample.
When you tidy()
this step, a tibble is retruned with
columns terms
and id
:
character, the selectors or variables selected
character, id of this step
This step has 2 tuning parameters:
over_ratio
: Over-Sampling Ratio (type: double, default: 1)
neighbors
: # Nearest Neighbors (type: integer, default: 5)
The underlying operation does not allow for case weights.
Chawla, N. V., Bowyer, K. W., Hall, L. O., and Kegelmeyer, W. P. (2002). Smote: Synthetic minority over-sampling technique. Journal of Artificial Intelligence Research, 16:321-357.
smote()
for direct implementation
Other Steps for over-sampling:
step_adasyn()
,
step_bsmote()
,
step_rose()
,
step_smotenc()
,
step_upsample()
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_smote(class, over_ratio = 0.4523) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without SMOTE") recipe(class ~ x + y, data = circle_example) %>% step_smote(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With SMOTE")
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_smote(class, over_ratio = 0.4523) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without SMOTE") recipe(class ~ x + y, data = circle_example) %>% step_smote(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With SMOTE")
step_smotenc()
creates a specification of a recipe step that generate new
examples of the minority class using nearest neighbors of these cases.
Gower's distance is used to handle mixed data types. For categorical
variables, the most common category along neighbors is chosen.
step_smotenc( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("smotenc") )
step_smotenc( recipe, ..., role = NA, trained = FALSE, column = NULL, over_ratio = 1, neighbors = 5, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("smotenc") )
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variable is used to sample the data. See |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
column |
A character string of the variable name that will
be populated (eventually) by the |
over_ratio |
A numeric value for the ratio of the minority-to-majority frequencies. The default value (1) means that all other levels are sampled up to have the same frequency as the most occurring level. A value of 0.5 would mean that the minority levels will have (at most) (approximately) half as many rows than the majority level. |
neighbors |
An integer. Number of nearest neighbor that are used to generate the new examples of the minority class. |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
seed |
An integer that will be used as the seed when smote-ing. |
id |
A character string that is unique to this step to identify it. |
The parameter neighbors
controls the way the new examples are created.
For each currently existing minority class example X new examples will be
created (this is controlled by the parameter over_ratio
as mentioned
above). These examples will be generated by using the information from the
neighbors
nearest neighbor of each example of the minority class.
The parameter neighbors
controls how many of these neighbor are used.
All columns in the data are sampled and returned by juice()
and bake()
.
Columns can be numeric and categorical with no missing data.
When used in modeling, users should strongly consider using the
option skip = TRUE
so that the extra sampling is not
conducted outside of the training set.
An updated version of recipe
with the new step
added to the sequence of existing steps (if any). For the
tidy
method, a tibble with columns terms
which is
the variable used to sample.
When you tidy()
this step, a tibble is retruned with
columns terms
and id
:
character, the selectors or variables selected
character, id of this step
This step has 2 tuning parameters:
over_ratio
: Over-Sampling Ratio (type: double, default: 1)
neighbors
: # Nearest Neighbors (type: integer, default: 5)
The underlying operation does not allow for case weights.
Chawla, N. V., Bowyer, K. W., Hall, L. O., and Kegelmeyer, W. P. (2002). Smote: Synthetic minority over-sampling technique. Journal of Artificial Intelligence Research, 16:321-357.
smotenc()
for direct implementation
Other Steps for over-sampling:
step_adasyn()
,
step_bsmote()
,
step_rose()
,
step_smote()
,
step_upsample()
library(recipes) library(modeldata) data(hpc_data) orig <- count(hpc_data, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data) %>% step_impute_knn(all_predictors()) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_smotenc(class, over_ratio = 0.4523) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class")
library(recipes) library(modeldata) data(hpc_data) orig <- count(hpc_data, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data) %>% step_impute_knn(all_predictors()) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_smotenc(class, over_ratio = 0.4523) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class")
step_tomek()
creates a specification of a recipe step that removes
majority class instances of tomek links.
step_tomek( recipe, ..., role = NA, trained = FALSE, column = NULL, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("tomek") )
step_tomek( recipe, ..., role = NA, trained = FALSE, column = NULL, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("tomek") )
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variable is used to sample the data. See |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
column |
A character string of the variable name that will
be populated (eventually) by the |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
seed |
An integer that will be used as the seed when applied. |
id |
A character string that is unique to this step to identify it. |
The factor variable used to balance around must only have 2 levels. All other variables must be numerics with no missing data.
A tomek link is defined as a pair of points from different classes and are each others nearest neighbors.
All columns in the data are sampled and returned by juice()
and bake()
.
When used in modeling, users should strongly consider using the
option skip = TRUE
so that the extra sampling is not
conducted outside of the training set.
An updated version of recipe
with the new step
added to the sequence of existing steps (if any). For the
tidy
method, a tibble with columns terms
which is
the variable used to sample.
When you tidy()
this step, a tibble is retruned with
columns terms
and id
:
character, the selectors or variables selected
character, id of this step
The underlying operation does not allow for case weights.
Tomek. Two modifications of cnn. IEEE Trans. Syst. Man Cybern., 6:769-772, 1976.
tomek()
for direct implementation
Other Steps for under-sampling:
step_downsample()
,
step_nearmiss()
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% step_tomek(class) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without Tomek") + xlim(c(1, 15)) + ylim(c(1, 15)) recipe(class ~ x + y, data = circle_example) %>% step_tomek(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With Tomek") + xlim(c(1, 15)) + ylim(c(1, 15))
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% step_tomek(class) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without Tomek") + xlim(c(1, 15)) + ylim(c(1, 15)) recipe(class ~ x + y, data = circle_example) %>% step_tomek(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_point() + labs(title = "With Tomek") + xlim(c(1, 15)) + ylim(c(1, 15))
step_upsample()
creates a specification of a recipe step that will
replicate rows of a data set to make the occurrence of levels in a specific
factor level equal.
step_upsample( recipe, ..., over_ratio = 1, ratio = deprecated(), role = NA, trained = FALSE, column = NULL, target = NA, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("upsample") )
step_upsample( recipe, ..., over_ratio = 1, ratio = deprecated(), role = NA, trained = FALSE, column = NULL, target = NA, skip = TRUE, seed = sample.int(10^5, 1), id = rand_id("upsample") )
recipe |
A recipe object. The step will be added to the sequence of operations for this recipe. |
... |
One or more selector functions to choose which
variable is used to sample the data. See |
over_ratio |
A numeric value for the ratio of the minority-to-majority frequencies. The default value (1) means that all other levels are sampled up to have the same frequency as the most occurring level. A value of 0.5 would mean that the minority levels will have (at most) (approximately) half as many rows than the majority level. |
ratio |
Deprecated argument; same as |
role |
Not used by this step since no new variables are created. |
trained |
A logical to indicate if the quantities for preprocessing have been estimated. |
column |
A character string of the variable name that will
be populated (eventually) by the |
target |
An integer that will be used to subsample. This
should not be set by the user and will be populated by |
skip |
A logical. Should the step be skipped when the
recipe is baked by |
seed |
An integer that will be used as the seed when upsampling. |
id |
A character string that is unique to this step to identify it. |
Up-sampling is intended to be performed on the training set
alone. For this reason, the default is skip = TRUE
.
If there are missing values in the factor variable that is used to define the sampling, missing data are selected at random in the same way that the other factor levels are sampled. Missing values are not used to determine the amount of data in the majority level (see example below).
For any data with factor levels occurring with the same frequency as the majority level, all data will be retained.
All columns in the data are sampled and returned by juice()
and bake()
.
An updated version of recipe
with the new step
added to the sequence of existing steps (if any). For the
tidy
method, a tibble with columns terms
which is
the variable used to sample.
When you tidy()
this step, a tibble is retruned with
columns terms
and id
:
character, the selectors or variables selected
character, id of this step
This step has 1 tuning parameters:
over_ratio
: Over-Sampling Ratio (type: double, default: 1)
This step performs an unsupervised operation that can utilize case weights.
To use them, see the documentation in recipes::case_weights and the examples on
tidymodels.org
.
Other Steps for over-sampling:
step_adasyn()
,
step_bsmote()
,
step_rose()
,
step_smote()
,
step_smotenc()
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_upsample(class, over_ratio = 0.4523) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without upsample") recipe(class ~ x + y, data = circle_example) %>% step_upsample(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_jitter(width = 0.1, height = 0.1) + labs(title = "With upsample (with jittering)")
library(recipes) library(modeldata) data(hpc_data) hpc_data0 <- hpc_data %>% select(-protocol, -day) orig <- count(hpc_data0, class, name = "orig") orig up_rec <- recipe(class ~ ., data = hpc_data0) %>% # Bring the minority levels up to about 1000 each # 1000/2211 is approx 0.4523 step_upsample(class, over_ratio = 0.4523) %>% prep() training <- up_rec %>% bake(new_data = NULL) %>% count(class, name = "training") training # Since `skip` defaults to TRUE, baking the step has no effect baked <- up_rec %>% bake(new_data = hpc_data0) %>% count(class, name = "baked") baked # Note that if the original data contained more rows than the # target n (= ratio * majority_n), the data are left alone: orig %>% left_join(training, by = "class") %>% left_join(baked, by = "class") library(ggplot2) ggplot(circle_example, aes(x, y, color = class)) + geom_point() + labs(title = "Without upsample") recipe(class ~ x + y, data = circle_example) %>% step_upsample(class) %>% prep() %>% bake(new_data = NULL) %>% ggplot(aes(x, y, color = class)) + geom_jitter(width = 0.1, height = 0.1) + labs(title = "With upsample (with jittering)")
Removed observations that are part of tomek links.
tomek(df, var)
tomek(df, var)
df |
data.frame or tibble. Must have 1 factor variable and remaining numeric variables. |
var |
Character, name of variable containing factor variable. |
All columns used in this function must be numeric with no missing data.
A data.frame or tibble, depending on type of df
.
Tomek. Two modifications of cnn. IEEE Trans. Syst. Man Cybern., 6:769-772, 1976.
step_tomek()
for step function of this method
Other Direct Implementations:
adasyn()
,
bsmote()
,
nearmiss()
,
smote()
,
smotenc()
circle_numeric <- circle_example[, c("x", "y", "class")] res <- tomek(circle_numeric, var = "class")
circle_numeric <- circle_example[, c("x", "y", "class")] res <- tomek(circle_numeric, var = "class")