Python imblearn.over_sampling.RandomOverSampler() Examples

The following are 8 code examples of imblearn.over_sampling.RandomOverSampler(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module imblearn.over_sampling , or try the search function .
Example #1
Source File: transformers.py    From healthcareai-py with MIT License 6 votes vote down vote up
def transform(self, X, y=None):
        # TODO how do we validate this happens before train/test split? Or do we need to? Can we implement it in the
        # TODO      simple trainer in the correct order and leave this to advanced users?

        # Extract predicted column
        y = np.squeeze(X[[self.predicted_column]])

        # Copy the dataframe without the predicted column
        temp_dataframe = X.drop([self.predicted_column], axis=1)

        # Initialize and fit the under sampler
        over_sampler = RandomOverSampler(random_state=self.random_seed)
        x_over_sampled, y_over_sampled = over_sampler.fit_sample(temp_dataframe, y)

        # Build the resulting under sampled dataframe
        result = pd.DataFrame(x_over_sampled)

        # Restore the column names
        result.columns = temp_dataframe.columns

        # Restore the y values
        y_over_sampled = pd.Series(y_over_sampled)
        result[self.predicted_column] = y_over_sampled

        return result 
Example #2
Source File: sampler_factory.py    From yelp with GNU Lesser General Public License v2.1 6 votes vote down vote up
def create_sampler(sampler_name, random_state=None):

    if sampler_name is None or sampler_name == 'None':
        return None
    if sampler_name.lower() == 'randomundersampler':
        return RandomUnderSampler(random_state=random_state)
    if sampler_name.lower() == 'tomeklinks':
        return TomekLinks(random_state=random_state)
    if sampler_name.lower() == 'enn':
        return EditedNearestNeighbours(random_state=random_state)
    if sampler_name.lower() == 'ncl':
        return NeighbourhoodCleaningRule(random_state=random_state)
    if sampler_name.lower() == 'randomoversampler':
        return RandomOverSampler(random_state=random_state)
    if sampler_name.lower() == 'smote':
        return SMOTE(random_state=random_state)
    if sampler_name.lower() == 'smotetomek':
        return SMOTETomek(random_state=random_state)
    if sampler_name.lower() == 'smoteenn':
        return SMOTEENN(random_state=random_state)
    else:
        raise ValueError('Unsupported value \'%s\' for sampler' % sampler_name) 
Example #3
Source File: test_kmeans_smote.py    From kmeans_smote with MIT License 6 votes vote down vote up
def test_random_oversampling_limit_case(plot=False):
    """Execute k-means SMOTE with parameters equivalent to random oversampling"""
    kmeans_smote = KMeansSMOTE(
        random_state=RND_SEED,
        imbalance_ratio_threshold=float('Inf'),
        kmeans_args={
            'n_clusters': 1
        },
        smote_args={
            'k_neighbors': 0
        }
    )
    random_oversampler = RandomOverSampler(random_state=RND_SEED)
    X_resampled, y_resampled = kmeans_smote.fit_sample(X, Y)
    X_resampled_random_oversampler, y_resampled_random_oversampler = random_oversampler.fit_sample(
        X, Y)

    if plot:
        plot_resampled(X, X_resampled, Y, y_resampled,
                       'random_oversampling_limit_case_test_kmeans_smote')
        plot_resampled(X, X_resampled_random_oversampler, Y, y_resampled_random_oversampler,
                       'random_oversampling_limit_case_test_random_oversampling')

    assert_array_equal(X_resampled, X_resampled_random_oversampler)
    assert_array_equal(y_resampled, y_resampled_random_oversampler) 
Example #4
Source File: DataBalance.py    From FAE with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        super(UpSampling, self).__init__(RandomOverSampler(random_state=RANDOM_SEED[BALANCE_UP_SAMPLING]),
                                         BALANCE_UP_SAMPLING) 
Example #5
Source File: random_over_sampler.py    From lale with Apache License 2.0 5 votes vote down vote up
def __init__(self, operator = None, sampling_strategy='auto', random_state=None):
        if operator is None:
            raise ValueError("Operator is a required argument.")

        self._hyperparams = {
            'sampling_strategy': sampling_strategy,
            'random_state': random_state}

        resampler_instance = OrigModel(**self._hyperparams)
        super(RandomOverSamplerImpl, self).__init__(
            operator = operator,
            resampler = resampler_instance) 
Example #6
Source File: imblearn_resampling_example.py    From hyperparameter_hunter with MIT License 5 votes vote down vote up
def over_sample_random(train_inputs, train_targets):
    sampler = RandomOverSampler(random_state=32)
    train_inputs, train_targets = _sampler_helper(sampler, train_inputs, train_targets)
    return train_inputs, train_targets 
Example #7
Source File: test_imbalance.py    From pandas-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_objectmapper_oversampling(self):
        import imblearn.over_sampling as os
        df = pdml.ModelFrame([])
        self.assertIs(df.imbalance.over_sampling.ADASYN,
                      os.ADASYN)
        self.assertIs(df.imbalance.over_sampling.RandomOverSampler,
                      os.RandomOverSampler)
        self.assertIs(df.imbalance.over_sampling.SMOTE,
                      os.SMOTE) 
Example #8
Source File: random.py    From Auto-PyTorch with Apache License 2.0 5 votes vote down vote up
def resample(self, X, y, target_size_strategy, seed):
        from imblearn.over_sampling import RandomOverSampler as imblearn_RandomOverSampler
        resampler = imblearn_RandomOverSampler(sampling_strategy=target_size_strategy, random_state=seed)
        return resampler.fit_resample(X, y)