Skip to content

Instantly share code, notes, and snippets.

@ahmdrz
Created May 20, 2019 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmdrz/b21163dfc107622c2a8de24f07ccd389 to your computer and use it in GitHub Desktop.
Save ahmdrz/b21163dfc107622c2a8de24f07ccd389 to your computer and use it in GitHub Desktop.
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
first_random = -2 * np.random.rand(100, 2)
second_random = 1 + 2 * np.random.rand(50, 2)
dataset = np.concatenate((first_random, second_random))
plt.scatter(dataset[:, 0], dataset[:, 1])
plt.show()
model = KMeans(n_clusters=2)
model.fit(dataset)
centers = model.cluster_centers_
plt.scatter(dataset[:, 0], dataset[:, 1], s=50, c='b')
plt.scatter(centers[0][0], centers[0][1], s=200, c='r')
plt.scatter(centers[1][0], centers[1][1], s=200, c='r')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment