-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdensity_clustering.py
More file actions
1507 lines (1213 loc) · 47.9 KB
/
density_clustering.py
File metadata and controls
1507 lines (1213 loc) · 47.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Density-Based Clustering Along the Main Line
Since all words satisfy sim + drift = 1, the entire space collapses to
a single dimension (similarity). This module finds natural clusters by
detecting gaps in the density distribution.
Algorithm:
1. Project all words onto the similarity axis (0 to 1)
2. For each candidate number of bins n:
- Divide [0, 1] into n equal bins
- Count words in each bin
- Compute gap score (emptiness/sparsity of bins)
3. Find optimal n that maximizes gap clarity
4. Identify cluster boundaries at gap locations
Gap Metrics:
- max_gap: Largest consecutive empty/sparse region
- gap_ratio: Fraction of bins below threshold
- variance: High variance = uneven distribution = gaps
- jenks_gvf: Goodness of Variance Fit (Jenks Natural Breaks)
"""
import numpy as np
from dataclasses import dataclass
from typing import Dict, List, Tuple, Optional
from collections import Counter
@dataclass
class BinAnalysis:
"""Analysis of a single binning configuration."""
n_bins: int
bin_edges: np.ndarray
bin_counts: np.ndarray
bin_centers: np.ndarray
# Gap metrics
max_gap_width: float # Width of largest empty region
n_empty_bins: int # Number of empty bins
gap_ratio: float # Fraction of empty bins
count_variance: float # Variance of bin counts
count_cv: float # Coefficient of variation
# Identified gaps
gap_locations: List[Tuple[float, float]] # (start, end) of each gap
def report(self) -> str:
lines = [
f"Bins: {self.n_bins}",
f" Empty bins: {self.n_empty_bins} ({self.gap_ratio*100:.1f}%)",
f" Max gap width: {self.max_gap_width:.3f}",
f" Count variance: {self.count_variance:.1f}",
f" Count CV: {self.count_cv:.3f}",
]
if self.gap_locations:
lines.append(f" Gaps: {len(self.gap_locations)}")
for start, end in self.gap_locations[:5]:
lines.append(f" [{start:.3f}, {end:.3f}]")
return "\n".join(lines)
@dataclass
class DensityCluster:
"""A cluster identified by density analysis."""
cluster_id: int
start: float # Lower bound (similarity)
end: float # Upper bound (similarity)
center: float # Center of mass
n_words: int # Number of words in cluster
words: List[str] # Words in this cluster
density: float # Words per unit similarity
def __repr__(self):
return f"Cluster({self.cluster_id}: [{self.start:.3f}, {self.end:.3f}], n={self.n_words})"
@dataclass
class ClusteringResult:
"""Result of density-based clustering."""
clusters: List[DensityCluster]
n_clusters: int
optimal_n_bins: int
# All bin analyses
bin_analyses: Dict[int, BinAnalysis]
# Gap detection scores by n_bins
gap_scores: Dict[int, float]
# Word assignments
word_to_cluster: Dict[str, int]
def report(self) -> str:
lines = [
"=" * 60,
"DENSITY-BASED CLUSTERING RESULT",
"=" * 60,
f"Optimal n_bins: {self.optimal_n_bins}",
f"N clusters: {self.n_clusters}",
"",
"Clusters:",
]
for c in self.clusters:
lines.append(f" {c.cluster_id}: [{c.start:.3f}, {c.end:.3f}] "
f"n={c.n_words}, density={c.density:.1f}")
# Show sample words
sample = c.words[:5]
if sample:
lines.append(f" e.g., {', '.join(sample)}")
return "\n".join(lines)
def analyze_bins(
similarities: np.ndarray,
n_bins: int,
sparse_threshold: float = 0.1
) -> BinAnalysis:
"""
Analyze a specific binning configuration.
Args:
similarities: Array of similarity values (0 to 1)
n_bins: Number of bins
sparse_threshold: Fraction of mean count below which a bin is "sparse"
Returns:
BinAnalysis with gap metrics
"""
# Create bins
bin_edges = np.linspace(0, 1, n_bins + 1)
bin_counts, _ = np.histogram(similarities, bins=bin_edges)
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
bin_width = 1.0 / n_bins
# Find empty/sparse bins
mean_count = np.mean(bin_counts)
threshold = mean_count * sparse_threshold
is_sparse = bin_counts <= threshold
n_empty = np.sum(bin_counts == 0)
n_sparse = np.sum(is_sparse)
# Find gap locations (consecutive sparse bins)
gap_locations = []
in_gap = False
gap_start = 0
for i, sparse in enumerate(is_sparse):
if sparse and not in_gap:
in_gap = True
gap_start = bin_edges[i]
elif not sparse and in_gap:
in_gap = False
gap_end = bin_edges[i]
gap_locations.append((gap_start, gap_end))
# Handle gap at the end
if in_gap:
gap_locations.append((gap_start, bin_edges[-1]))
# Compute max gap width
if gap_locations:
max_gap_width = max(end - start for start, end in gap_locations)
else:
max_gap_width = 0.0
# Variance metrics
count_variance = np.var(bin_counts)
count_mean = np.mean(bin_counts)
count_cv = np.std(bin_counts) / count_mean if count_mean > 0 else 0
return BinAnalysis(
n_bins=n_bins,
bin_edges=bin_edges,
bin_counts=bin_counts,
bin_centers=bin_centers,
max_gap_width=max_gap_width,
n_empty_bins=n_empty,
gap_ratio=n_sparse / n_bins,
count_variance=count_variance,
count_cv=count_cv,
gap_locations=gap_locations
)
def compute_gap_score(analysis: BinAnalysis, method: str = "combined") -> float:
"""
Compute a score indicating how well gaps separate clusters.
Higher score = clearer gaps = better clustering.
Args:
analysis: BinAnalysis object
method: Scoring method
- "max_gap": Largest gap width
- "total_gap": Sum of all gap widths
- "cv": Coefficient of variation (higher = more uneven)
- "combined": Weighted combination
Returns:
Gap score (higher = better)
"""
if method == "max_gap":
return analysis.max_gap_width
elif method == "total_gap":
return sum(end - start for start, end in analysis.gap_locations)
elif method == "cv":
return analysis.count_cv
elif method == "combined":
# Combine multiple signals
# Normalize each to [0, 1] range approximately
gap_score = analysis.max_gap_width # Already in [0, 1]
cv_score = min(1.0, analysis.count_cv / 2) # CV > 2 is very high
empty_score = analysis.gap_ratio
# Weight: prioritize actual gaps over variance
return 0.5 * gap_score + 0.3 * cv_score + 0.2 * empty_score
else:
raise ValueError(f"Unknown method: {method}")
def find_optimal_bins(
similarities: np.ndarray,
min_bins: int = 5,
max_bins: int = 50,
method: str = "combined",
verbose: bool = False
) -> Tuple[int, Dict[int, BinAnalysis], Dict[int, float]]:
"""
Find optimal number of bins that maximizes gap clarity.
Args:
similarities: Array of similarity values
min_bins: Minimum bins to try
max_bins: Maximum bins to try
method: Gap scoring method
verbose: Print progress
Returns:
Tuple of (optimal_n_bins, all_analyses, all_scores)
"""
analyses = {}
scores = {}
for n in range(min_bins, max_bins + 1):
analysis = analyze_bins(similarities, n)
score = compute_gap_score(analysis, method)
analyses[n] = analysis
scores[n] = score
if verbose and n % 10 == 0:
print(f" n={n}: score={score:.4f}, max_gap={analysis.max_gap_width:.3f}")
# Find optimal (but prefer smaller n for equal scores - parsimony)
best_n = max(scores.keys(), key=lambda n: (scores[n], -n))
return best_n, analyses, scores
def extract_clusters(
words: List[str],
similarities: np.ndarray,
analysis: BinAnalysis,
min_cluster_size: int = 5
) -> List[DensityCluster]:
"""
Extract clusters from gap analysis.
Clusters are contiguous regions separated by gaps.
Args:
words: List of words corresponding to similarities
similarities: Similarity values
analysis: BinAnalysis with gap locations
min_cluster_size: Minimum words to form a cluster
Returns:
List of DensityCluster objects
"""
# Sort words by similarity
sorted_indices = np.argsort(similarities)
sorted_words = [words[i] for i in sorted_indices]
sorted_sims = similarities[sorted_indices]
# Find cluster boundaries from gaps
boundaries = [0.0]
for gap_start, gap_end in sorted(analysis.gap_locations):
# Use gap midpoint as boundary
boundaries.append((gap_start + gap_end) / 2)
boundaries.append(1.0)
# Assign words to clusters
clusters = []
cluster_id = 0
for i in range(len(boundaries) - 1):
start = boundaries[i]
end = boundaries[i + 1]
# Find words in this range
mask = (sorted_sims >= start) & (sorted_sims < end)
cluster_words = [w for w, m in zip(sorted_words, mask) if m]
cluster_sims = sorted_sims[mask]
if len(cluster_words) >= min_cluster_size:
center = np.mean(cluster_sims) if len(cluster_sims) > 0 else (start + end) / 2
density = len(cluster_words) / (end - start) if end > start else 0
clusters.append(DensityCluster(
cluster_id=cluster_id,
start=start,
end=end,
center=center,
n_words=len(cluster_words),
words=cluster_words,
density=density
))
cluster_id += 1
return clusters
def cluster_by_density(
words: List[str],
similarities: np.ndarray,
min_bins: int = 5,
max_bins: int = 50,
min_cluster_size: int = 5,
method: str = "combined",
verbose: bool = True
) -> ClusteringResult:
"""
Main entry point for density-based clustering.
Args:
words: List of words
similarities: Corresponding similarity values
min_bins, max_bins: Range of bin counts to try
min_cluster_size: Minimum words per cluster
method: Gap scoring method
verbose: Print progress
Returns:
ClusteringResult with clusters and analysis
"""
if verbose:
print("Finding optimal binning...")
optimal_n, analyses, scores = find_optimal_bins(
similarities, min_bins, max_bins, method, verbose
)
if verbose:
print(f"Optimal n_bins: {optimal_n} (score: {scores[optimal_n]:.4f})")
# Extract clusters using optimal binning
best_analysis = analyses[optimal_n]
clusters = extract_clusters(words, similarities, best_analysis, min_cluster_size)
if verbose:
print(f"Found {len(clusters)} clusters")
# Build word-to-cluster mapping
word_to_cluster = {}
for cluster in clusters:
for word in cluster.words:
word_to_cluster[word] = cluster.cluster_id
return ClusteringResult(
clusters=clusters,
n_clusters=len(clusters),
optimal_n_bins=optimal_n,
bin_analyses=analyses,
gap_scores=scores,
word_to_cluster=word_to_cluster
)
def plot_density_analysis(
similarities: np.ndarray,
result: ClusteringResult,
output_path: str = None,
figsize: Tuple[int, int] = (14, 10)
):
"""
Visualize the density analysis and clustering.
Creates a 4-panel figure:
1. Histogram with cluster boundaries
2. Gap score vs n_bins (or GVF vs n_classes for Jenks)
3. Cumulative distribution
4. Cluster summary
"""
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2, figsize=figsize)
# Panel 1: Histogram with cluster boundaries
ax1 = axes[0, 0]
# Use a reasonable number of bins for visualization
n_viz_bins = min(50, max(20, len(similarities) // 10))
ax1.hist(similarities, bins=n_viz_bins, alpha=0.7, edgecolor='black')
# Mark cluster boundaries
colors = plt.cm.tab10(np.linspace(0, 1, len(result.clusters)))
for i, cluster in enumerate(result.clusters):
ax1.axvline(cluster.start, color=colors[i], linestyle='--', linewidth=2, alpha=0.7)
ax1.axvline(cluster.end, color=colors[i], linestyle='--', linewidth=2, alpha=0.7)
# Shade cluster region
ax1.axvspan(cluster.start, cluster.end, alpha=0.1, color=colors[i])
ax1.set_xlabel('Similarity')
ax1.set_ylabel('Count')
ax1.set_title(f'Distribution with {result.n_clusters} Clusters')
# Panel 2: Score vs n (gap score or GVF)
ax2 = axes[0, 1]
if result.gap_scores:
n_values = sorted(result.gap_scores.keys())
scores = [result.gap_scores[n] for n in n_values]
ax2.plot(n_values, scores, 'b-o', linewidth=2, markersize=4)
ax2.axvline(result.optimal_n_bins, color='red', linestyle='--',
label=f'Optimal: {result.optimal_n_bins}')
# Determine label based on whether it's binning or Jenks
if result.bin_analyses:
ylabel = 'Gap Score'
title = 'Gap Score vs Bin Count'
else:
ylabel = 'GVF (Goodness of Variance Fit)'
title = 'GVF vs Number of Classes'
ax2.set_xlabel('Number of bins/classes')
ax2.set_ylabel(ylabel)
ax2.set_title(title)
ax2.legend()
else:
ax2.text(0.5, 0.5, 'No score data available', ha='center', va='center',
transform=ax2.transAxes)
# Panel 3: Cumulative distribution
ax3 = axes[1, 0]
sorted_sims = np.sort(similarities)
cumulative = np.arange(1, len(sorted_sims) + 1) / len(sorted_sims)
ax3.plot(sorted_sims, cumulative, 'b-', linewidth=2)
# Mark cluster boundaries with colors
for i, cluster in enumerate(result.clusters):
ax3.axvspan(cluster.start, cluster.end, alpha=0.2, color=colors[i],
label=f'C{cluster.cluster_id}: n={cluster.n_words}')
ax3.set_xlabel('Similarity')
ax3.set_ylabel('Cumulative fraction')
ax3.set_title('Cumulative Distribution with Clusters')
ax3.legend(loc='lower right', fontsize=8)
# Panel 4: Cluster summary
ax4 = axes[1, 1]
ax4.axis('off')
summary_text = result.report()
ax4.text(0.05, 0.95, summary_text, transform=ax4.transAxes,
fontfamily='monospace', fontsize=9,
verticalalignment='top')
plt.tight_layout()
if output_path:
plt.savefig(output_path, dpi=150, bbox_inches='tight')
print(f"Saved to {output_path}")
else:
plt.show()
plt.close()
# =============================================================================
# JENKS NATURAL BREAKS (Alternative Method)
# =============================================================================
def jenks_breaks(data: np.ndarray, n_classes: int) -> Tuple[List[float], float]:
"""
Compute Jenks Natural Breaks.
Finds class boundaries that minimize within-class variance
and maximize between-class variance.
Args:
data: 1D array of values
n_classes: Number of classes
Returns:
Tuple of (break_points, goodness_of_variance_fit)
"""
data = np.sort(data)
n = len(data)
if n_classes >= n:
return list(data), 1.0
# Initialize matrices
lower_class_limits = np.zeros((n + 1, n_classes + 1))
variance_combinations = np.full((n + 1, n_classes + 1), np.inf)
for i in range(1, n_classes + 1):
lower_class_limits[1, i] = 1
variance_combinations[1, i] = 0
# Build variance matrix
for l in range(2, n + 1):
sum_val = 0
sum_sq = 0
w = 0
for m in range(1, l + 1):
lower_idx = l - m + 1
val = data[lower_idx - 1]
w += 1
sum_val += val
sum_sq += val * val
variance = sum_sq - (sum_val * sum_val) / w
if lower_idx > 1:
for j in range(2, n_classes + 1):
new_var = variance + variance_combinations[lower_idx - 1, j - 1]
if new_var < variance_combinations[l, j]:
lower_class_limits[l, j] = lower_idx
variance_combinations[l, j] = new_var
# Extract breaks
k = n
breaks = [data[-1]]
for j in range(n_classes, 1, -1):
idx = int(lower_class_limits[k, j]) - 1
breaks.insert(0, data[idx])
k = int(lower_class_limits[k, j]) - 1
breaks.insert(0, data[0])
# Compute GVF (Goodness of Variance Fit)
total_variance = np.var(data) * len(data)
within_variance = 0
for i in range(n_classes):
class_data = data[(data >= breaks[i]) & (data < breaks[i + 1])]
if len(class_data) > 0:
within_variance += np.var(class_data) * len(class_data)
gvf = 1 - (within_variance / total_variance) if total_variance > 0 else 1
return breaks, gvf
def cluster_by_jenks(
words: List[str],
similarities: np.ndarray,
min_classes: int = 2,
max_classes: int = 10,
verbose: bool = True
) -> ClusteringResult:
"""
Cluster using Jenks Natural Breaks.
Finds optimal number of classes by maximizing GVF improvement.
"""
if verbose:
print("Computing Jenks Natural Breaks...")
# Try different numbers of classes
gvf_scores = {}
all_breaks = {}
for n in range(min_classes, max_classes + 1):
breaks, gvf = jenks_breaks(similarities, n)
gvf_scores[n] = gvf
all_breaks[n] = breaks
if verbose:
print(f" n={n}: GVF={gvf:.4f}")
# Find elbow: where GVF improvement drops
gvf_improvements = {}
prev_gvf = 0
for n in range(min_classes, max_classes + 1):
gvf_improvements[n] = gvf_scores[n] - prev_gvf
prev_gvf = gvf_scores[n]
# Simple heuristic: find where improvement drops below 0.02
optimal_n = min_classes
for n in range(min_classes + 1, max_classes + 1):
if gvf_improvements[n] < 0.02:
optimal_n = n - 1
break
optimal_n = n
if verbose:
print(f"Optimal n_classes: {optimal_n} (GVF={gvf_scores[optimal_n]:.4f})")
# Build clusters from breaks
breaks = all_breaks[optimal_n]
sorted_indices = np.argsort(similarities)
sorted_words = [words[i] for i in sorted_indices]
sorted_sims = similarities[sorted_indices]
clusters = []
for i in range(len(breaks) - 1):
start, end = breaks[i], breaks[i + 1]
mask = (sorted_sims >= start) & (sorted_sims < end)
cluster_words = [w for w, m in zip(sorted_words, mask) if m]
cluster_sims = sorted_sims[mask]
if len(cluster_words) > 0:
clusters.append(DensityCluster(
cluster_id=i,
start=start,
end=end,
center=np.mean(cluster_sims),
n_words=len(cluster_words),
words=cluster_words,
density=len(cluster_words) / (end - start) if end > start else 0
))
# Build word-to-cluster mapping
word_to_cluster = {}
for cluster in clusters:
for word in cluster.words:
word_to_cluster[word] = cluster.cluster_id
return ClusteringResult(
clusters=clusters,
n_clusters=len(clusters),
optimal_n_bins=optimal_n,
bin_analyses={}, # Not applicable for Jenks
gap_scores=gvf_scores,
word_to_cluster=word_to_cluster
)
# =============================================================================
# DBSCAN METHOD
# =============================================================================
def dbscan_clustering(
words: List[str],
similarities: np.ndarray,
eps: float = None,
min_samples: int = 5,
auto_eps_percentile: float = 5,
verbose: bool = True
) -> ClusteringResult:
"""
Cluster using DBSCAN (Density-Based Spatial Clustering).
DBSCAN groups points that are closely packed together, marking
points in low-density regions as outliers.
For 1D data, this effectively finds "dense runs" along the
similarity axis, with gaps between them.
Args:
words: List of words
similarities: Corresponding similarity values
eps: Maximum distance between neighbors (None = auto)
min_samples: Minimum points to form a dense region
auto_eps_percentile: Percentile of k-distances for auto eps
verbose: Print progress
Returns:
ClusteringResult with clusters
"""
from sklearn.cluster import DBSCAN
if verbose:
print("Computing DBSCAN clustering...")
# Reshape for sklearn (needs 2D array)
X = similarities.reshape(-1, 1)
# Auto-select eps using k-distance graph
if eps is None:
# Compute k-nearest neighbor distances
k = min_samples
from sklearn.neighbors import NearestNeighbors
nn = NearestNeighbors(n_neighbors=k)
nn.fit(X)
distances, _ = nn.kneighbors(X)
k_distances = np.sort(distances[:, -1])
# Use percentile of k-distances as eps
eps = np.percentile(k_distances, auto_eps_percentile)
if verbose:
print(f" Auto eps (percentile {auto_eps_percentile}): {eps:.4f}")
print(f" k-distance range: [{k_distances.min():.4f}, {k_distances.max():.4f}]")
# Run DBSCAN
db = DBSCAN(eps=eps, min_samples=min_samples)
labels = db.fit_predict(X)
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)
n_noise = np.sum(labels == -1)
if verbose:
print(f" Found {n_clusters} clusters, {n_noise} noise points")
# Build cluster objects
clusters = []
for cluster_id in range(n_clusters):
mask = labels == cluster_id
cluster_words = [w for w, m in zip(words, mask) if m]
cluster_sims = similarities[mask]
if len(cluster_words) > 0:
clusters.append(DensityCluster(
cluster_id=cluster_id,
start=cluster_sims.min(),
end=cluster_sims.max(),
center=np.mean(cluster_sims),
n_words=len(cluster_words),
words=cluster_words,
density=len(cluster_words) / (cluster_sims.max() - cluster_sims.min() + 1e-10)
))
# Sort clusters by start position
clusters.sort(key=lambda c: c.start)
# Reassign cluster IDs after sorting
for i, c in enumerate(clusters):
c.cluster_id = i
# Build word-to-cluster mapping (including noise as -1)
word_to_cluster = {}
for word, label in zip(words, labels):
word_to_cluster[word] = label
# Store DBSCAN info for plotting
dbscan_info = {
'labels': labels,
'eps': eps,
'min_samples': min_samples,
'n_noise': n_noise
}
result = ClusteringResult(
clusters=clusters,
n_clusters=n_clusters,
optimal_n_bins=n_clusters,
bin_analyses={},
gap_scores={},
word_to_cluster=word_to_cluster
)
result.dbscan_info = dbscan_info
return result
def find_optimal_dbscan_eps(
similarities: np.ndarray,
min_samples: int = 5,
eps_range: Tuple[float, float] = None,
n_eps: int = 50,
verbose: bool = True
) -> Tuple[float, Dict[float, int]]:
"""
Find optimal eps by testing range and looking for stability.
Args:
similarities: Similarity values
min_samples: DBSCAN min_samples parameter
eps_range: (min_eps, max_eps) or None for auto
n_eps: Number of eps values to test
verbose: Print progress
Returns:
Tuple of (optimal_eps, {eps: n_clusters})
"""
from sklearn.cluster import DBSCAN
X = similarities.reshape(-1, 1)
# Auto eps range based on data spread
if eps_range is None:
data_range = similarities.max() - similarities.min()
eps_range = (data_range * 0.005, data_range * 0.1)
eps_values = np.linspace(eps_range[0], eps_range[1], n_eps)
n_clusters_by_eps = {}
for eps in eps_values:
db = DBSCAN(eps=eps, min_samples=min_samples)
labels = db.fit_predict(X)
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)
n_clusters_by_eps[eps] = n_clusters
# Find "elbow" - where number of clusters stabilizes
# Look for longest run of same cluster count
counts = list(n_clusters_by_eps.values())
eps_list = list(n_clusters_by_eps.keys())
# Find the most common cluster count in the stable region
from collections import Counter
count_freq = Counter(counts)
# Prefer moderate number of clusters (not 1, not too many)
best_count = None
for count, freq in count_freq.most_common():
if 2 <= count <= 10:
best_count = count
break
if best_count is None:
best_count = count_freq.most_common(1)[0][0]
# Find eps that gives this count (middle of the stable region)
matching_eps = [e for e, c in n_clusters_by_eps.items() if c == best_count]
optimal_eps = np.median(matching_eps)
if verbose:
print(f" Tested {n_eps} eps values in [{eps_range[0]:.4f}, {eps_range[1]:.4f}]")
print(f" Cluster counts: {dict(count_freq.most_common(5))}")
print(f" Optimal eps: {optimal_eps:.4f} (gives {best_count} clusters)")
return optimal_eps, n_clusters_by_eps
def plot_dbscan_analysis(
similarities: np.ndarray,
result: ClusteringResult,
output_path: str = None,
figsize: Tuple[int, int] = (14, 10)
):
"""
Visualize DBSCAN clustering.
Creates a 4-panel figure:
1. Scatter plot with cluster colors
2. Histogram with cluster regions
3. Cumulative distribution with clusters
4. Cluster summary
"""
import matplotlib.pyplot as plt
if not hasattr(result, 'dbscan_info'):
print("No DBSCAN info available. Use dbscan_clustering() first.")
return
info = result.dbscan_info
labels = info['labels']
fig, axes = plt.subplots(2, 2, figsize=figsize)
# Color map: -1 (noise) is gray, clusters get distinct colors
unique_labels = sorted(set(labels))
colors = {}
cluster_colors = plt.cm.tab10(np.linspace(0, 1, max(len(unique_labels), 1)))
color_idx = 0
for label in unique_labels:
if label == -1:
colors[label] = 'gray'
else:
colors[label] = cluster_colors[color_idx]
color_idx += 1
# Panel 1: Scatter plot (1D projected to strip)
ax1 = axes[0, 0]
# Add jitter for visibility
np.random.seed(42)
y_jitter = np.random.uniform(-0.3, 0.3, len(similarities))
for label in unique_labels:
mask = labels == label
label_name = f'Cluster {label}' if label >= 0 else f'Noise (n={info["n_noise"]})'
ax1.scatter(similarities[mask], y_jitter[mask],
c=[colors[label]], label=label_name, alpha=0.6, s=20)
ax1.set_xlabel('Similarity')
ax1.set_ylabel('(jittered for visibility)')
ax1.set_title(f'DBSCAN Clustering (eps={info["eps"]:.4f}, min_samples={info["min_samples"]})')
ax1.legend(loc='upper left', fontsize=8)
ax1.set_ylim(-0.5, 0.5)
# Panel 2: Histogram with cluster regions
ax2 = axes[0, 1]
ax2.hist(similarities, bins=50, alpha=0.5, edgecolor='black', label='All')
# Overlay cluster regions
for cluster in result.clusters:
ax2.axvspan(cluster.start, cluster.end, alpha=0.2,
color=colors[cluster.cluster_id],
label=f'C{cluster.cluster_id}: [{cluster.start:.2f}, {cluster.end:.2f}]')
ax2.set_xlabel('Similarity')
ax2.set_ylabel('Count')
ax2.set_title('Distribution with Cluster Regions')
ax2.legend(fontsize=8)
# Panel 3: Cumulative distribution
ax3 = axes[1, 0]
sorted_sims = np.sort(similarities)
cumulative = np.arange(1, len(sorted_sims) + 1) / len(sorted_sims)
ax3.plot(sorted_sims, cumulative, 'b-', linewidth=2)
for cluster in result.clusters:
ax3.axvspan(cluster.start, cluster.end, alpha=0.2,
color=colors[cluster.cluster_id],
label=f'C{cluster.cluster_id}: n={cluster.n_words}')
ax3.set_xlabel('Similarity')
ax3.set_ylabel('Cumulative fraction')
ax3.set_title('Cumulative Distribution with Clusters')
ax3.legend(loc='lower right', fontsize=8)
# Panel 4: Summary
ax4 = axes[1, 1]
ax4.axis('off')
summary_lines = [
"=" * 50,
"DBSCAN CLUSTERING RESULT",
"=" * 50,
f"eps: {info['eps']:.4f}",
f"min_samples: {info['min_samples']}",
f"N clusters: {result.n_clusters}",
f"Noise points: {info['n_noise']}",
"",
"Clusters:"
]
for c in result.clusters:
summary_lines.append(
f" {c.cluster_id}: [{c.start:.3f}, {c.end:.3f}] "
f"n={c.n_words}"
)
sample = c.words[:5]
if sample:
summary_lines.append(f" e.g., {', '.join(sample)}")
# Noise words
noise_words = [w for w, l in zip(result.word_to_cluster.keys(), labels) if l == -1]
if noise_words:
summary_lines.append(f"\nNoise ({len(noise_words)} words):")
summary_lines.append(f" e.g., {', '.join(noise_words[:10])}")
ax4.text(0.05, 0.95, '\n'.join(summary_lines), transform=ax4.transAxes,
fontfamily='monospace', fontsize=9,
verticalalignment='top')
plt.tight_layout()
if output_path:
plt.savefig(output_path, dpi=150, bbox_inches='tight')
print(f"Saved to {output_path}")
else:
plt.show()
plt.close()
# =============================================================================
# KERNEL DENSITY ESTIMATION (KDE) METHOD
# =============================================================================
def kde_clustering(
words: List[str],
similarities: np.ndarray,
bandwidth: float = None,
min_cluster_size: int = 5,
local_minima_threshold: float = 0.1,
verbose: bool = True
) -> ClusteringResult:
"""
Cluster using Kernel Density Estimation.
Finds clusters by:
1. Estimating smooth density function via KDE
2. Finding local minima (valleys) in the density
3. Using valleys as cluster boundaries
This naturally handles relative gaps - a valley in a dense region
is detected even if its absolute density is higher than peaks
in sparse regions.