<jupyter_start><jupyter_text>Load digit dataset<jupyter_code>digits = load_digits()
X = np.array(digits.data)
y = np.array(digits.target)
X_scaled = scale(X)
X_reduced = PCA(n_components=2).fit_transform(X_scaled)<jupyter_output><empty_output><jupyter_text>Let's try sklearn's DBSCAN<jupyter_code>db = DBSCAN(eps=0.4, min_samples=14)
db.fit(X_reduced)
print('Estimated number of clusters: %d' % len(np.unique(db.labels_)))
print("Homogeneity: %0.3f" % homogeneity_score(y, db.labels_))
print("Completeness: %0.3f" % completeness_score(y, db.labels_))
print("V-measure: %0.3f" % v_measure_score(y, db.labels_))
print("Adjusted Rand Index: %0.3f"
      % adjusted_rand_score(y, db.labels_))
print("Adjusted Mutual Information: %0.3f"
      % adjusted_mutual_info_score(y, db.labels_))
print("Silhouette Coefficient: %0.3f"
      % silhouette_score(X_reduced, db.labels_))
num_cluster = len(np.unique(y))
unique_labels = set(db.labels_)
colors = [plt.cm.Spectral(each)
          for each in np.linspace(0, 1, len(unique_labels))]

core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True

for k, col in zip(unique_labels, colors):
    if k == -1:
        col = [0, 0, 0, 1]

    class_member_mask = (db.labels_ == k)

    xy = X_reduced[class_member_mask & core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
             markeredgecolor='k', markersize=14)

    xy = X_reduced[class_member_mask & ~core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
             markeredgecolor='k', markersize=6)

plt.title('Estimated number of clusters: %d' % len(np.unique(db.labels_)))
plt.show()<jupyter_output><empty_output><jupyter_text>Now our implementation<jupyter_code>db1 = dscan(eps=0.4, min_samples=13)
db1.fit(X_reduced)
print('Estimated number of clusters: %d' % len(np.unique(db1.labels_)))
print("Homogeneity: %0.3f" % homogeneity_score(y, db1.labels_))
print("Completeness: %0.3f" % completeness_score(y, db1.labels_))
print("V-measure: %0.3f" % v_measure_score(y, db1.labels_))
print("Adjusted Rand Index: %0.3f"
      % adjusted_rand_score(y, db1.labels_))
print("Adjusted Mutual Information: %0.3f"
      % adjusted_mutual_info_score(y, db1.labels_))
print("Silhouette Coefficient: %0.3f"
      % silhouette_score(X_reduced, db1.labels_))
palette = sns.color_palette("hls", num_cluster + 1)
_, ax = plt.subplots(figsize=(12, 8))

for i, index in enumerate(range(len(db1.clusters_))):
    point = np.array(X_reduced[db1.clusters_[index]]).T
    ax.scatter(*point, c=[palette[i], ])

plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup