Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pca = PCA(n_components=2)
- X = StandardScaler().fit_transform(df_clustering_ready)
- principal_components = pca.fit_transform(X)
- df_pca = pd.DataFrame(data = principal_components,columns=['component1', 'component2'])
- clust_opt = OPTICS(min_samples=2, xi=.05, min_cluster_size=.05, n_jobs=-1)
- clust_opt.fit(X)
- reachability = clust_opt.reachability_[clust_opt.ordering_]
- labels_op = clust_opt.labels_[clust_opt.ordering_]
- unique_labels_op = set(labels_op)
- n_clusters_op = len(set(labels_op)) - (1 if -1 in labels_op else 0)
- colors = [plt.cm.Spectral(each)
- for each in np.linspace(0, 1, len(labels_op))]
- for k, col in zip(labels_op, colors):
- if k == -1:
- # Black used for noise.
- col = [0, 0, 0, 1]
- class_member_mask = (labels_op == k)
- Rk = reachability[labels_op == k]
- #xy = X[class_member_mask & core_samples_mask]
- xy = df_pca[class_member_mask & core_samples_mask]
- plt.plot(xy['component1'], xy['component2'], 'o', markerfacecolor=tuple(col),
- markeredgecolor='k', markersize=14)
- #xy = X[class_member_mask & ~core_samples_mask]
- #xy = df_pca[class_member_mask & ~core_samples_mask]
- plt.plot(xy['component1'], xy['component2'], 'o', markerfacecolor=tuple(col),markeredgecolor='k', markersize=6)
- plt.title('Estimated number of clusters: {} \nAutomatic Clustering OPTICS'.format(n_clusters_op))
- plt.grid()
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement