Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 36700a1

Browse files
authoredOct 2, 2024··
Update dbscan.py with more test cases
1 parent 94caecf commit 36700a1

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed
 

‎machine_learning/dbscan.py

+72-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import math
99

10+
import matplotlib.patches as mpatches
1011
import matplotlib.pyplot as plt
1112
import pandas as pd
1213

@@ -118,6 +119,38 @@ def perform_dbscan(self) -> dict[int, list[int]]:
118119
11 [2, 10, 11, 12]
119120
12 [9, 11, 12]
120121
122+
>>> result = DbScan(3, 2.5).perform_dbscan()
123+
>>> for key in sorted(result):
124+
... print(key, sorted(result[key]))
125+
1 [1, 2, 10, 11]
126+
2 [1, 2, 3, 10, 11]
127+
3 [2, 3, 4, 11]
128+
4 [3, 4, 5, 6, 7, 8]
129+
5 [4, 5, 6, 7, 8]
130+
6 [4, 5, 6, 7]
131+
7 [4, 5, 6, 7, 8]
132+
8 [4, 5, 7, 8]
133+
9 [9, 11, 12]
134+
10 [1, 2, 10, 11, 12]
135+
11 [1, 2, 3, 9, 10, 11, 12]
136+
12 [9, 10, 11, 12]
137+
138+
>>> result = DbScan(5, 2.5).perform_dbscan()
139+
>>> for key in sorted(result):
140+
... print(key, sorted(result[key]))
141+
1 [1, 2, 10, 11]
142+
2 [1, 2, 3, 10, 11]
143+
3 [2, 3, 4, 11]
144+
4 [3, 4, 5, 6, 7, 8]
145+
5 [4, 5, 6, 7, 8]
146+
6 [4, 5, 6, 7]
147+
7 [4, 5, 6, 7, 8]
148+
8 [4, 5, 7, 8]
149+
9 [9, 11, 12]
150+
10 [1, 2, 10, 11, 12]
151+
11 [1, 2, 3, 9, 10, 11, 12]
152+
12 [9, 10, 11, 12]
153+
121154
"""
122155
if type(self.file) is str:
123156
data = pd.read_csv(self.file)
@@ -159,6 +192,35 @@ def print_dbscan(self) -> None:
159192
10 [1, 10, 11] ---> Noise ---> Border
160193
11 [2, 10, 11, 12] ---> Core
161194
12 [9, 11, 12] ---> Noise ---> Border
195+
196+
>>> DbScan(5,2.5).print_dbscan()
197+
1 [1, 2, 10, 11] ---> Noise ---> Border
198+
2 [1, 2, 3, 10, 11] ---> Core
199+
3 [2, 3, 4, 11] ---> Noise ---> Border
200+
4 [3, 4, 5, 6, 7, 8] ---> Core
201+
5 [4, 5, 6, 7, 8] ---> Core
202+
6 [4, 5, 6, 7] ---> Noise ---> Border
203+
7 [4, 5, 6, 7, 8] ---> Core
204+
8 [4, 5, 7, 8] ---> Noise ---> Border
205+
9 [9, 11, 12] ---> Noise ---> Border
206+
10 [1, 2, 10, 11, 12] ---> Core
207+
11 [1, 2, 3, 9, 10, 11, 12] ---> Core
208+
12 [9, 10, 11, 12] ---> Noise ---> Border
209+
210+
>>> DbScan(2,0.5).print_dbscan()
211+
1 [1] ---> Noise
212+
2 [2] ---> Noise
213+
3 [3] ---> Noise
214+
4 [4] ---> Noise
215+
5 [5] ---> Noise
216+
6 [6] ---> Noise
217+
7 [7] ---> Noise
218+
8 [8] ---> Noise
219+
9 [9] ---> Noise
220+
10 [10] ---> Noise
221+
11 [11] ---> Noise
222+
12 [12] ---> Noise
223+
162224
"""
163225
for i in self.dict1:
164226
print(i, " ", self.dict1[i], end=" ---> ")
@@ -185,6 +247,13 @@ def plot_dbscan(self) -> None:
185247
186248
>>> DbScan(4,1.9).plot_dbscan()
187249
Plotted Successfully
250+
251+
>>> DbScan(5,2.5).plot_dbscan()
252+
Plotted Successfully
253+
254+
>>> DbScan(5,2.5).plot_dbscan()
255+
Plotted Successfully
256+
188257
"""
189258
if type(self.file) is str:
190259
data = pd.read_csv(self.file)
@@ -214,10 +283,12 @@ def plot_dbscan(self) -> None:
214283
ha="center",
215284
va="bottom",
216285
)
286+
core_legend = mpatches.Patch(color="red", label="Core")
287+
noise_legend = mpatches.Patch(color="green", label="Noise")
217288
plt.xlabel("X")
218289
plt.ylabel("Y")
219290
plt.title("DBSCAN Clustering")
220-
plt.legend(["Core", "Noise"])
291+
plt.legend(handles=[core_legend, noise_legend])
221292
plt.show()
222293
print("Plotted Successfully")
223294

0 commit comments

Comments
 (0)
Please sign in to comment.