clover.over_sampling
.SOMO¶
-
class
clover.over_sampling.
SOMO
(sampling_strategy='auto', random_state=None, k_neighbors=5, som_estimator=None, distribution_ratio=0.8, raise_error=True, n_jobs=None)[source]¶ Applies the SOM algorithm to the input space before applying SMOTE.
This is an implementation of the algorithm described in [R7e9535891f0a-1].
Read more in the user guide.
Parameters: - sampling_strategy : float, str, dict or callable, default=’auto’
Sampling information to resample the data set.
When
float
, it corresponds to the desired ratio of the number of samples in the minority class over the number of samples in the majority class after resampling. Therefore, the ratio is expressed aswhere
is the number of samples in the minority class after resampling and
is the number of samples in the majority class.
Warning
float
is only available for binary classification. An error is raised for multi-class classification.When
str
, specify the class targeted by the resampling. The number of samples in the different classes will be equalized. Possible choices are:'minority'
: resample only the minority class;'not minority'
: resample all classes but the minority class;'not majority'
: resample all classes but the majority class;'all'
: resample all classes;'auto'
: equivalent to'not majority'
.When
dict
, the keys correspond to the targeted classes. The values correspond to the desired number of samples for each targeted class.When callable, function taking
y
and returns adict
. The keys correspond to the targeted classes. The values correspond to the desired number of samples for each class.
- random_state : int, RandomState instance, default=None
Control the randomization of the algorithm.
- If int,
random_state
is the seed used by the random number generator; - If
RandomState
instance, random_state is the random number generator; - If
None
, the random number generator is theRandomState
instance used bynp.random
.
- If int,
- k_neighbors : int or object, default=5
Defines the number of nearest neighbors to be used by SMOTE.
- If
int
, this number is used to construct synthetic samples. - If
object
, an estimator that inherits fromsklearn.neighbors.base.KNeighborsMixin
that will be used to find the number of nearest neighbors.
- If
- som_estimator : None or object or int or float, default=None
Defines the SOM clusterer applied to the input space.
- If
None
, :class:`` is used which tends to be better with large number of samples. - If KMeans object, then an instance from either
sklearn.cluster.KMeans
orsklearn.cluster.MiniBatchKMeans
. - If
int
, the number of clusters to be used. - If
float
, the proportion of the number of clusters over the number of samples to be used.
- If
- distribution_ratio : float, default=0.8
The ratio of intra-cluster to inter-cluster generated samples. It is a number in the
range. The default value is
0.8
, a number equal to the proportion of intra-cluster generated samples over the total number of generated samples. As the number decreases, less intra-cluster and more inter-cluster samples are generated.- raise_error : boolean, default=True
- n_jobs : int, default=None
Number of CPU cores used during the cross-validation loop.
None
means 1 unless in ajoblib.parallel_backend
context.-1
means using all processors. See Glossary for more details.
References
[R7e9535891f0a-1] Georgios Douzas, Fernando Bacao, “Self-Organizing Map Oversampling (SOMO) for imbalanced data set learning” https://www.sciencedirect.com/science/article/abs/pii/S0957417417302324?via%3Dihub Examples
>>> import numpy as np >>> from clover.over_sampling import SOMO >>> from sklearn.datasets import make_blobs >>> blobs = [100, 800, 100] >>> X, y = make_blobs(blobs, centers=[(-10, 0), (0,0), (10, 0)]) >>> # Add a single 0 sample in the middle blob >>> X = np.concatenate([X, [[0, 0]]]) >>> y = np.append(y, 0) >>> # Make this a binary classification problem >>> y = y == 1 >>> somo = SOMO(random_state=42) >>> X_res, y_res = somo.fit_resample(X, y) >>> # Find the number of new samples in the middle blob >>> n_res_in_middle = ((X_res[:, 0] > -5) & (X_res[:, 0] < 5)).sum() >>> print("Samples in the middle blob: %s" % n_res_in_middle) Samples in the middle blob: 801 >>> print("Middle blob unchanged: %s" % (n_res_in_middle == blobs[1] + 1)) Middle blob unchanged: True >>> print("More 0 samples: %s" % ((y_res == 0).sum() > (y == 0).sum())) More 0 samples: True
Attributes: - clusterer_ : object
A fitted
somlearn.SOM
instance.- distributor_ : object
A fitted
clover.distribution.DensityDistributor
instance.- labels_ : array, shape (n_samples,)
Labels of each sample.
- neighbors_ : array, (n_neighboring_pairs, 2) or None
An array that contains all neighboring pairs with each row being a unique neighboring pair.
- oversampler_ : object
A fitted
imblearn.over_sampling.SMOTE
instance.- random_state_ : object
An instance of
RandomState
class.- sampling_strategy_ : dict
Actual sampling strategy.
-
__init__
(self, sampling_strategy='auto', random_state=None, k_neighbors=5, som_estimator=None, distribution_ratio=0.8, raise_error=True, n_jobs=None)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
fit
(self, X, y)¶ Check inputs and statistics of the sampler.
You should use
fit_resample
in all cases.Parameters: - X : {array-like, dataframe, sparse matrix} of shape (n_samples, n_features)
Data array.
- y : array-like of shape (n_samples,)
Target array.
Returns: - self : object
Return the instance itself.
-
fit_resample
(self, X, y, **fit_params)¶ Resample the dataset.
Parameters: - X : {array-like, dataframe, sparse matrix} of shape (n_samples, n_features)
Matrix containing the data which have to be sampled.
- y : array-like of shape (n_samples,)
Corresponding label for each sample in X.
Returns: - X_resampled : {array-like, dataframe, sparse matrix} of shape (n_samples_new, n_features)
The array containing the resampled data.
- y_resampled : array-like of shape (n_samples_new,)
The corresponding label of X_resampled.
-
fit_sample
(self, X, y)¶ Resample the dataset.
Parameters: - X : {array-like, dataframe, sparse matrix} of shape (n_samples, n_features)
Matrix containing the data which have to be sampled.
- y : array-like of shape (n_samples,)
Corresponding label for each sample in X.
Returns: - X_resampled : {array-like, dataframe, sparse matrix} of shape (n_samples_new, n_features)
The array containing the resampled data.
- y_resampled : array-like of shape (n_samples_new,)
The corresponding label of X_resampled.
-
get_params
(self, deep=True)¶ Get parameters for this estimator.
Parameters: - deep : bool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns: - params : mapping of string to any
Parameter names mapped to their values.
-
set_params
(self, **params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object.Parameters: - **params : dict
Estimator parameters.
Returns: - self : object
Estimator instance.