Advertisement
makispaiktis

ML - Lab 8 - DBSCAN

Oct 23rd, 2022 (edited)
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import sklearn
  5.  
  6.  
  7. # Create Data
  8. X = [2, 2, 8, 5, 7, 6, 1, 4]
  9. Y = [10, 5, 4, 8, 5, 4, 2, 9]
  10. labels = ["x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8"]
  11. ddata = pd.DataFrame({"X": X, "Y": Y}, index=labels)
  12.  
  13.  
  14. # Plot data
  15. plt.scatter(ddata.X, ddata.Y)
  16. for i in range(len(ddata.index)):
  17.     plt.text(ddata.loc[labels[i], "X"], ddata.loc[labels[i], "Y"], '%s' % (str(labels[i])), size=15, zorder=1)
  18. plt.title("Data 2-D")
  19. plt.xlabel("X")
  20. plt.ylabel("Y")
  21. plt.show()
  22.  
  23.  
  24. # DBSCAN
  25. from sklearn.cluster import DBSCAN
  26. clustering = DBSCAN(eps=2, min_samples=2).fit(ddata)
  27. plt.scatter(ddata.X, ddata.Y, c=clustering.labels_, cmap="spring")
  28. for i in range(len(ddata.index)):
  29.     plt.text(ddata.loc[labels[i], "X"], ddata.loc[labels[i], "Y"], '%s' % (str(labels[i])), size=15, zorder=1)
  30. plt.xlabel("X")
  31. plt.ylabel("Y")
  32. plt.title("DBSCAN(eps=2, minPts=2)")
  33. plt.show()
  34.  
  35. clustering = DBSCAN(eps=3.5, min_samples=2).fit(ddata)
  36. plt.scatter(ddata.X, ddata.Y, c=clustering.labels_, cmap="spring")
  37. for i in range(len(ddata.index)):
  38.     plt.text(ddata.loc[labels[i], "X"], ddata.loc[labels[i], "Y"], '%s' % (str(labels[i])), size=15, zorder=1)
  39. plt.xlabel("X")
  40. plt.ylabel("Y")
  41. plt.title("DBSCAN(eps=3.5, minPts=2)")
  42. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement