本文整理汇总了Python中imblearn.under_sampling.RandomUnderSampler.fit_resample方法的典型用法代码示例。如果您正苦于以下问题:Python RandomUnderSampler.fit_resample方法的具体用法?Python RandomUnderSampler.fit_resample怎么用?Python RandomUnderSampler.fit_resample使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类imblearn.under_sampling.RandomUnderSampler
的用法示例。
在下文中一共展示了RandomUnderSampler.fit_resample方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_random_under_sampling_heterogeneous_data
# 需要导入模块: from imblearn.under_sampling import RandomUnderSampler [as 别名]
# 或者: from imblearn.under_sampling.RandomUnderSampler import fit_resample [as 别名]
def test_random_under_sampling_heterogeneous_data():
X_hetero = np.array([['xxx', 1, 1.0], ['yyy', 2, 2.0], ['zzz', 3, 3.0]],
dtype=np.object)
y = np.array([0, 0, 1])
rus = RandomUnderSampler(random_state=RND_SEED)
X_res, y_res = rus.fit_resample(X_hetero, y)
assert X_res.shape[0] == 2
assert y_res.shape[0] == 2
assert X_res.dtype == object
示例2: test_multiclass_fit_resample
# 需要导入模块: from imblearn.under_sampling import RandomUnderSampler [as 别名]
# 或者: from imblearn.under_sampling.RandomUnderSampler import fit_resample [as 别名]
def test_multiclass_fit_resample():
y = Y.copy()
y[5] = 2
y[6] = 2
rus = RandomUnderSampler(random_state=RND_SEED)
X_resampled, y_resampled = rus.fit_resample(X, y)
count_y_res = Counter(y_resampled)
assert count_y_res[0] == 2
assert count_y_res[1] == 2
assert count_y_res[2] == 2
示例3: test_rus_fit_resample
# 需要导入模块: from imblearn.under_sampling import RandomUnderSampler [as 别名]
# 或者: from imblearn.under_sampling.RandomUnderSampler import fit_resample [as 别名]
def test_rus_fit_resample():
rus = RandomUnderSampler(random_state=RND_SEED, replacement=True)
X_resampled, y_resampled = rus.fit_resample(X, Y)
X_gt = np.array([[0.92923648, 0.76103773], [0.47104475, 0.44386323],
[0.13347175, 0.12167502], [0.09125309, -0.85409574],
[0.12372842, 0.6536186], [0.04352327, -0.20515826]])
y_gt = np.array([0, 0, 0, 1, 1, 1])
assert_array_equal(X_resampled, X_gt)
assert_array_equal(y_resampled, y_gt)
示例4: test_pipeline_sample
# 需要导入模块: from imblearn.under_sampling import RandomUnderSampler [as 别名]
# 或者: from imblearn.under_sampling.RandomUnderSampler import fit_resample [as 别名]
def test_pipeline_sample():
# Test whether pipeline works with a sampler at the end.
# Also test pipeline.sampler
X, y = make_classification(
n_classes=2,
class_sep=2,
weights=[0.1, 0.9],
n_informative=3,
n_redundant=1,
flip_y=0,
n_features=20,
n_clusters_per_class=1,
n_samples=5000,
random_state=0)
rus = RandomUnderSampler(random_state=0)
pipeline = Pipeline([('rus', rus)])
# test transform and fit_transform:
X_trans, y_trans = pipeline.fit_resample(X, y)
X_trans2, y_trans2 = rus.fit_resample(X, y)
assert_allclose(X_trans, X_trans2, rtol=R_TOL)
assert_allclose(y_trans, y_trans2, rtol=R_TOL)
pca = PCA()
pipeline = Pipeline([('pca', PCA()), ('rus', rus)])
X_trans, y_trans = pipeline.fit_resample(X, y)
X_pca = pca.fit_transform(X)
X_trans2, y_trans2 = rus.fit_resample(X_pca, y)
# We round the value near to zero. It seems that PCA has some issue
# with that
X_trans[np.bitwise_and(X_trans < R_TOL, X_trans > -R_TOL)] = 0
X_trans2[np.bitwise_and(X_trans2 < R_TOL, X_trans2 > -R_TOL)] = 0
assert_allclose(X_trans, X_trans2, rtol=R_TOL)
assert_allclose(y_trans, y_trans2, rtol=R_TOL)
示例5: test_rus_fit_resample_half
# 需要导入模块: from imblearn.under_sampling import RandomUnderSampler [as 别名]
# 或者: from imblearn.under_sampling.RandomUnderSampler import fit_resample [as 别名]
def test_rus_fit_resample_half():
sampling_strategy = {0: 3, 1: 6}
rus = RandomUnderSampler(
sampling_strategy=sampling_strategy,
random_state=RND_SEED,
replacement=True)
X_resampled, y_resampled = rus.fit_resample(X, Y)
X_gt = np.array([[0.92923648, 0.76103773], [0.47104475, 0.44386323], [
0.92923648, 0.76103773
], [0.15490546, 0.3130677], [0.15490546, 0.3130677],
[0.15490546, 0.3130677], [0.20792588, 1.49407907],
[0.15490546, 0.3130677], [0.12372842, 0.6536186]])
y_gt = np.array([0, 0, 0, 1, 1, 1, 1, 1, 1])
assert_array_equal(X_resampled, X_gt)
assert_array_equal(y_resampled, y_gt)
示例6: print
# 需要导入模块: from imblearn.under_sampling import RandomUnderSampler [as 别名]
# 或者: from imblearn.under_sampling.RandomUnderSampler import fit_resample [as 别名]
print(__doc__)
# Generate the dataset
X, y = make_classification(n_classes=2, class_sep=2, weights=[0.1, 0.9],
n_informative=3, n_redundant=1, flip_y=0,
n_features=20, n_clusters_per_class=1,
n_samples=200, random_state=10)
# Instanciate a PCA object for the sake of easy visualisation
pca = PCA(n_components=2)
# Fit and transform x to visualise inside a 2D feature space
X_vis = pca.fit_transform(X)
# Apply the random under-sampling
rus = RandomUnderSampler(return_indices=True)
X_resampled, y_resampled, idx_resampled = rus.fit_resample(X, y)
X_res_vis = pca.transform(X_resampled)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
idx_samples_removed = np.setdiff1d(np.arange(X_vis.shape[0]),
idx_resampled)
idx_class_0 = y_resampled == 0
plt.scatter(X_res_vis[idx_class_0, 0], X_res_vis[idx_class_0, 1],
alpha=.8, label='Class #0')
plt.scatter(X_res_vis[~idx_class_0, 0], X_res_vis[~idx_class_0, 1],
alpha=.8, label='Class #1')
plt.scatter(X_vis[idx_samples_removed, 0], X_vis[idx_samples_removed, 1],
alpha=.8, label='Removed samples')
示例7: RandomUnderSampler
# 需要导入模块: from imblearn.under_sampling import RandomUnderSampler [as 别名]
# 或者: from imblearn.under_sampling.RandomUnderSampler import fit_resample [as 别名]
#
# ``sampling_strategy`` can be given a ``float``. For **under-sampling
# methods**, it corresponds to the ratio :math:`\\alpha_{us}` defined by
# :math:`N_{rM} = \\alpha_{us} \\times N_{m}` where :math:`N_{rM}` and
# :math:`N_{m}` are the number of samples in the majority class after
# resampling and the number of samples in the minority class, respectively.
# select only 2 classes since the ratio make sense in this case
binary_mask = np.bitwise_or(y == 0, y == 2)
binary_y = y[binary_mask]
binary_X = X[binary_mask]
sampling_strategy = 0.8
rus = RandomUnderSampler(sampling_strategy=sampling_strategy)
X_res, y_res = rus.fit_resample(binary_X, binary_y)
print('Information of the iris data set after making it '
'balanced using a float and an under-sampling method: \n '
'sampling_strategy={} \n y: {}'
.format(sampling_strategy, Counter(y_res)))
plot_pie(y_res)
###############################################################################
# For **over-sampling methods**, it correspond to the ratio
# :math:`\\alpha_{os}` defined by :math:`N_{rm} = \\alpha_{os} \\times N_{M}`
# where :math:`N_{rm}` and :math:`N_{M}` are the number of samples in the
# minority class after resampling and the number of samples in the majority
# class, respectively.
ros = RandomOverSampler(sampling_strategy=sampling_strategy)
X_res, y_res = ros.fit_resample(binary_X, binary_y)
示例8: func
# 需要导入模块: from imblearn.under_sampling import RandomUnderSampler [as 别名]
# 或者: from imblearn.under_sampling.RandomUnderSampler import fit_resample [as 别名]
def func(X, y, sampling_strategy, random_state):
rus = RandomUnderSampler(
sampling_strategy=sampling_strategy, random_state=random_state)
return rus.fit_resample(X, y)