-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_sklearn_K-Means.py
More file actions
52 lines (40 loc) · 2.41 KB
/
Python_sklearn_K-Means.py
File metadata and controls
52 lines (40 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Python tutorial using scikit-learn for k-means clustering using a randomly generated dataset.
# The k-means clustering algorithm attempts to split a given anonymous data set (a set containing no information as to class identity) into a fixed number (k) of clusters. Initially k number of so called centroids are chosen.
# A centroid is a data point (imaginary or real) at the center of a cluster.
# Python is an interpreted, high-level, general-purpose programming language.
# sci-kit learn or sklearn is an high-level machine learning library for python.
# Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy.
# Import python libraries
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
# create randomly generated dataset containing points
X, y = make_blobs(n_samples=150, n_features=2, centers=3, cluster_std=0.5, shuffle=True, random_state=0)
# plot the newly generated dataset using matplotlib
plt.scatter(X[:, 0], X[:, 1], c='white', marker='o', edgecolor='black', s=50)
plt.show()
# Create the K Means model
km = KMeans(n_clusters=3, init='random', n_init=10, max_iter=300, tol=1e-04, random_state=0)
#Train the model using the X dataset
y_km = km.fit_predict(X)
# plot the three seperate clusters using the scatter function
plt.scatter(X[y_km == 0, 0], X[y_km == 0, 1], s=50, c='lightgreen', marker='s', edgecolor='black', label='cluster 1')
plt.scatter(X[y_km == 1, 0], X[y_km == 1, 1], s=50, c='orange', marker='o', edgecolor='black', label='cluster 2')
plt.scatter(X[y_km == 2, 0], X[y_km == 2, 1], s=50, c='lightblue', marker='v', edgecolor='black', label='cluster 3')
# plot the three seperate centroids for each of the clusters
plt.scatter(km.cluster_centers_[:, 0], km.cluster_centers_[:, 1], s=250, marker='*', c='red', edgecolor='black', label='centroids')
plt.legend(scatterpoints=1)
plt.grid()
plt.show()
#################### The elbow method ####################
# calculate the distortion map for the range of number of clusters that should be using for K Means
distortions = []
for i in range(1, 11):
km = KMeans(n_clusters=i, init='random', n_init=10, max_iter=300, tol=1e-04, random_state=0)
km.fit(X)
distortions.append(km.inertia_)
# plot the distortion map using matplotlib
plt.plot(range(1, 11), distortions, marker='o')
plt.xlabel('Number of clusters')
plt.ylabel('Distortion')
plt.show()