-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsmote_impl.R
109 lines (97 loc) · 3.86 KB
/
smote_impl.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#' SMOTE Algorithm
#'
#' SMOTE generates new examples of the minority class using nearest neighbors
#' of these cases.
#'
#' @inheritParams step_smote
#' @param df data.frame or tibble. Must have 1 factor variable and remaining
#' numeric variables.
#' @param var Character, name of variable containing factor variable.
#' @param k An integer. Number of nearest neighbor that are used
#' to generate the new examples of the minority class.
#'
#' @return A data.frame or tibble, depending on type of `df`.
#' @export
#'
#' @details
#' 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.
#'
#' @references 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.
#'
#' @seealso [step_smote()] for step function of this method
#' @family Direct Implementations
#'
#' @examples
#' 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)
smote <- function(df, var, k = 5, over_ratio = 1) {
check_data_frame(df)
check_var(var, df)
check_number_whole(k, min = 1)
check_number_decimal(over_ratio)
predictors <- setdiff(colnames(df), var)
check_numeric(df[, predictors])
check_na(select(df, -all_of(var)))
smote_impl(df, var, k, over_ratio)
}
smote_impl <- function(df, var, k, over_ratio, call = caller_env()) {
data <- split(df, df[[var]])
majority_count <- max(table(df[[var]]))
ratio_target <- majority_count * over_ratio
which_upsample <- which(table(df[[var]]) < ratio_target)
samples_needed <- ratio_target - table(df[[var]])[which_upsample]
min_names <- names(samples_needed)
out_dfs <- list()
for (i in seq_along(samples_needed)) {
minority_df <- data[[min_names[i]]]
minority <- as.matrix(minority_df[names(minority_df) != var])
if (nrow(minority) <= k) {
cli::cli_abort("Not enough observations of {.val {min_names[i]}} to perform SMOTE.", call = call)
}
synthetic <- smote_data(minority, k = k, n_samples = samples_needed[i])
out_df <- as.data.frame(synthetic)
names(out_df) <- setdiff(names(df), var)
out_df_nrow <- min(nrow(out_df), 1)
out_df[var] <- data[[names(samples_needed)[i]]][[var]][out_df_nrow]
out_df <- out_df[names(df)]
out_dfs[[i]] <- out_df
}
final <- rbind(df, do.call(rbind, out_dfs))
final[[var]] <- factor(final[[var]], levels = levels(df[[var]]))
rownames(final) <- NULL
final
}
smote_data <- function(data, k, n_samples, smote_ids = seq_len(nrow(data))) {
ids <- RANN::nn2(data, k = k + 1, searchtype = "priority")$nn.idx
indexes <- rep(sample(smote_ids), length.out = n_samples)
index_len <- tabulate(indexes, NROW(data))
out <- matrix(0, nrow = n_samples, ncol = ncol(data))
sampleids <- sample.int(k, n_samples, TRUE)
runif_ids <- stats::runif(n_samples)
iii <- 0
for (row_num in smote_ids) {
index_selection <- iii + seq_len(index_len[row_num])
# removes itself as nearest neighbour
id_knn <- ids[row_num, ids[row_num, ] != row_num]
dif <- data[id_knn[sampleids[index_selection]], ] -
data[rep(row_num, index_len[row_num]), ]
gap <- dif * runif_ids[index_selection]
out[index_selection, ] <- data[rep(row_num, index_len[row_num]), ] + gap
iii <- iii + index_len[row_num]
}
out
}