Skip to content
2 changes: 2 additions & 0 deletions imblearn/over_sampling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ._smote import KMeansSMOTE
from ._smote import SVMSMOTE
from ._smote import SMOTENC
from ._rose import ROSE

__all__ = [
"ADASYN",
Expand All @@ -19,4 +20,5 @@
"BorderlineSMOTE",
"SVMSMOTE",
"SMOTENC",
"ROSE"
]
121 changes: 121 additions & 0 deletions imblearn/over_sampling/_rose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""Class to perform over-sampling using ROSE."""

import numpy as np
from scipy import sparse

from sklearn.utils import check_random_state

from .base import BaseOverSampler
from ..utils._validation import _deprecate_positional_args


class ROSE(BaseOverSampler):

"""Oversample using Random OverSampling Examples (ROSE) algorithm.

Read more in the :ref:`User Guide <rose>`.
Parameters
----------
{sampling_strategy}
{random_state}
shrink_factors : dict of {classes: shrinkfactors} couples, applied to
the gaussian kernels
{n_jobs}

Notes
-----
TODO: Support for multi-class resampling. A one-vs.one scheme is used.
References
----------
.. [1] N. Lunardon, G. Menardi, N.Torelli, "ROSE: A Package for Binary
Imbalanced Learning," R Journal, 6(1), 2014.

.. [2] G Menardi, N. Torelli, "Training and assessing classification
rules with imbalanced data," Data Mining and Knowledge
Discovery, 28(1), pp.92-122, 2014.
Comment on lines +36 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are great to include. References should be referenced from the main docstring for the class. A short summary of the method would also be good.

Here's an example from the BorderlineSMOTE class:

class BorderlineSMOTE(BaseSMOTE):
"""Over-sampling using Borderline SMOTE.
This algorithm is a variant of the original SMOTE algorithm proposed in
[2]_. Borderline samples will be detected and used to generate new
synthetic samples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a better description in the docstring.

I would like to add more complete information on the maths in the docs too, but I'm not familiar with Sphinx. Could you point me to some instructions or metadocumentation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math typesetting should look familiar if you've seen LaTeX before, here's a short guide from sphinx's docs: https://www.sphinx-doc.org/en/1.0/ext/math.html. Syntax is based on reStructuredText, which feels similar to markdown but has a powerful directive system.

Could you point me to some instructions or metadocumentation?

Nothing specific to imblearn. If you want to learn more, sphinx's "Getting Started" guide is a good place to start. (I'd recommend it regardless. Sphinx is used for a huge number of projects, so the skill is extremely transferable).

Our Makefile and conf.py in the docs/ directory are fairly standard. Building local documentation then looks like:

cd docs/ make html xdg-open _build/html/index.html
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I will just have some problem with plots, but I'll manage to add everything to the docs.

I have another issue: failing checks, see below. It's not very clear to me what they do address at, and what to fix.

"""

@_deprecate_positional_args
def __init__(self, *, sampling_strategy="auto", shrink_factors=None,
random_state=None, n_jobs=None):
super().__init__(sampling_strategy=sampling_strategy)
self.random_state = random_state
self.shrink_factors = shrink_factors
self.n_jobs = n_jobs

def _make_samples(self,
X,
class_indices,
n_class_samples,
h_shrink):
""" A support function that returns artificial samples constructed from
FIXME

Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
Observations from which the samples will be created.

class_indices : ndarray, shape (n_class_samples,)
The target class indices

n_class_samples : int
The total number of samples per class to generate

h_shrink : int
the shrink factor

Returns
-------
X_new : {ndarray, sparse matrix}, shape (n_samples, n_features)
Synthetically generated samples.

y_new : ndarray, shape (n_samples,)
Target values for synthetic samples.

"""

# pdb.set_trace()

p = X.shape[1]

random_state = check_random_state(self.random_state)
samples_indices = random_state.choice(
class_indices, size=n_class_samples, replace=True)

h_opt = (4 / ((p + 2) * len(class_indices))) ** (1 / (p + 4))
H_opt = h_shrink * h_opt * np.diagflat(
X[class_indices, :].std(axis=0, ddof=1))

Xrose = np.random.standard_normal(
size=(n_class_samples, p)) @ H_opt + X[samples_indices, :]

return Xrose

def _fit_resample(self, X, y):

#random_state = check_random_state(self.random_state)
X_resampled = np.empty((0, X.shape[1]), dtype=X.dtype)
y_resampled = np.empty((0), dtype=X.dtype)

if self.shrink_factors is None:
self.shrink_factors = {key: 1 for key in self.sampling_strategy_.keys()}

for class_sample, n_samples in self.sampling_strategy_.items():
class_indices = np.flatnonzero(y == class_sample)
n_class_samples = len(class_indices) + n_samples
X_new = self._make_samples(X,
class_indices,
n_class_samples,
self.shrink_factors[class_sample])
y_new = np.array([class_sample] * n_class_samples)

if sparse.issparse(X_new):
X_resampled = sparse.vstack([X_resampled, X_new])
else:
X_resampled = np.vstack((X_resampled, X_new))

y_resampled = np.hstack((y_resampled, y_new))

return X_resampled.astype(X.dtype), y_resampled.astype(y.dtype)

59 changes: 59 additions & 0 deletions imblearn/over_sampling/tests/test_rose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Test the module ROSE."""
# Authors: Andrea Lorenzon <andrelorenzon@gmail.com>
# License: MIT

import numpy as np

from imblearn.over_sampling import ROSE


def test_testunit():
"""Check test unit"""
return True


def test_random_state():
"""Check randomState()"""
assert np.random.RandomState(42)


def test_instance():
"""Check ROSE instantiation"""
rose = ROSE()
assert id(rose)


RND_SEED = 0
X = np.array(
[
[0.11622591, -0.0317206],
[0.77481731, 0.60935141],
[1.25192108, -0.22367336],
[0.53366841, -0.30312976],
[1.52091956, -0.49283504],
[-0.28162401, -2.10400981],
[0.83680821, 1.72827342],
[0.3084254, 0.33299982],
[0.70472253, -0.73309052],
[0.28893132, -0.38761769],
[1.15514042, 0.0129463],
[0.88407872, 0.35454207],
[1.31301027, -0.92648734],
[-1.11515198, -0.93689695],
[-0.18410027, -0.45194484],
[0.9281014, 0.53085498],
[-0.14374509, 0.27370049],
[-0.41635887, -0.38299653],
[0.08711622, 0.93259929],
[1.70580611, -0.11219234],
]
)
Y = np.array([0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0])
R_TOL = 1e-4


def test_rose():
"""Check ROSE use"""
X_res, y_res = ROSE().fit_resample(X, Y)
assert np.unique(Y.all()) == np.unique(y_res.all())
assert X_res.shape[1] == X.shape[1]