當前位置: 首頁>>代碼示例>>Python>>正文


Python ClusterCentroids.fit_resample方法代碼示例

本文整理匯總了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'
開發者ID:bodycat,項目名稱:imbalanced-learn,代碼行數:9,代碼來源:test_cluster_centroids.py

示例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)
開發者ID:bodycat,項目名稱:imbalanced-learn,代碼行數:19,代碼來源:test_cluster_centroids.py

示例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
開發者ID:bodycat,項目名稱:imbalanced-learn,代碼行數:12,代碼來源:test_cluster_centroids.py

示例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)
開發者ID:bodycat,項目名稱:imbalanced-learn,代碼行數:13,代碼來源:test_cluster_centroids.py

示例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)
開發者ID:bodycat,項目名稱:imbalanced-learn,代碼行數:16,代碼來源:test_cluster_centroids.py

示例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))
開發者ID:bodycat,項目名稱:imbalanced-learn,代碼行數:21,代碼來源:test_cluster_centroids.py

示例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')
開發者ID:bodycat,項目名稱:imbalanced-learn,代碼行數:33,代碼來源:plot_cluster_centroids.py


注:本文中的imblearn.under_sampling.ClusterCentroids.fit_resample方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。