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',
            label='cluster 1')

plt.scatter(X[y_clust == 1, 0],
            X[y_clust == 1, 1],
            s=50,
            c='orange',
            marker='o',
            label='cluster 2')
示例#2
0
X, y = iris_data()
X = X[:, [
    2, 3
]]  # choose combination 2 of 4 features, then will results in 6 kinds of results. see the png.

#plt.scatter(X[:,0],X[:,1], c='red')
#plt.show()

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

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

# Visualize the cluster memberships
y_cluster = km.predict(X)

plt.scatter(X[y_cluster == 0, 0],
            X[y_cluster == 0, 1],
            s=50,
            c='lightgreen',
            marker='s',
            label='cluster 1')

plt.scatter(X[y_cluster == 1, 0],
            X[y_cluster == 1, 1],
            s=50,
            c='orange',
            marker='o',
            label='cluster 2')