本文整理匯總了Python中imblearn.over_sampling.SMOTE屬性的典型用法代碼示例。如果您正苦於以下問題:Python over_sampling.SMOTE屬性的具體用法?Python over_sampling.SMOTE怎麽用?Python over_sampling.SMOTE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類imblearn.over_sampling
的用法示例。
在下文中一共展示了over_sampling.SMOTE屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create_sampler
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
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)
示例2: test_smote_limit_case
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def test_smote_limit_case(plot=False):
"""Execute k-means SMOTE with parameters equivalent to SMOTE"""
kmeans_smote = KMeansSMOTE(
random_state=RND_SEED,
imbalance_ratio_threshold=float('Inf'),
kmeans_args={
'n_clusters': 1
}
)
smote = SMOTE(random_state=RND_SEED)
X_resampled, y_resampled = kmeans_smote.fit_sample(X, Y)
X_resampled_smote, y_resampled_smote = smote.fit_sample(X, Y)
if plot:
plot_resampled(X, X_resampled, Y, y_resampled,
'smote_limit_case_test_kmeans_smote')
plot_resampled(X, X_resampled_smote, Y, y_resampled_smote,
'smote_limit_case_test_smote')
assert_array_equal(X_resampled, X_resampled_smote)
assert_array_equal(y_resampled, y_resampled_smote)
示例3: test_random_oversampling_limit_case
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
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)
示例4: test_smote_limit_case_multiclass
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def test_smote_limit_case_multiclass(plot=False):
"""Execute k-means SMOTE with parameters equivalent to SMOTE"""
kmeans_smote = KMeansSMOTE(
random_state=RND_SEED,
imbalance_ratio_threshold=float('Inf'),
kmeans_args={
'n_clusters': 1
},
smote_args={'k_neighbors':3}
)
smote = SMOTE(random_state=RND_SEED, k_neighbors=3)
X_resampled, y_resampled = kmeans_smote.fit_sample(X_MULTICLASS, Y_MULTICLASS)
X_resampled_smote, y_resampled_smote = smote.fit_sample(X_MULTICLASS, Y_MULTICLASS)
if plot:
plot_resampled(X_MULTICLASS, X_resampled, Y_MULTICLASS, y_resampled,
'smote_limit_case_multiclass_test_kmeans_smote')
plot_resampled(X_MULTICLASS, X_resampled_smote, Y_MULTICLASS, y_resampled_smote,
'smote_limit_case_multiclass_test_smote')
assert_array_equal(X_resampled, X_resampled_smote)
assert_array_equal(y_resampled, y_resampled_smote)
示例5: test_multiclass_irt_dict
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def test_multiclass_irt_dict(plot=False):
"""
Execute k-means SMOTE for multi-class dataset with
different imbalance ratio thresholds per class.
"""
kmeans_smote = KMeansSMOTE(
random_state=RND_SEED,
kmeans_args={'n_clusters': 10},
imbalance_ratio_threshold={1: 1, 2: np.inf})
X_resampled, y_resampled = kmeans_smote.fit_sample(
X_MULTICLASS, Y_MULTICLASS)
assert (np.unique(y_resampled, return_counts=True)[1]
== np.unique(Y_MULTICLASS_EXPECTED, return_counts=True)[1]).all()
assert (X_resampled.shape == X_MULTICLASS_SHAPE_EXPECTED)
if plot:
plot_resampled(X_MULTICLASS, X_resampled, Y_MULTICLASS,
y_resampled, 'multiclass_test')
示例6: __init__
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def __init__(self, **kwargs):
super(SmoteSampling, self).__init__(SMOTE(**kwargs, random_state=RANDOM_SEED[BALANCE_SMOTE]), BALANCE_SMOTE)
示例7: GetDescription
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def GetDescription(self):
return "To Remove the unbalance of the training data set, we used the Synthetic Minority Oversampling " \
"TEchnique (SMOTE) to make positive/negative samples balance. "
示例8: __init__
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def __init__(self, operator = None, sampling_strategy='auto', random_state=None, k_neighbors=5, n_jobs=1):
if operator is None:
raise ValueError("Operator is a required argument.")
self._hyperparams = {
'sampling_strategy': sampling_strategy,
'random_state': random_state,
'k_neighbors': k_neighbors,
'n_jobs': n_jobs}
resampler_instance = OrigModel(**self._hyperparams)
super(SMOTEImpl, self).__init__(
operator = operator,
resampler = resampler_instance)
示例9: over_sample_smote
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def over_sample_smote(train_inputs, train_targets):
sampler = SMOTE(random_state=32)
train_inputs, train_targets = _sampler_helper(sampler, train_inputs, train_targets)
return train_inputs, train_targets
示例10: test_objectmapper_oversampling
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
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)
示例11: test_sample
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def test_sample(self):
from imblearn.under_sampling import ClusterCentroids, OneSidedSelection
from imblearn.over_sampling import ADASYN, SMOTE
from imblearn.combine import SMOTEENN, SMOTETomek
models = [ClusterCentroids, OneSidedSelection, ADASYN, SMOTE,
SMOTEENN, SMOTETomek]
X = np.random.randn(100, 5)
y = np.array([0, 1]).repeat([80, 20])
df = pdml.ModelFrame(X, target=y, columns=list('ABCDE'))
for model in models:
mod1 = model(random_state=self.random_state)
mod2 = model(random_state=self.random_state)
df.fit(mod1)
mod2.fit(X, y)
result = df.fit_resample(mod1)
expected_X, expected_y = mod2.fit_resample(X, y)
self.assertIsInstance(result, pdml.ModelFrame)
tm.assert_numpy_array_equal(result.target.values, expected_y)
tm.assert_numpy_array_equal(result.data.values, expected_X)
tm.assert_index_equal(result.columns, df.columns)
mod1 = model(random_state=self.random_state)
mod2 = model(random_state=self.random_state)
result = df.fit_sample(mod1)
expected_X, expected_y = mod2.fit_sample(X, y)
self.assertIsInstance(result, pdml.ModelFrame)
tm.assert_numpy_array_equal(result.target.values, expected_y)
tm.assert_numpy_array_equal(result.data.values, expected_X)
tm.assert_index_equal(result.columns, df.columns)
示例12: _validate_smote_args
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def _validate_smote_args(self, smote_args, minority_count):
# determine max number of nearest neighbors considering sample size
max_k_neighbors = minority_count - 1
# check if max_k_neighbors is violated also considering smote's default
smote = SMOTE(**smote_args)
if smote.k_neighbors > max_k_neighbors:
smote_args['k_neighbors'] = max_k_neighbors
smote = SMOTE(**smote_args)
return smote_args
示例13: test_smoke
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def test_smoke(plot=False):
"""Execute k-means SMOTE with default parameters"""
kmeans_smote = KMeansSMOTE(random_state=RND_SEED)
X_resampled, y_resampled = kmeans_smote.fit_sample(X, Y)
assert (np.unique(y_resampled, return_counts=True)[1]
== np.unique(Y_EXPECTED, return_counts=True)[1]).all()
assert (X_resampled.shape == X_SHAPE_EXPECTED)
if plot:
plot_resampled(X, X_resampled, Y, y_resampled, 'smoke_test')
示例14: test_smoke_regular_kmeans
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def test_smoke_regular_kmeans(plot=False):
"""Execute k-means SMOTE with default parameters using regular k-means (not minibatch)"""
kmeans_smote = KMeansSMOTE(
random_state=RND_SEED, use_minibatch_kmeans=False)
X_resampled, y_resampled = kmeans_smote.fit_sample(X, Y)
assert (np.unique(y_resampled, return_counts=True)[1]
== np.unique(Y_EXPECTED, return_counts=True)[1]).all()
assert (X_resampled.shape == X_SHAPE_EXPECTED)
if plot:
plot_resampled(X, X_resampled, Y, y_resampled, 'smoke_test')
示例15: test_smoke_multiclass
# 需要導入模塊: from imblearn import over_sampling [as 別名]
# 或者: from imblearn.over_sampling import SMOTE [as 別名]
def test_smoke_multiclass(plot=False):
"""Execute k-means SMOTE with default parameters for multi-class dataset"""
kmeans_smote = KMeansSMOTE(random_state=RND_SEED)
X_resampled, y_resampled = kmeans_smote.fit_sample(X_MULTICLASS, Y_MULTICLASS)
assert (np.unique(y_resampled, return_counts=True)[1]
== np.unique(Y_MULTICLASS_EXPECTED, return_counts=True)[1]).all()
assert (X_resampled.shape == X_MULTICLASS_SHAPE_EXPECTED)
if plot:
plot_resampled(X_MULTICLASS, X_resampled, Y_MULTICLASS, y_resampled, 'smoke_multiclass_test')