Skip to content

Instantly share code, notes, and snippets.

@makispl
Last active August 31, 2021 10:21
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 makispl/b0d6dd3e6cfefbb38660c998aaf5ea27 to your computer and use it in GitHub Desktop.
Save makispl/b0d6dd3e6cfefbb38660c998aaf5ea27 to your computer and use it in GitHub Desktop.
# Instantiate a KMeans model with 4 clusters, fit and predict cluster indices
kmeans_pca = KMeans(n_clusters=4, init='random', random_state=1)
kmeans_pca.fit_predict(pca_scores)
plays_km_df['km_cluster'] = kmeans_pca.labels_
# concat plays_km_df with the pca components
plays_pca_km_df = pd.concat([plays_km_df.reset_index(drop=True), pd.DataFrame(
data=pca_scores, columns=['pca_1', 'pca_2', 'pca_3', 'pca_4'])], axis=1)
# visualize clusters
x_axis = plays_pca_km_df['pca_1']
y_axis = plays_pca_km_df['pca_2']
plt.figure(figsize=(10,8))
sns.scatterplot(x_axis, y_axis, hue = plays_pca_km_df['km_cluster'], palette = ['g', 'r', 'c', 'b'])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment