Exemple #1
0
class TestClustererEnsemble(unittest.TestCase):
    def setUp(self):
        self.X, self.y = load_breast_cancer(return_X_y=True)

        n_clusters = 5
        n_estimators = 3

        # Initialize a set of estimators
        estimators = [
            KMeans(n_clusters=n_clusters),
            MiniBatchKMeans(n_clusters=n_clusters),
            AgglomerativeClustering(n_clusters=n_clusters)
        ]

        self.estimator = ClustererEnsemble(estimators, n_clusters=n_clusters)
        self.estimator.fit(self.X)

    def test_parameters(self):
        assert_true(
            hasattr(self.estimator, 'base_estimators')
            and self.estimator.base_estimators is not None)

    def test_scores(self):
        predicted_labels = self.estimator.labels_
        assert_equal(predicted_labels.shape[0], self.X.shape[0])

    def tearDown(self):
        pass
    def setUp(self):
        self.X, self.y = load_breast_cancer(return_X_y=True)

        n_clusters = 5
        n_estimators = 3

        # Initialize a set of estimators
        estimators = [KMeans(n_clusters=n_clusters),
                      MiniBatchKMeans(n_clusters=n_clusters),
                      AgglomerativeClustering(n_clusters=n_clusters)]

        self.estimator = ClustererEnsemble(estimators, n_clusters=n_clusters)
        self.estimator.fit(self.X)
Exemple #3
0
if __name__ == "__main__":
    # Define data file and read X and y
    random_state = 42
    X, y = load_breast_cancer(return_X_y=True)

    n_clusters = 5
    n_estimators = 3

    # Initialize a set of estimators
    estimators = [
        KMeans(n_clusters=n_clusters),
        MiniBatchKMeans(n_clusters=n_clusters),
        AgglomerativeClustering(n_clusters=n_clusters)
    ]

    clf = ClustererEnsemble(estimators, n_clusters=n_clusters)
    clf.fit(X)
    predicted_labels = clf.labels_
    aligned_labels = clf.aligned_labels_

    # Clusterer Ensemble without ininializing a new Class
    original_labels = np.zeros([X.shape[0], n_estimators])

    for i, estimator in enumerate(estimators):
        estimator.fit(X)
        original_labels[:, i] = estimator.labels_

    # Invoke method directly without initialiing a new Class
    labels_by_vote1 = clusterer_ensemble_scores(original_labels, n_estimators,
                                                n_clusters)
    labels_by_vote2, aligned_labels = clusterer_ensemble_scores(