- Notifications
You must be signed in to change notification settings - Fork 1.3k
[WIP] Rose #750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Rose #750
Changes from 5 commits
53c7a8d 183c03f 0a3307b 886694f 07731c4 c0d7473 013f7cc 2b34f47 8d0e99e 8bbbd2e 9ac2797 b41b06a d2dd6f4 b31a5c3 d5ca24c b6e95aa 6f7f8e1 c391ec3 93ac868 bdffda3 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 imbalanced-learn/imblearn/over_sampling/_smote.py Lines 219 to 224 in 0acd717
Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? Member There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
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 cd docs/ make html xdg-open _build/html/index.html Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||||||||||||||
andrealorenzon marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||||||||||
| | ||||||||||||||
| 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) | ||||||||||||||
andrealorenzon marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||||||||||
| 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()} | ||||||||||||||
andrealorenzon marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||||||||||
| | ||||||||||||||
| 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) | ||||||||||||||
| | ||||||||||||||
andrealorenzon marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||||||||||
| 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) | ||
andrealorenzon marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| | ||
| 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] | ||
andrealorenzon marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
Uh oh!
There was an error while loading. Please reload this page.