Skip to content

Commit 8c8ad8c

Browse files
committed
Fix logic order in dbscan: check cluster membership before assigning
1 parent 8259e11 commit 8c8ad8c

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

machine_learning/dbscan.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,14 @@ def dbscan(
171171
while seeds:
172172
current_point = seeds.pop()
173173

174-
if labels[current_point] == -1:
175-
# was noise — reassign as border point of this cluster
176-
labels[current_point] = current_cluster_id
177-
178-
already_in_another = (
174+
# skip points already claimed by a different cluster
175+
if (
179176
labels[current_point] != -1
180177
and labels[current_point] != current_cluster_id
181-
)
182-
if already_in_another:
183-
continue # already in another cluster
178+
):
179+
continue
184180

181+
# assign noise points and unvisited points to this cluster
185182
labels[current_point] = current_cluster_id
186183
current_neighbors = get_neighbors(data, current_point, epsilon)
187184

@@ -199,4 +196,4 @@ def dbscan(
199196
if __name__ == "__main__":
200197
import doctest
201198

202-
doctest.testmod(verbose=True)
199+
doctest.testmod(verbose=True)

0 commit comments

Comments
 (0)