Advertisement
makispaiktis

ML - Lab 7 - kmedoids

Oct 22nd, 2022 (edited)
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. # Create Data
  4. Rank = ["High", "Low", "High", "Low", "Low", "High"]
  5. Topic = ["SE", "SE", "ML", "DM", "ML", "SE"]
  6. conferences = pd.DataFrame({"Rank": Rank, "Topic": Topic})
  7. print(conferences)
  8. print()
  9.  
  10. # kmedoids
  11. # Not 'pip install sklearn_extra' ---> Try this: pip install scikit-learn-extra
  12. from sklearn_extra.cluster import KMedoids
  13. from sklearn.preprocessing import OneHotEncoder
  14. encoder = OneHotEncoder(handle_unknown="ignore", sparse=False)
  15. encoder = encoder.fit(conferences)
  16. conferences = encoder.transform(conferences)
  17. kmedoids = KMedoids(n_clusters=3, method="pam").fit(conferences)
  18.  
  19. # Displays
  20. print("Medoids: ")
  21. print(kmedoids.cluster_centers_)
  22. print("Labels: ")
  23. print(kmedoids.labels_)
  24. # print(conferences.loc[kmedoids.medoid_indices_, :])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement