-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[MRG] ENH: Vectorized ADASYN #649
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
Merged
glemaitre
merged 6 commits into
scikit-learn-contrib:master
from
MattEding:adasyn-vectorized
Dec 5, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46f2cb5
vectorized adasyn; fixed adasyn module docstring; todo: update unit t…
MattEding e0b5c94
fix indentation error
MattEding 07175ef
fixed row selection indices; fixed n_samples to work with non-ints
MattEding 85c173d
fixed row & col shape occassional mismatch due to rounding in algorithm
MattEding 902438c
Merge branch 'master' of https://github.com/scikit-learn-contrib/imba…
MattEding ad574ce
update unit tests to reflect random state changes
MattEding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| """Class to perform random over-sampling.""" | ||
| """Class to perform over-sampling using ADASYN.""" | ||
|
|
||
| # Authors: Guillaume Lemaitre <[email protected]> | ||
| # Christos Aridas | ||
|
|
@@ -104,8 +104,8 @@ def _fit_resample(self, X, y): | |
| self._validate_estimator() | ||
| random_state = check_random_state(self.random_state) | ||
|
|
||
| X_resampled = X.copy() | ||
| y_resampled = y.copy() | ||
| X_resampled = [X.copy()] | ||
| y_resampled = [y.copy()] | ||
|
|
||
| for class_sample, n_samples in self.sampling_strategy_.items(): | ||
| if n_samples == 0: | ||
|
|
@@ -114,13 +114,12 @@ def _fit_resample(self, X, y): | |
| X_class = _safe_indexing(X, target_class_indices) | ||
|
|
||
| self.nn_.fit(X) | ||
| _, nn_index = self.nn_.kneighbors(X_class) | ||
| nns = self.nn_.kneighbors(X_class, return_distance=False)[:, 1:] | ||
| # The ratio is computed using a one-vs-rest manner. Using majority | ||
| # in multi-class would lead to slightly different results at the | ||
| # cost of introducing a new parameter. | ||
| ratio_nn = np.sum(y[nn_index[:, 1:]] != class_sample, axis=1) / ( | ||
| self.nn_.n_neighbors - 1 | ||
| ) | ||
| n_neighbors = self.nn_.n_neighbors - 1 | ||
| ratio_nn = np.sum(y[nns] != class_sample, axis=1) / n_neighbors | ||
| if not np.sum(ratio_nn): | ||
| raise RuntimeError( | ||
| "Not any neigbours belong to the majority" | ||
|
|
@@ -131,7 +130,9 @@ def _fit_resample(self, X, y): | |
| ) | ||
| ratio_nn /= np.sum(ratio_nn) | ||
| n_samples_generate = np.rint(ratio_nn * n_samples).astype(int) | ||
| if not np.sum(n_samples_generate): | ||
| # rounding may cause new amount for n_samples | ||
| n_samples = np.sum(n_samples_generate) | ||
| if not n_samples: | ||
| raise ValueError( | ||
| "No samples will be generated with the" | ||
| " provided ratio settings." | ||
|
|
@@ -140,66 +141,30 @@ def _fit_resample(self, X, y): | |
| # the nearest neighbors need to be fitted only on the current class | ||
| # to find the class NN to generate new samples | ||
| self.nn_.fit(X_class) | ||
| _, nn_index = self.nn_.kneighbors(X_class) | ||
| nns = self.nn_.kneighbors(X_class, return_distance=False)[:, 1:] | ||
|
|
||
| if sparse.issparse(X): | ||
| row_indices, col_indices, samples = [], [], [] | ||
| n_samples_generated = 0 | ||
| for x_i, x_i_nn, num_sample_i in zip( | ||
| X_class, nn_index, n_samples_generate | ||
| ): | ||
| if num_sample_i == 0: | ||
| continue | ||
| nn_zs = random_state.randint( | ||
| 1, high=self.nn_.n_neighbors, size=num_sample_i | ||
| ) | ||
| steps = random_state.uniform(size=len(nn_zs)) | ||
| if x_i.nnz: | ||
| for step, nn_z in zip(steps, nn_zs): | ||
| sample = x_i + step * ( | ||
| X_class[x_i_nn[nn_z], :] - x_i | ||
| ) | ||
| row_indices += [n_samples_generated] * len( | ||
| sample.indices | ||
| ) | ||
| col_indices += sample.indices.tolist() | ||
| samples += sample.data.tolist() | ||
| n_samples_generated += 1 | ||
| X_new = sparse.csr_matrix( | ||
| (samples, (row_indices, col_indices)), | ||
| [np.sum(n_samples_generate), X.shape[1]], | ||
| dtype=X.dtype, | ||
| ) | ||
| y_new = np.array( | ||
| [class_sample] * np.sum(n_samples_generate), dtype=y.dtype | ||
| ) | ||
| else: | ||
| x_class_gen = [] | ||
| for x_i, x_i_nn, num_sample_i in zip( | ||
| X_class, nn_index, n_samples_generate | ||
| ): | ||
| if num_sample_i == 0: | ||
| continue | ||
| nn_zs = random_state.randint( | ||
| 1, high=self.nn_.n_neighbors, size=num_sample_i | ||
| ) | ||
| steps = random_state.uniform(size=len(nn_zs)) | ||
| x_class_gen.append( | ||
| [ | ||
| x_i + step * (X_class[x_i_nn[nn_z], :] - x_i) | ||
| for step, nn_z in zip(steps, nn_zs) | ||
| ] | ||
| ) | ||
|
|
||
| X_new = np.concatenate(x_class_gen).astype(X.dtype) | ||
| y_new = np.array( | ||
| [class_sample] * np.sum(n_samples_generate), dtype=y.dtype | ||
| ) | ||
| enumerated_class_indices = np.arange(len(target_class_indices)) | ||
| rows = np.repeat(enumerated_class_indices, n_samples_generate) | ||
| cols = random_state.choice(n_neighbors, size=n_samples) | ||
| diffs = X_class[nns[rows, cols]] - X_class[rows] | ||
| steps = random_state.uniform(size=(n_samples, 1)) | ||
|
|
||
| if sparse.issparse(X_new): | ||
| X_resampled = sparse.vstack([X_resampled, X_new]) | ||
| if sparse.issparse(X): | ||
| sparse_func = type(X).__name__ | ||
| steps = getattr(sparse, sparse_func)(steps) | ||
| X_new = X_class[rows] + steps.multiply(diffs) | ||
| else: | ||
| X_resampled = np.vstack((X_resampled, X_new)) | ||
| y_resampled = np.hstack((y_resampled, y_new)) | ||
| X_new = X_class[rows] + steps * diffs | ||
|
|
||
| X_new = X_new.astype(X.dtype) | ||
| y_new = np.full(n_samples, fill_value=class_sample, dtype=y.dtype) | ||
| X_resampled.append(X_new) | ||
| y_resampled.append(y_new) | ||
|
|
||
| if sparse.issparse(X): | ||
| X_resampled = sparse.vstack(X_resampled, format=X.format) | ||
| else: | ||
| X_resampled = np.vstack(X_resampled) | ||
| y_resampled = np.hstack(y_resampled) | ||
|
|
||
| return X_resampled, y_resampled | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,7 +98,7 @@ def _make_samples( | |
| """ | ||
| random_state = check_random_state(self.random_state) | ||
| samples_indices = random_state.randint( | ||
| low=0, high=len(nn_num.flatten()), size=n_samples | ||
| low=0, high=nn_num.size, size=n_samples | ||
|
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. avoid making copy of array |
||
| ) | ||
|
|
||
| # np.newaxis for backwards compatability with random_state | ||
|
|
@@ -731,13 +731,12 @@ def _fit_resample(self, X, y): | |
| X_resampled.append(X_new) | ||
| y_resampled.append(y_new) | ||
|
|
||
| if sparse.issparse(X_new): | ||
| if sparse.issparse(X): | ||
|
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. changed in case of some edge case where the number of samples produced is 0 and thus X_new would cause an UnboundLocalError |
||
| X_resampled = sparse.vstack(X_resampled, format=X.format) | ||
| else: | ||
| X_resampled = np.vstack(X_resampled) | ||
| y_resampled = np.hstack(y_resampled) | ||
|
|
||
|
|
||
| return X_resampled, y_resampled | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Original non-vectorized implementation used this approach to make n-samples rather than truncating
np.sum(n_samples_generate)down ton_samples