Working with imbalanced datasets

Imbalance classification problem

Let:

  • S = {(x1, y1), …(xm, ym)} be our training data for a classification problem, where yi ∈ {0, 1} will be our data labels. Therefore, we will have a binary classification problem.
  • S+ = {(x, y) ∈ S : y = 1} be the positive or minority instances.
  • S = {(x, y) ∈ S : y = −1} be the negative or majority instances.

If |S+| > |S|, the performance of classification algorithms is highly hindered, especially when it comes to the positive class. Therefore, methods to improve that performance are required.

Namely, imbalance package provides oversampling algorithms. Those family of procedures aim to generate a set E of synthetic positive instances based on the training ones, so that we have a new classification problem with + = S+ ∪ E,  = S and  = + ∪  our new training set.

Contents of the package

In the package, we have the following oversampling functions available:

  • mwmote
  • racog
  • wracog
  • rwo
  • pdfos

Each of these functions can be applied to a binary dataset (that is, a set of data where labels y could only take two possible values). In particular, the following examples will use datasets included in the package, which are imbalanced datasets. For example, we can run pdfos algorithm on newthyroid1 dataset.

First of all we could check the shape of the dataset:

library("imbalance")
data(newthyroid1)

head(newthyroid1)
##   T3resin Thyroxin Triiodothyronine Thyroidstimulating TSH_value    Class
## 1     105      7.3              1.5                1.5      -0.1 negative
## 2      67     23.3              7.4                1.8      -0.6 positive
## 3     111      8.4              1.5                0.8       1.2 negative
## 4      89     14.3              4.1                0.5       0.2 positive
## 5     105      9.5              1.8                1.6       3.6 negative
## 6     110     20.3              3.7                0.6       0.2 positive

Clearly, Class is the class attribute of the dataset and there are two possible classes: positive and negative. How many instances do we need to balance the dataset? We could easily compute this by doing:

numPositive <- length(which(newthyroid1$Class == "positive"))
numNegative <- length(which(newthyroid1$Class == "negative"))
nInstances <-  numNegative - numPositive

We get that we need to generate 145 instances to balance the dataset. It would not be advisable such a high number of instances, due to the scarcity of minority examples required to infer data structure. We could try to generate 80 synthetic examples instead:

newSamples <- pdfos(dataset = newthyroid1, numInstances = 80, 
                    classAttr = "Class")

newSamples would contain the 80 synthetic examples, with same shape as the original dataset newthyroid1.

All of the algorithms can be used with the minimal parameters dataset, numInstances and classAttr, except for wRACOG, which does not have a numInstances parameter. The latter adjusts this number itself, and needs two datasets (more accurately, two partitions of the same dataset), train and validation to work.

The package also includes a method to plot a visual comparison between the oversampled dataset and the old imbalanced dataset:

# Bind a balanced dataset
newDataset <- rbind(newthyroid1, newSamples)
# Plot a visual comparison between new and old dataset
plotComparison(newthyroid1, newDataset, 
               attrs = names(newthyroid1)[1:3], classAttr = "Class")

There is also a filtering algorithm available, neater, to cleanse synthetic instances. This algorithm could be used with every oversampling method, either included in this package or in another one:

filteredSamples <- neater(newthyroid1, newSamples, iterations = 500)
## [1] "21 samples filtered by NEATER"
filteredNewDataset <- rbind(newthyroid1, filteredSamples)
plotComparison(newthyroid1, filteredNewDataset, 
               attrs = names(newthyroid1)[1:3])

Oversampling

MWMOTE [1]

SMOTE is a classic algorithm which generates new examples by filling empty areas among the positive instances. It updates the training set iteratively, by performing:

E := E ∪ {x + r(y − x)},  x, y ∈ S+, r ∼ N(0, 1)

It has a major setback though: it does not detect noisy instances. Therefore it can generate synthetic examples out of noisy ones or even between two minority classes, which if not cleansed up, may end up becoming noise inside a majority class cluster.

