SHARE
TWEET

pca_python

a guest Jan 22nd, 2020 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pca = PCA(n_components=2)
  2.  
  3. X = StandardScaler().fit_transform(df_clustering_ready)
  4. principal_components = pca.fit_transform(X)
  5. df_pca = pd.DataFrame(data = principal_components,columns=['component1', 'component2'])
  6.  
  7. clust_opt = OPTICS(min_samples=2, xi=.05, min_cluster_size=.05, n_jobs=-1)
  8. clust_opt.fit(X)
  9. reachability = clust_opt.reachability_[clust_opt.ordering_]
  10. labels_op = clust_opt.labels_[clust_opt.ordering_]
  11. unique_labels_op = set(labels_op)
  12. n_clusters_op = len(set(labels_op)) - (1 if -1 in labels_op else 0)
  13. colors = [plt.cm.Spectral(each)
  14.           for each in np.linspace(0, 1, len(labels_op))]
  15. for k, col in zip(labels_op, colors):
  16.     if k == -1:
  17.         # Black used for noise.
  18.         col = [0, 0, 0, 1]
  19.  
  20.     class_member_mask = (labels_op == k)
  21.     Rk = reachability[labels_op == k]
  22.     #xy = X[class_member_mask & core_samples_mask]
  23.     xy = df_pca[class_member_mask & core_samples_mask]
  24.     plt.plot(xy['component1'], xy['component2'], 'o', markerfacecolor=tuple(col),
  25.              markeredgecolor='k', markersize=14)
  26.  
  27.     #xy = X[class_member_mask & ~core_samples_mask]
  28.     #xy = df_pca[class_member_mask & ~core_samples_mask]
  29.     plt.plot(xy['component1'], xy['component2'], 'o', markerfacecolor=tuple(col),markeredgecolor='k', markersize=6)
  30.  
  31. plt.title('Estimated number of clusters: {} \nAutomatic Clustering OPTICS'.format(n_clusters_op))
  32. plt.grid()
  33. plt.legend()
  34. 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