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
fig = plt.figure(figsize=(20,15)) | |
ax = fig.add_subplot(111, projection='3d') | |
ax.scatter(df2['Age'][df2['Label']==0], df2['Annual Income (k$)'][df2['Label']==0], | |
df2['Spending Score (1-100)'][df2['Label']==0], c='green', s=50) | |
ax.scatter(df2['Age'][df2['Label']==1], df2['Annual Income (k$)'][df2['Label']==1], | |
df2['Spending Score (1-100)'][df2['Label']==1], c='blue', s=50) | |
ax.scatter(df2['Age'][df2['Label']==2], df2['Annual Income (k$)'][df2['Label']==2], | |
df2['Spending Score (1-100)'][df2['Label']==2], c='black', s=50) | |
ax.scatter(df2['Age'][df2['Label']==3], df2['Annual Income (k$)'][df2['Label']==3], |
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
df2_clusters = df2['Label'].value_counts() | |
df2_clusters |
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
km = KMeans(n_clusters=5) | |
km.fit(df2) | |
y = km.predict(df2) | |
df2['Label'] = y | |
df2.head() |
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
plt.figure(figsize=(13, 6)) | |
plt.plot(range(1, 11), errors) | |
plt.plot(range(1,11), errors, linewidth=3, color='blue', marker='8') | |
plt.xlabel('No. of Clusters') | |
plt.ylabel('WCSS') | |
plt.xticks(np.arange(1,11,1)) | |
plt.show() |
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
errors = [] | |
for i in range(1, 11): | |
kmeans = KMeans(n_clusters=i) | |
kmeans.fit(df2) | |
errors.append(kmeans.inertia_) |
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
df2 = df[['Annual Income (k$)', 'Spending Score (1-100)', 'Age']] | |
df2.head() |
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
sns.scatterplot(x='Annual Income (k$)', y='Spending Score (1-100)', data=df1, hue='Label', s=50, | |
palette =['red', 'green', 'black', 'brown', 'orange']); |
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
df1_clusters = df1['Label'].value_counts() | |
df1_clusters |
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
km = KMeans(n_clusters=5) | |
km.fit(df1) | |
y = km.predict(df1) | |
df1['Label'] = y | |
df1.head() |
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
# plot | |
plt.figure(figsize=(13, 6)) | |
plt.plot(range(1, 11), errors) | |
plt.plot(range(1,11), errors, linewidth=3, color='blue', marker='8') | |
plt.xlabel('No. of Clusters') | |
plt.ylabel('WCSS') | |
plt.xticks(np.arange(1,11,1)) | |
plt.show() |
NewerOlder