SMOTE generating noise

SMOTE generating noise

MWMOTE (Majority Weighted Minority Oversampling Technique) tries to overcome both problems. It intends to give higher weight to borderline instances, undersize minority cluster instances and examples near the borderline of the two clases.

Let us recall the header of the method:

mwmote(dataset, numInstances, kNoisy, kMajority, kMinority,
       threshold, cmax, cclustering, classAttr)

A KNN algorithm will be used, where we call d(x, y) the euclidean distance between x and y. Let NNk(x) ⊆ S be the k-neighbourhood of x among the whole trainning set (the k closest instances with euclidean distance). Let NN+k(x) ⊆ S+ be its k minority neighbourhood and NNk(x) ⊆ S be its k majority neighbourhood.

For ease of notation, we will name k1:=KNoisy, k2:=KMajority, k3:=KMinority, α:=threshold, C:=clust, Cclust:=cclustering.

We define Iα, C(x, y) = Cf(x, y) ⋅ Df(x, y), where if x ∉ NN+k3(y) then Iα, Cw(x, y) = 0. Otherwise: $$ f(x) = \left\{\begin{array}{ll} x &, x\le \alpha \\ C & \textrm{otherwise} \end{array}\right.,\qquad C_f(x,y) = \frac{C}{\alpha} \cdot f\left(\frac{d}{d(x,y)}\right) $$

Cf measures the closeness to y, that is, it will measure the proximity of borderline instances.

$D_f(x,y) = \frac{C_f(x,y)}{\sum_{z\in V} C_f(z,y)}$ will represent a density factor so an instance belonging to a compact cluster will have higher Cf(z, y) than another one belonging to a more sparse one.

Let $T_{clust}:= C_{clust} \cdot \frac{1}{|S_f^{+}|} \sum_{x\in S_f^{+}} \underset{y\in S_f^{+}, y\neq x}{min} d(x,y)$. We will also use a mean-average agglomerative hierarchical clustering of the minority instances with threshold Tclust, that is, we will use a mean distance: $$dist(L_i, L_j) = \frac{1}{|L_i||L_j|} \sum_{x\in L_i} \sum_{y\in L_j} d(x,y)$$ and having started with a cluster per instance, we will proceed by joining nearest clusters until minimum of distances is lower than Tclust.

A general outline of the algorithm is:

  • Firstly, MWMOTE computes a set of filtered positive instances: Sf+, by erasing those instances whose k1-neighborhood does not contain any positive instance.
  • Secondly, it computes the positive boundary of Sf+, that is, U = ∪x ∈ Sf+NNk2(x) and the negative boundary, by doing V = ∪x ∈ UNN+k3(x).
  • For each x ∈ V, it figures out probability of picking x by assigning: P(x) = ∑y ∈ UIα, C(x, y) and normalizing those probabilities.
  • Then, it estimates L1, …, LM clusters of S+, with the aforementioned jerarquical agglomerative clustering algorithm and threshold Tclust.
  • Generate numInstances examples by iteratively picking x ∈ V with respect to probability P(x), and updating E := E ∪ {x + r(y − x)}, where y ∈ Lk is uniformly picked and Lk is the cluster containing x.

A few interesting considerations:

  • Low k2 is required in order to ensure we do not pick too many negative instances in U.
  • For an opposite reason, a high k3 must be selected to ensure we pick as many positive hard-to-learn borderline examples as we can.
  • The higher the Cclust parameter, the less and more-populated clusters we will get.

RACOG and wRACOG [2]

These set of algorithms assume we want to approximate a discrete distribution P(W1, …, Wd).

Computing that distribution can be too expensive, because we have to compute: |{Feasible values for W1}|⋯|{Feasible values forWd}| total values.

We are going to approximate P(W1, …, Wd) as $\prod_{i=1}^d P(W_i \mid W_{n(i)})$ where n(i) ∈ {1, …, d}. Chow-Liu’s algorithm will be used to meet that purpose. This algorithm minimizes Kullback-Leibler distance between two distributions: DKL(P ∥ Q) = ∑iP(i)(log P(i) − log Q(i))

We recall the definition for the mutual information of two random discrete variables Wi, Wj: $$ I(W_i, W_j) = \sum_{w_1\in W_1} \sum_{w_2\in W_2} p(w_1, w_2) \log\left(\frac{p(w_1,w_2)}{p(w_1) p(w_2)}\right) $$

Let S+ = {xi = (w1(i), …, wd(i))}i = 1m be the unlabeled positive instances. The algorithm to approximate the distribution that will be used is:

  • Compute G′ = (E′, V′), Chow Liu’s dependence tree.
  • If r is the root of the tree, we will define P(Wr|n(r)) := P(Wr).
  • For each (u, v) ∈ E arc in the tree, n(v) := u and compute P(Wv|Wn(v)).

A Gibbs Sampling scheme would later be used to extract samples with respect to the approximated probability distribution, where a badge of new instances is obtained by performing:

  • Given a minority sample xk = (w1(i), …wd(i)).
  • Iteratively construct for each attribute k(i) ∼ P(Wk ∣ 1(i), …, k − 1(i), wk + 1(i)…, wd(i)).
  • Return S = {i = (1(i), …d(i))}i = 1m.
Markov chain generated by Gibbs Sampler

Markov chain generated by Gibbs Sampler

Let us recall the headers of racog and wracog functions:

racog(dataset, numInstances, burnin, lag, classAttr)
wracog(train, validation, wrapper, slideWin, 
       threshold, classAttr, ...)

RACOG

RACOG (Rapidly Converging Gibbs) iteratively builds badges of synthetic instances using minority given ones. But it rules out first burnin generated badges and from that moment onwards, it picks a badge of newly-generated examples each lag iterations.

wRACOG

The downside of RACOG is that it clearly depends on burnin, lag and the requested number of instances numInstances. wRACOG (wrapper-based RACOG) tries to overcome that problem. Let wrapper be a classifier, that could be declared as it follows:

myWrapper <- structure(list(), class = "C50Wrapper")
trainWrapper.C50Wrapper <- function(wrapper, train, trainClass){
  C50::C5.0(train, trainClass)
}

That is, a wrapper should be an S3 class with a method trainWrapper following the generic method:

trainWrapper(wrapper, train, trainClass, ...)

Furthermore, the result of trainWrapper must be a predict callable S3 class.

Another example of wrapper with a knn (which can get a little tricky, since it is a lazy classificator):

library("FNN")
myWrapper <- structure(list(), class = "KNNWrapper")

predict.KNN <- function(model, test){
  FNN::knn(model$train, test, model$trainClass)
}

trainWrapper.KNNWrapper <- function(wrapper, train, trainClass){
  myKNN <- structure(list(), class = "KNN")
  myKNN$train <- train
  myKNN$trainClass <- trainClass
  myKNN
}

where train is the unlabeled tranining dataset, and trainClass are the labels for the training set.

An example of call for this dataset may consist in splitting haberman dataset (provided by the package) into train and validation, and calling wracog with both partitions and any of the aforementioned wrappers:

data(haberman)

trainFold <- sample(1:nrow(haberman), nrow(haberman)/2, FALSE)
newSamples <- wracog(haberman[trainFold, ], haberman[-trainFold, ],
                     myWrapper, classAttr = "Class")
head(newSamples)
##   Age Year Positive    Class
## 1  54   61        5 positive
## 2  47   63        0 positive
## 3  49   65        0 positive
## 4  39   65        0 positive
## 5  72   58        0 positive
## 6  45   60        1 positive

RWO [3]

RWO (Random Walk Oversampling) generates synthetic instances so that mean and deviation of numerical attributes remain as close as possible to the original ones. This algorithm is motivated by the central limit theorem.

Central limit theorem

Let W1, …, Wm be a collection of independent and identically distributed random variables, with 𝔼(Wi) = μ and Var(Wi) = σ2 < ∞. Hence: $$ \lim_{m} P\left[\frac{\sqrt{m}}{\sigma} \left(\underbrace{\frac{1}{m}\sum_{i=1}^m W_i}_{\overline{W}} - \mu \right) \le z \right] = \phi(z) $$

where ϕ is the distribution function of N(0, 1).

That is, $\frac{\overline{W} - \mu}{\sigma/\sqrt{m}} \rightarrow N(0,1)$ probability-wise.

Let S+ = {xi = (w1(i), …wd(i))}i = 1m be the minority instances. Now, let’s fix some j ∈ {1, …d}, and let’s assume that j-ith column follows a numerical random variable Wj, with mean μj and standard deviation σj < ∞. Let’s compute $\sigma_j' = \sqrt{\frac{1}{m}\sum_{i=1}^m \left(w_j^{(i)} - \frac{\sum_{i=1}^m w_j^{(i)}}{m} \right)^2}$ the biased estimator for the standard deviation. It can be proven that instances generated with $\bar{w}_j = w_j^{(i)} - \frac{\sigma_j'}{\sqrt{m}}\cdot r, r\sim N(0,1)$ have the same sample mean as the original ones, and their sample variance tends to the original one.

