7
7
8
8
import math
9
9
10
+ import matplotlib .patches as mpatches
10
11
import matplotlib .pyplot as plt
11
12
import pandas as pd
12
13
@@ -118,6 +119,38 @@ def perform_dbscan(self) -> dict[int, list[int]]:
118
119
11 [2, 10, 11, 12]
119
120
12 [9, 11, 12]
120
121
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
+
121
154
"""
122
155
if type (self .file ) is str :
123
156
data = pd .read_csv (self .file )
@@ -159,6 +192,35 @@ def print_dbscan(self) -> None:
159
192
10 [1, 10, 11] ---> Noise ---> Border
160
193
11 [2, 10, 11, 12] ---> Core
161
194
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
+
162
224
"""
163
225
for i in self .dict1 :
164
226
print (i , " " , self .dict1 [i ], end = " ---> " )
@@ -185,6 +247,13 @@ def plot_dbscan(self) -> None:
185
247
186
248
>>> DbScan(4,1.9).plot_dbscan()
187
249
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
+
188
257
"""
189
258
if type (self .file ) is str :
190
259
data = pd .read_csv (self .file )
@@ -214,10 +283,12 @@ def plot_dbscan(self) -> None:
214
283
ha = "center" ,
215
284
va = "bottom" ,
216
285
)
286
+ core_legend = mpatches .Patch (color = "red" , label = "Core" )
287
+ noise_legend = mpatches .Patch (color = "green" , label = "Noise" )
217
288
plt .xlabel ("X" )
218
289
plt .ylabel ("Y" )
219
290
plt .title ("DBSCAN Clustering" )
220
- plt .legend ([ "Core" , "Noise" ])
291
+ plt .legend (handles = [ core_legend , noise_legend ])
221
292
plt .show ()
222
293
print ("Plotted Successfully" )
223
294
0 commit comments