Skip to content

Instantly share code, notes, and snippets.

@makispl
Created August 31, 2021 11:11
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/5f908e82f1676aac14b8a4bf1ff9aa72 to your computer and use it in GitHub Desktop.
Save makispl/5f908e82f1676aac14b8a4bf1ff9aa72 to your computer and use it in GitHub Desktop.
# Switch to a new dataframe instance
# for the gm implementation
plays_gm_df = plays_df.copy()
# Instantiate a GM model with 4 clusters, fit and predict cluster indices
# pay attention to the 'init_params' - we initialized gm based on kmeans
gm = GaussianMixture(n_components=4, init_params='kmeans', tol=1e-4,
covariance_type='full', n_init=10, random_state=1)
plays_gm_df['gm_cluster'] = gm.fit_predict(pca_scores)
# concat plays_gm_df with the pca components
plays_pca_gm_df = pd.concat([plays_gm_df.reset_index(drop=True), pd.DataFrame(
data=pca_scores, columns=['pca_1', 'pca_2', 'pca_3', 'pca_4'])], axis=1)
plays_pca_gm_df.head()
# visualize clusters
x_axis = plays_pca_gm_df['pca_1']
y_axis = plays_pca_gm_df['pca_2']
plt.figure(figsize=(10,8))
sns.scatterplot(x_axis, y_axis, hue = plays_pca_gm_df['gm_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