Outline of the algorithm

Our algorithm will proceed as follows:

  • For each numerical attribute j = 1, …, d compute the standard deviation of the column, $\sigma_j' = \sqrt{\frac{1}{m}\sum_{i=1}^m \left(w_j^{(i)} - \frac{\sum_{i=1}^m w_j^{(i)}}{m} \right)^2}$.
  • For a given instance xi = (w1(i), …, wd(i)), for each attribute attribute j, generate:

$$ \bar{w}_j = \left\{\begin{array}{ll} w_j^{(i)} - \frac{\sigma_j'}{\sqrt{m}}\cdot r, r\sim N(0,1) & \textrm{if numerical attribute}\\ \textrm{pick uniformly over } \{w_j^{(1)}, \ldots w_j^{(m)}\} & \textrm{otherwise} \end{array}\right. $$

PDFOS [4]

Motivation

Given a distribution function of a random variable X, namely F(x), if that function has an almost everywhere derivative, then, almost everywhere, it holds: $$ f(x) = \lim_{h\rightarrow 0} \frac{F(x+h) - F(x-h)}{2h} = \lim_{h\rightarrow 0} \frac{P(x-h < X \le x+h)}{2h} $$

Given random samples of X, X1, …Xn, namely x1, …xn, an estimator for f could be the mean of samples in ]x − h, x + h[ divided by the length of the interval: $$ \widehat{f}(x) = \frac{1}{2hn} \bigg[\textrm{Number of samples } x_1, \ldots, x_n \textrm{ that belong to ]x-h, x+h[}\bigg] $$

If we define $\omega(x) = \left\{\begin{array}{ll} \frac{1}{2} &, |x| < 1\\ 0 & \textrm{otherwise} \end{array}\right.$

and $w_h(x) = w\left(\left|\frac{x}{h}\right|\right)$, then we could write as: $$ \widehat{f}(x) = \frac{1}{nh} \sum_{i=1}^n \omega_h(x-x_i) $$

It we assume that x1, …, xn are equidistant with distance 2h (they are placed in the middle of 2h length intervals), could be seen as an histogram where each bar has a 2h width and a $\frac{1}{2nh} \cdot \bigg[[\textrm{Number of samples } x_1, \ldots, x_n \textrm{ that belong to the interval}]\bigg]$ length. Parameter h is called bandwidth.

In multivariate case (d dimensional), we define: $$ \widehat{f}(x) = \frac{1}{nh^d} \sum_{i=1}^n \omega_h(x-x_i) $$

Kernel methods

If we took $w = \frac{1}{2} \Large{1}\normalsize_{]-1,1[}$, then would have jump discontinuities and we would have jump derivatives. On the other hand, we could took ω, where w ≥ 0, Ωω(x)dx = 1, Ω ⊆ X a domain, and w were even, and that way we could have estimators with more desirable properties with respect to continuity and differentiability.

can be evaluated through its MISE (Mean Integral Squared Error): $$ MISE(h) = \underset{x_1, \ldots, x_d}{\mathbb{E}} \int (\widehat{f}(x) - f(x))^2 dx $$

Example of kernel estimation

Example of kernel estimation

Gaussian kernels

PDFOS (Probability Distribution density Function estimation based Oversampling) uses multivariate Gaussian kernel methods. The probability density function of a d-Gaussian distribution with mean 0 and Ψ as its covariance matrix is: $$ \phi^{\Psi}(x) = \frac{1}{\sqrt{(2\pi \cdot det(\Psi))^d}} exp\left(-\frac{1}{2} x \Psi^{-1} x^T \right) $$

Let S+ = {xi = (w1(i), …, wd(i))}i = 1m be the minority instances. The unbiased covariance estimator is: $$ U = \frac{1}{m-1} \sum_{i=1}^m (x_i - \overline{x})(x_i - \overline{x})^T, \qquad \textrm{where } \overline{x} = \frac{1}{m}\sum_{i=1}^m x_i $$

We will use kernel functions $\phi_h(x) = \phi^U\left(\frac{x}{h}\right)$, where h ought to be optimized to minimize the MISE. It is well-known that can be achieved by minimizing the following cross validation function: $$ M(h) = \frac{1}{m^2 h^d} \sum_{i=1}^m \sum_{j=1}^m \phi_h^{\ast} (x_i - x_j) + \frac{2}{m h^d} \phi_h(0) $$ where $\phi_h^{\ast} \approx \phi_{h\sqrt{2}} - 2\phi_h$.

Once a proper h has been found, a suitable generating scheme could be to take xi + hRr, where xi ∈ S+, r ∼ Nd(0, 1) and U = R ⋅ RT. In case we have enough guarantees to decompose U = RT ⋅ R (U must be a positive-definite matrix), we could use Choleski decomposition. In fact, we provide a sketch of proof showing that all covariance matrices are positive-semidefinite: $$ y^T \left(\sum_{i=1}^m (x_i - \overline{x})(x_i - \overline{x})^T\right) y = \sum_{i=1}^m (\underbrace{(x_i - \overline{x})^T y}_{z_i^T})^T \underbrace{(x_i - \overline{x})^T y}_{z_i}) = \sum_{i=1}^m ||z_i||^2\ge 0 $$ for arbitrary y ∈ ℝd. We need a strict positive definite matrix, otherwise PDFOS would not provide a result and will stop its execution.

Search of optimal bandwidth

We take a first approximation to h as the value: $$ h_{Silverman} = \left(\frac{4}{m(d+2)}\right)^{\frac{1}{d+4}} $$ where d is number of attributes and m the size of the minority class.

Reshaping the equation of the cross validation function and differentiating:

And a straightforward gradient descendent algorithm is used to find a good h estimation.

Filtering

Once we have created synthetic examples, we should ask ourselves how many of those instances are in fact relevant to our problem. Filtering algorithms can be applied to oversampled datasets, to erase the least relevant instances.

NEATER [5]

NEATER (filteriNg of ovErsampled dAta using non cooperaTive gamE theoRy) is a filtering algorithm based on game theory.

Introduction to Game Theory

Let (P, T, f) be our game space. We would have a set of players, P = {1, …, n}, and Ti = {1, …, ki}, set of feasible strategies for the i-th player, resulting in T = T1 × … × Tn. We can easily assign a payoff to each player taking into account his/her own strategy as well as other players’ strategy. So f will be given by the following equation: $$ \begin{array}{rll} f: T &\longrightarrow& \mathbb{R}^n\\ t &\longmapsto& (f_1(t), \ldots, f_n(t)) \end{array} $$

ti will denote (t1, …, ti − 1, ti + 1, …, tn) and similarly we can denote fi(ti, ti) = fi(t).

An strategic Nash equilibrium is a tuple (t1, …, tn) where fi(ti, ti) ≥ fi(ti, ti) for every other t′ ∈ T, and all i = 1, …, n. That is, an strategic Nash equilibrium maximizes the payoff for all the players.

The strategy for each player will be picked with respect to a given probability: $$ \delta_i \in \Delta_i = \{(\delta_i^{(1)}, \ldots, \delta_i^{(k_i)}) \in (R^{+}_0)^{k_i} : \sum_{j=1}^{k_i} \delta_i^{(j)} = 1\} $$ We define Δ1 × … × Δn := Δ and we call an element δ = (δ1, …, δn) ∈ Δ an strategy profile. Having a fixed strategy profile δ, the overall payoff for the i-th player is defined as: ui(δ) = ∑(t1, …, tn) ∈ Tδi(ti)fi(t)

Given ui the payoff for a δ strategy profile in the i-th player and δ ∈ Δ we will denote

A probabilistic Nash equilibrium is a strategy profile x = (δ1, …, δn) verifying ui(δi, δi) ≥ ui(δi, δi) for every other δ′ ∈ Δ, and all i = 1, …, n.

A theorem ensures that every game space (P, T, f) with finite players and strategies has a probabistic Nash equilibrium

Particularization to imbalance problem

Let S be the original training set, E the synthetic generated instances. Our players would be S ∪ E. Every player would be able to pick between two different strategies: 0 - being a negative instance - and 1 - being a positive instance -. Players of S would always have a fixed strategy, where the i-th player would have δi = (0, 1) (a 0 strategy) in case it is a negative instance or δi = (1, 0) (a 1 strategy) otherwise.

The payoff for a given instance is affected only by its own strategy and its k nearest neighbors in S ∪ E. That is, for every xi ∈ E, we will have ui(δ) = ∑j ∈ NNk(x)(xiTwijxj) where wij = g(d(xi, xj)) and g is a decreasing function (the further, the lower payoff). In our implementation, we have considered $g(z) = \frac{1}{1+z^2}$, with d the euclidean distance.

Each step should involve an update to the strategy profiles of instances of E. Namely, if xi ∈ E, the following equation will be used:

That is, we are reinforcing the strategy that is producing the higher payoff, in detriment to the opposite strategy. This method has enough convergence guarantees.

Let’s recall the header for neater:

neater(dataset, newSamples, k, iterations, smoothFactor, classAttr)

Then a rough sketch of the algorithm is:

  • Compute k nearest neighbors for every instance of E:=newSamples.
  • Initialize strategy profiles of datasetnewSamples.
  • Iterate iterations times updating payoffs with the aforementioned rule and strategy profiles.
  • Keep only those examples of newSamples with probability of being positive instances higher than 0.5.

References

[1]
Barua, S., Islam, Md. M., Yao, X. and Murase, K. (2014). MWMOTE–majority weighted minority oversampling technique for imbalanced data set learning. IEEE Transactions on Knowledge and Data Engineering 26 405–25.
[2]
Das, B., Krishnan, N. C. and Cook, D. J. (2015). RACOG and wRACOG: Two probabilistic oversampling techniques. IEEE Transactions on Knowledge and Data Engineering 27 222–34.
[3]
Zhang, H. and Li, M. (2014). RWO-sampling: A random walk over-sampling approach to imbalanced data classification. Information Fusion 20 99–116.
[4]
Gao, M., Hong, X., Chen, S., Harris, C. J. and Khalaf, E. (2014). PDFOS: PDF estimation based over-sampling for imbalanced two-class problems. Neurocomputing 138 248–59.
[5]
Almogahed, B. A. and Kakadiaris, I. A. (2014). NEATER: Filtering of over-sampled data using non-cooperative game theory. Soft Computing 19 3301–22.