Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #program 7
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.cluster import KMeans
- # Data
- x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8])
- x2 = np.array([5, 4, 6, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3])
- # Plot original data
- plt.scatter(x1, x2)
- plt.xlim([0, 10])
- plt.ylim([0, 10])
- plt.title('Dataset')
- plt.show()
- # KMeans clustering
- X = np.column_stack((x1, x2))
- kmeans = KMeans(n_clusters=3).fit(X)
- # Plot clustered data
- for i, label in enumerate(kmeans.labels_):
- plt.plot(x1[i], x2[i], color=['b', 'g', 'r'][label], marker=['o',
- 'v', 's'][label], ls='None')
- plt.xlim([0, 10])
- plt.ylim([0, 10])
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment