本文整理汇总了Python中imblearn.under_sampling.ClusterCentroids.fit_resample方法的典型用法代码示例。如果您正苦于以下问题:Python ClusterCentroids.fit_resample方法的具体用法?Python ClusterCentroids.fit_resample怎么用?Python ClusterCentroids.fit_resample使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类imblearn.under_sampling.ClusterCentroids
的用法示例。
在下文中一共展示了ClusterCentroids.fit_resample方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_fit_resample_check_voting
# 需要导入模块: from imblearn.under_sampling import ClusterCentroids [as 别名]
# 或者: from imblearn.under_sampling.ClusterCentroids import fit_resample [as 别名]
def test_fit_resample_check_voting():
cc = ClusterCentroids(random_state=RND_SEED)
cc.fit_resample(X, Y)
assert cc.voting_ == 'soft'
cc = ClusterCentroids(random_state=RND_SEED)
cc.fit_resample(sparse.csr_matrix(X), Y)
assert cc.voting_ == 'hard'
示例2: test_fit_resample_error
# 需要导入模块: from imblearn.under_sampling import ClusterCentroids [as 别名]
# 或者: from imblearn.under_sampling.ClusterCentroids import fit_resample [as 别名]
def test_fit_resample_error():
sampling_strategy = 'auto'
cluster = 'rnd'
cc = ClusterCentroids(
sampling_strategy=sampling_strategy,
random_state=RND_SEED,
estimator=cluster)
with raises(ValueError, match="has to be a KMeans clustering"):
cc.fit_resample(X, Y)
voting = 'unknown'
cc = ClusterCentroids(
sampling_strategy=sampling_strategy,
voting=voting,
random_state=RND_SEED)
with raises(ValueError, match="needs to be one of"):
cc.fit_resample(X, Y)
示例3: test_multiclass_fit_resample
# 需要导入模块: from imblearn.under_sampling import ClusterCentroids [as 别名]
# 或者: from imblearn.under_sampling.ClusterCentroids import fit_resample [as 别名]
def test_multiclass_fit_resample():
y = Y.copy()
y[5] = 2
y[6] = 2
cc = ClusterCentroids(random_state=RND_SEED)
X_resampled, y_resampled = cc.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
示例4: test_fit_resample_auto
# 需要导入模块: from imblearn.under_sampling import ClusterCentroids [as 别名]
# 或者: from imblearn.under_sampling.ClusterCentroids import fit_resample [as 别名]
def test_fit_resample_auto():
sampling_strategy = 'auto'
cc = ClusterCentroids(
sampling_strategy=sampling_strategy, random_state=RND_SEED)
X_resampled, y_resampled = cc.fit_resample(X, Y)
X_gt = np.array([[0.92923648, 0.76103773], [0.47104475, 0.44386323],
[0.13347175, 0.12167502], [0.06738818, -0.529627],
[0.17901516, 0.69860992], [0.094035, -2.55298982]])
y_gt = np.array([0, 0, 0, 1, 1, 1])
assert_allclose(X_resampled, X_gt, rtol=R_TOL)
assert_array_equal(y_resampled, y_gt)
示例5: test_fit_resample_half
# 需要导入模块: from imblearn.under_sampling import ClusterCentroids [as 别名]
# 或者: from imblearn.under_sampling.ClusterCentroids import fit_resample [as 别名]
def test_fit_resample_half():
sampling_strategy = {0: 3, 1: 6}
cc = ClusterCentroids(
sampling_strategy=sampling_strategy, random_state=RND_SEED)
X_resampled, y_resampled = cc.fit_resample(X, Y)
X_gt = np.array([[0.92923648, 0.76103773], [0.13347175, 0.12167502], [
0.47104475, 0.44386323
], [0.09125309, -0.85409574], [0.19220316, 0.32337101],
[0.094035, -2.55298982], [0.20792588, 1.49407907],
[0.04352327, -0.20515826], [0.12372842, 0.6536186]])
y_gt = np.array([0, 0, 0, 1, 1, 1, 1, 1, 1])
print(X_resampled)
assert_allclose(X_resampled, X_gt, rtol=R_TOL)
assert_array_equal(y_resampled, y_gt)
示例6: test_fit_hard_voting
# 需要导入模块: from imblearn.under_sampling import ClusterCentroids [as 别名]
# 或者: from imblearn.under_sampling.ClusterCentroids import fit_resample [as 别名]
def test_fit_hard_voting():
sampling_strategy = 'auto'
voting = 'hard'
cluster = KMeans(random_state=RND_SEED)
cc = ClusterCentroids(
sampling_strategy=sampling_strategy,
random_state=RND_SEED,
estimator=cluster,
voting=voting)
X_resampled, y_resampled = cc.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.094035, -2.55298982]])
y_gt = np.array([0, 0, 0, 1, 1, 1])
assert_allclose(X_resampled, X_gt, rtol=R_TOL)
assert_array_equal(y_resampled, y_gt)
for x in X_resampled:
assert np.any(np.all(x == X, axis=1))
示例7: print
# 需要导入模块: from imblearn.under_sampling import ClusterCentroids [as 别名]
# 或者: from imblearn.under_sampling.ClusterCentroids 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=50, 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 Cluster Centroids
cc = ClusterCentroids()
X_resampled, y_resampled = cc.fit_resample(X, y)
X_res_vis_soft = pca.transform(X_resampled)
# Use hard voting instead of soft voting
cc = ClusterCentroids(voting='hard')
X_resampled, y_resampled = cc.fit_resample(X, y)
X_res_vis_hard = pca.transform(X_resampled)
# Two subplots, unpack the axes array immediately
f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5))
c0 = ax1.scatter(X_vis[y == 0, 0], X_vis[y == 0, 1], label="Class #0",
alpha=0.5)
c1 = ax1.scatter(X_vis[y == 1, 0], X_vis[y == 1, 1], label="Class #1",
alpha=0.5)
ax1.set_title('Original set')