Ejemplo n.º 1
0
def test_obvious_clustering():
    random = np.random.RandomState(0)
    X = random.randn(40, 2)
    X[20:] += 5

    k1 = _MiniBatchKMedoids(n_clusters=2, max_iter=5, random_state=0, batch_size=20).fit(X)
    k2 = _KMedoids(n_clusters=2, n_passes=10).fit(X)

    assert (np.all(k1.labels_ == k2.labels_) or
            np.all(k1.labels_ == np.logical_not(k2.labels_)))
Ejemplo n.º 2
0
def test_inertia():
    random = np.random.RandomState(0)
    X = random.randn(10, 2)

    for km in (_KMedoids(n_clusters=3,
                         n_passes=5), _MiniBatchKMedoids(n_clusters=3)):
        km.fit(X)
        inertia = 0
        for i in range(len(X)):
            inertia += euclidean(X[km.cluster_ids_[km.labels_[i]]], X[i])

        np.testing.assert_almost_equal(inertia, km.inertia_)
Ejemplo n.º 3
0
def test_inertia():
    random = np.random.RandomState(0)
    X = random.randn(10, 2)

    for km in (_KMedoids(n_clusters=3, n_passes=5),
               _MiniBatchKMedoids(n_clusters=3)):
        km.fit(X)
        inertia = 0
        for i in range(len(X)):
            inertia += euclidean(X[km.cluster_ids_[km.labels_[i]]], X[i])

        np.testing.assert_almost_equal(inertia, km.inertia_)
Ejemplo n.º 4
0
def test_obvious_clustering():
    random = np.random.RandomState(0)
    X = random.randn(40, 2)
    X[20:] += 5

    k1 = _MiniBatchKMedoids(n_clusters=2,
                            max_iter=5,
                            random_state=0,
                            batch_size=20).fit(X)
    k2 = _KMedoids(n_clusters=2, n_passes=10).fit(X)

    assert (np.all(k1.labels_ == k2.labels_)
            or np.all(k1.labels_ == np.logical_not(k2.labels_)))
Ejemplo n.º 5
0
 def medoid():
     return _KMedoids(metric='asdf').fit(np.zeros((10, 2)))
Ejemplo n.º 6
0
def test_4():
    assert_raises(ValueError, lambda: _MiniBatchKMedoids(metric='sdf').fit(np.zeros((10,2))))
    assert_raises(ValueError, lambda: _KMedoids(metric='sdf').fit(np.zeros((10,2))))
Ejemplo n.º 7
0
def test_4():
    assert_raises(
        ValueError,
        lambda: _MiniBatchKMedoids(metric='sdf').fit(np.zeros((10, 2))))
    assert_raises(ValueError,
                  lambda: _KMedoids(metric='sdf').fit(np.zeros((10, 2))))
Ejemplo n.º 8
0
 def medoid():
     return _KMedoids(metric='asdf').fit(np.zeros((10, 2)))