SHARE
TWEET

Untitled

a guest Jan 22nd, 2020 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. from sklearn.cluster import OPTICS, cluster_optics_dbscan
  3. import matplotlib.gridspec as gridspec
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6.  
  7.  
  8.  
  9. clust = OPTICS(min_samples=2, xi=.005, min_cluster_size=0.05)
  10.  
  11. # Run the fit
  12. clust.fit(X)
  13. eps_1 = 2
  14. labels_050 = cluster_optics_dbscan(reachability=clust.reachability_,
  15.                                    core_distances=clust.core_distances_,
  16.                                    ordering=clust.ordering_, eps=eps_1)
  17. eps_2 = 0.1
  18. labels_200 = cluster_optics_dbscan(reachability=clust.reachability_,
  19.                                    core_distances=clust.core_distances_,
  20.                                    ordering=clust.ordering_, eps=eps_2)
  21.  
  22. space = np.arange(len(X))
  23. reachability = clust.reachability_[clust.ordering_]
  24. labels = clust.labels_[clust.ordering_]
  25.  
  26. plt.figure(figsize=(20, 14))
  27. G = gridspec.GridSpec(2, 3)
  28. ax1 = plt.subplot(G[0, :])
  29. ax2 = plt.subplot(G[1, 0])
  30. ax3 = plt.subplot(G[1, 1])
  31. ax4 = plt.subplot(G[1, 2])
  32.  
  33. # Reachability plot
  34. colors = ['g.', 'r.', 'b.', 'y.', 'c.']
  35. for klass, color in zip(range(0, 5), colors):
  36.     Xk = space[labels == klass]
  37.     Rk = reachability[labels == klass]
  38.     ax1.plot(Xk, Rk, color, alpha=0.3)
  39. ax1.plot(space[labels == -1], reachability[labels == -1], 'k.', alpha=0.3)
  40. ax1.plot(space, np.full_like(space, float(eps_1), dtype=float), 'k-', alpha=0.5)
  41. ax1.plot(space, np.full_like(space, float(eps_2), dtype=float), 'k-.', alpha=0.5)
  42. ax1.set_ylabel('Reachability (epsilon distance)')
  43. ax1.set_title('Reachability Plot')
  44.  
  45. # OPTICS
  46. colors = ['g.', 'r.', 'b.', 'y.', 'c.']
  47. for klass, color in zip(range(0, 5), colors):
  48.     Xk = X[clust.labels_ == klass]
  49.     ax2.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3)
  50. ax2.plot(X[clust.labels_ == -1, 0], X[clust.labels_ == -1, 1], 'k+', alpha=0.1)
  51. n_clusters = len(set(clust.labels_)) - (1 if -1 in clust.labels_ else 0)
  52. ax2.set_title('cluster {} Automatic Clustering\nOPTICS xi'.format(n_clusters))
  53.  
  54. # DBSCAN at 0.5
  55. colors = ['g', 'greenyellow', 'olive', 'r', 'b', 'c']
  56. for klass, color in zip(range(0, 6), colors):
  57.     Xk = X[labels_050 == klass]
  58.     ax3.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3, marker='.')
  59. ax3.plot(X[labels_050 == -1, 0], X[labels_050 == -1, 1], 'k+', alpha=0.1)
  60. n_clusters = len(set(labels_050)) - (1 if -1 in labels_050 else 0)
  61. ax3.set_title('cluster {} Clustering at {} epsilon cut\nDBSCAN'.format(n_clusters,eps_1))
  62.  
  63. # DBSCAN at 2.
  64. colors = ['g.', 'm.', 'y.', 'c.']
  65. for klass, color in zip(range(0, 4), colors):
  66.     Xk = X[labels_200 == klass]
  67.     ax4.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3)
  68. ax4.plot(X[labels_200 == -1, 0], X[labels_200 == -1, 1], 'k+', alpha=0.1)
  69. n_clusters = len(set(labels_200)) - (1 if -1 in labels_200 else 0)
  70. ax4.set_title('cluster {} Clustering at {} epsilon cut\nDBSCAN'.format(n_clusters,eps_2))
  71. plt.savefig('f2')
  72. plt.tight_layout()
  73. plt.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top