Skip to content

Instantly share code, notes, and snippets.

@liliya2022
Created March 12, 2023 07:57
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 liliya2022/63d389ec39c3f08a58f89f831713a26d to your computer and use it in GitHub Desktop.
Save liliya2022/63d389ec39c3f08a58f89f831713a26d to your computer and use it in GitHub Desktop.
km = KMeans(n_clusters = 5)
y_predicted = km.fit_predict(salary_story[['Total Salary Paid', 'Home Price']])
salary_story['Cluster'] = y_predicted
plt.figure(figsize = (10,5))
df1 = salary_story[salary_story.Cluster == 0]
df2 = salary_story[salary_story.Cluster == 1]
df3 = salary_story[salary_story.Cluster == 2]
df4 = salary_story[salary_story.Cluster == 3]
df5 = salary_story[salary_story.Cluster == 4]
plt.scatter(df1['Total Salary Paid'], df1['Home Price'], color = 'green')
plt.scatter(df2['Total Salary Paid'], df2['Home Price'], color = 'red')
plt.scatter(df3['Total Salary Paid'], df3['Home Price'], color = 'yellow')
plt.scatter(df4['Total Salary Paid'], df4['Home Price'], color='blue')
plt.scatter(df5['Total Salary Paid'], df5['Home Price'], color='black')
plt.scatter(km.cluster_centers_[:, 0], km.cluster_centers_[:, 1], color='purple', marker='*', label='centroid')
plt.xlabel('Total Salary Paid')
plt.ylabel('Home Price')
plt.legend()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment