Last active
March 12, 2023 02:16
-
-
Save ukantjadia/1ebc4e77b019242b0ccb2ebeca82a78b to your computer and use it in GitHub Desktop.
Elbow Curve Method for finding number of cluster(K) for clustering with Plot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Elbow Curve Method for finding number of cluster(K) for clustering. | |
# Taking initially range is from (1,11). | |
x = df.iloc[:,[0,1,2,3]].values | |
wcss = [] | |
for i in range(1,11): # we are taking 1,10 values as initial centroids. | |
kmeans = KMeans(n_clusters = i, init = 'k-means++',max_iter = 300, n_init = 10, random_state = 0) | |
kmeans.fit(x) | |
wcss.append(kmeans.inertia_) # appending the within cluster sum of squares | |
fig = plt.figure(figsize=(10,12)) | |
plt.rc('font', size=15) | |
plt.subplots_adjust(bottom = 0, left = 0, top = 1, right = 1) | |
sub1 = fig.add_subplot(2,2,(1,2)) # two rows, two columns, first cell | |
sub1.plot(range(1,11),wcss) | |
sub1.set_title('The Elbow Method') | |
sub1.set_xlabel('Number of Cluster') | |
sub1.set_ylabel('WCSS') | |
sub2 = fig.add_subplot(2,2,3) # two rows, two colums, combined third and forth cell | |
sub2.set_xlim(2,5) | |
sub2.set_ylim(0,150000) | |
sub2.plot(range(1,11),wcss) | |
sub2.set_title('The Elbow Method') | |
sub2.set_xlabel('Number of Cluster') | |
sub2.set_ylabel('WCSS') | |
con1 = ConnectionPatch(xyA=(1.7,140000),xyB=(2,140000),axesA=sub1,axesB=sub2,coordsA="data",coordsB="data") | |
con2 = ConnectionPatch(xyA=(4,22000),xyB=(5,140000),axesA=sub1,axesB=sub2,coordsA="data",coordsB="data") | |
sub2.add_artist(con1) | |
sub2.add_artist(con2) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment