Beispiel #1
0
def test_three_blobs_multi():
    km = Kmeans(k=3,
                max_iter=50,
                random_seed=1,
                print_progress=0)
    y_pred = km.fit(X).predict(X)
    assert (y_pred == y).all()
Beispiel #2
0
def test_three_blobs_centroids():
    km = Kmeans(k=3, max_iter=50, random_seed=1, print_progress=0)

    centroids = np.array([[-1.5947298, 2.92236966], [2.06521743, 0.96137409],
                          [0.9329651, 4.35420713]])

    km.fit(X)
    np.testing.assert_almost_equal(centroids, km.centroids_, decimal=2)
Beispiel #3
0
def test_three_blobs_1sample():
    km = Kmeans(k=3,
                max_iter=50,
                random_seed=1,
                print_progress=0)
    sample = X[1, :].reshape(1, 2)

    y_pred = km.fit(X).predict(sample)
    assert y_pred[0] == y[1]
Beispiel #4
0
def test_nonfitted():
    km = Kmeans(k=3,
                max_iter=50,
                random_seed=1,
                print_progress=0)

    km._is_fitted = False
    assert_raises(AttributeError,
                  'Model is not fitted, yet.',
                  km.predict,
                  X)
Beispiel #5
0
def test_three_blobs_centroids():
    km = Kmeans(k=3,
                max_iter=50,
                random_seed=1,
                print_progress=0)

    centroids = np.array([[-1.5947298, 2.92236966],
                          [2.06521743, 0.96137409],
                          [0.9329651, 4.35420713]])

    km.fit(X)
    np.testing.assert_almost_equal(centroids, km.centroids_, decimal=2)
Beispiel #6
0
def test_continue_training():
    km = Kmeans(k=3,
                max_iter=1,
                random_seed=1,
                print_progress=0)

    first_iter = np.array([[-1.33, 3.26],
                           [1.95, 0.99],
                           [1.09, 4.26]])

    second_iter = np.array([[-1.5947298, 2.92236966],
                            [2.06521743, 0.96137409],
                            [0.9329651, 4.35420713]])

    km.fit(X)
    np.testing.assert_almost_equal(first_iter, km.centroids_, decimal=2)
    assert km.iterations_ == 1, km.iterations_

    km.fit(X, init_params=False)
    np.testing.assert_almost_equal(second_iter, km.centroids_, decimal=2)
    assert km.iterations_ == 2, km.iterations_
Beispiel #7
0
def test_continue_training():
    km = Kmeans(k=3, max_iter=1, random_seed=1, print_progress=0)

    first_iter = np.array([[-1.33, 3.26], [1.95, 0.99], [1.09, 4.26]])

    second_iter = np.array([[-1.5947298, 2.92236966], [2.06521743, 0.96137409],
                            [0.9329651, 4.35420713]])

    km.fit(X)
    np.testing.assert_almost_equal(first_iter, km.centroids_, decimal=2)
    assert km.iterations_ == 1, km.iterations_

    km.fit(X, init_params=False)
    np.testing.assert_almost_equal(second_iter, km.centroids_, decimal=2)
    assert km.iterations_ == 2, km.iterations_
Beispiel #8
0
def test_print_progress_3():
    km = Kmeans(k=3,
                max_iter=50,
                random_seed=1,
                print_progress=3)
    km.fit(X)
Beispiel #9
0
def test_clone():
    km = Kmeans(k=2)
    clone(km)
# # Kmeans

# In[106]:

import matplotlib.pyplot as plt
from mlxtend.data import three_blobs_data

X, y = three_blobs_data()
plt.scatter(X[:, 0], X[:, 1], c='b')
plt.show()

# In[107]:

from mlxtend.cluster import Kmeans

km = Kmeans(k=3, max_iter=50, random_seed=1, print_progress=3)

km.fit(X)

print('Iterations until convergence:', km.iterations_)
print('Final centroids:\n', km.centroids_)

# In[108]:

y_clust = km.predict(X)

plt.scatter(X[y_clust == 0, 0],
            X[y_clust == 0, 1],
            s=50,
            c='lightgreen',
            marker='s',