mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-15 02:59:50 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
cd539e378d
commit
5e148f524d
@ -1,18 +1,19 @@
|
|||||||
'''
|
"""
|
||||||
|
|
||||||
Author : Gowtham Kamalasekar
|
Author : Gowtham Kamalasekar
|
||||||
LinkedIn : https://www.linkedin.com/in/gowtham-kamalasekar/
|
LinkedIn : https://www.linkedin.com/in/gowtham-kamalasekar/
|
||||||
|
|
||||||
'''
|
"""
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from typing import dict, list
|
from typing import dict, list
|
||||||
|
|
||||||
class DbScan:
|
|
||||||
|
|
||||||
'''
|
class DbScan:
|
||||||
|
"""
|
||||||
DBSCAN Algorithm :
|
DBSCAN Algorithm :
|
||||||
Density-Based Spatial Clustering Of Applications With Noise
|
Density-Based Spatial Clustering Of Applications With Noise
|
||||||
Refer this website for more details : https://en.wikipedia.org/wiki/DBSCAN
|
Refer this website for more details : https://en.wikipedia.org/wiki/DBSCAN
|
||||||
@ -32,14 +33,28 @@ class DbScan:
|
|||||||
obj = dbscan.DbScan(minpts, radius, file)
|
obj = dbscan.DbScan(minpts, radius, file)
|
||||||
obj.print_dbscan()
|
obj.print_dbscan()
|
||||||
obj.plot_dbscan()
|
obj.plot_dbscan()
|
||||||
'''
|
"""
|
||||||
def __init__(self, minpts : int, radius : int, file : str =
|
|
||||||
({'x': 3, 'y': 7}, {'x': 4, 'y': 6}, {'x': 5, 'y': 5},
|
def __init__(
|
||||||
{'x': 6, 'y': 4},{'x': 7, 'y': 3}, {'x': 6, 'y': 2},
|
self,
|
||||||
{'x': 7, 'y': 2}, {'x': 8, 'y': 4},{'x': 3, 'y': 3},
|
minpts: int,
|
||||||
{'x': 2, 'y': 6}, {'x': 3, 'y': 5}, {'x': 2, 'y': 4})
|
radius: int,
|
||||||
) -> None:
|
file: str = (
|
||||||
'''
|
{"x": 3, "y": 7},
|
||||||
|
{"x": 4, "y": 6},
|
||||||
|
{"x": 5, "y": 5},
|
||||||
|
{"x": 6, "y": 4},
|
||||||
|
{"x": 7, "y": 3},
|
||||||
|
{"x": 6, "y": 2},
|
||||||
|
{"x": 7, "y": 2},
|
||||||
|
{"x": 8, "y": 4},
|
||||||
|
{"x": 3, "y": 3},
|
||||||
|
{"x": 2, "y": 6},
|
||||||
|
{"x": 3, "y": 5},
|
||||||
|
{"x": 2, "y": 4},
|
||||||
|
),
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
Constructor
|
Constructor
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -67,13 +82,14 @@ class DbScan:
|
|||||||
6 | 4
|
6 | 4
|
||||||
7 | 3
|
7 | 3
|
||||||
-----
|
-----
|
||||||
'''
|
"""
|
||||||
self.minpts = minpts
|
self.minpts = minpts
|
||||||
self.radius = radius
|
self.radius = radius
|
||||||
self.file = file
|
self.file = file
|
||||||
self.dict1 = self.perform_dbscan()
|
self.dict1 = self.perform_dbscan()
|
||||||
|
|
||||||
def perform_dbscan(self) -> dict[int, list[int]]:
|
def perform_dbscan(self) -> dict[int, list[int]]:
|
||||||
'''
|
"""
|
||||||
Args:
|
Args:
|
||||||
-----------
|
-----------
|
||||||
None
|
None
|
||||||
@ -99,7 +115,7 @@ class DbScan:
|
|||||||
11 [2, 10, 11, 12]
|
11 [2, 10, 11, 12]
|
||||||
12 [9, 11, 12]
|
12 [9, 11, 12]
|
||||||
|
|
||||||
'''
|
"""
|
||||||
if type(self.file) is str:
|
if type(self.file) is str:
|
||||||
data = pd.read_csv(self.file)
|
data = pd.read_csv(self.file)
|
||||||
else:
|
else:
|
||||||
@ -108,16 +124,21 @@ class DbScan:
|
|||||||
dict1 = {}
|
dict1 = {}
|
||||||
for i in range(len(data)):
|
for i in range(len(data)):
|
||||||
for j in range(len(data)):
|
for j in range(len(data)):
|
||||||
dist = math.sqrt(pow(data['x'][j] - data['x'][i],2)
|
dist = math.sqrt(
|
||||||
+ pow(data['y'][j] - data['y'][i],2))
|
pow(data["x"][j] - data["x"][i], 2)
|
||||||
|
+ pow(data["y"][j] - data["y"][i], 2)
|
||||||
|
)
|
||||||
if dist < e:
|
if dist < e:
|
||||||
if i+1 in dict1:
|
if i + 1 in dict1:
|
||||||
dict1[i+1].append(j+1)
|
dict1[i + 1].append(j + 1)
|
||||||
else:
|
else:
|
||||||
dict1[i+1] = [j+1,]
|
dict1[i + 1] = [
|
||||||
|
j + 1,
|
||||||
|
]
|
||||||
return dict1
|
return dict1
|
||||||
|
|
||||||
def print_dbscan(self) -> None:
|
def print_dbscan(self) -> None:
|
||||||
'''
|
"""
|
||||||
Outputs:
|
Outputs:
|
||||||
--------
|
--------
|
||||||
Prints each point and if it is a core or a noise (w/ border)
|
Prints each point and if it is a core or a noise (w/ border)
|
||||||
@ -135,9 +156,9 @@ class DbScan:
|
|||||||
10 [1, 10, 11] ---> Noise ---> Border
|
10 [1, 10, 11] ---> Noise ---> Border
|
||||||
11 [2, 10, 11, 12] ---> Core
|
11 [2, 10, 11, 12] ---> Core
|
||||||
12 [9, 11, 12] ---> Noise ---> Border
|
12 [9, 11, 12] ---> Noise ---> Border
|
||||||
'''
|
"""
|
||||||
for i in self.dict1:
|
for i in self.dict1:
|
||||||
print(i," ",self.dict1[i], end=' ---> ')
|
print(i, " ", self.dict1[i], end=" ---> ")
|
||||||
if len(self.dict1[i]) >= self.minpts:
|
if len(self.dict1[i]) >= self.minpts:
|
||||||
print("Core")
|
print("Core")
|
||||||
else:
|
else:
|
||||||
@ -151,8 +172,9 @@ class DbScan:
|
|||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print("Noise")
|
print("Noise")
|
||||||
|
|
||||||
def plot_dbscan(self) -> None:
|
def plot_dbscan(self) -> None:
|
||||||
'''
|
"""
|
||||||
Output:
|
Output:
|
||||||
-------
|
-------
|
||||||
A matplotlib plot that show points as core and noise along
|
A matplotlib plot that show points as core and noise along
|
||||||
@ -160,7 +182,7 @@ class DbScan:
|
|||||||
|
|
||||||
>>> DbScan(4,1.9).plot_dbscan()
|
>>> DbScan(4,1.9).plot_dbscan()
|
||||||
Plotted Successfully
|
Plotted Successfully
|
||||||
'''
|
"""
|
||||||
if type(self.file) is str:
|
if type(self.file) is str:
|
||||||
data = pd.read_csv(self.file)
|
data = pd.read_csv(self.file)
|
||||||
else:
|
else:
|
||||||
@ -168,23 +190,36 @@ class DbScan:
|
|||||||
e = self.radius
|
e = self.radius
|
||||||
for i in self.dict1:
|
for i in self.dict1:
|
||||||
if len(self.dict1[i]) >= self.minpts:
|
if len(self.dict1[i]) >= self.minpts:
|
||||||
plt.scatter(data['x'][i-1], data['y'][i-1], color='red')
|
plt.scatter(data["x"][i - 1], data["y"][i - 1], color="red")
|
||||||
circle = plt.Circle((data['x'][i-1], data['y'][i-1]),
|
circle = plt.Circle(
|
||||||
e, color='blue', fill=False)
|
(data["x"][i - 1], data["y"][i - 1]), e, color="blue", fill=False
|
||||||
|
)
|
||||||
plt.gca().add_artist(circle)
|
plt.gca().add_artist(circle)
|
||||||
plt.text(data['x'][i-1], data['y'][i-1],
|
plt.text(
|
||||||
'P'+str(i), ha='center', va='bottom')
|
data["x"][i - 1],
|
||||||
|
data["y"][i - 1],
|
||||||
|
"P" + str(i),
|
||||||
|
ha="center",
|
||||||
|
va="bottom",
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
plt.scatter(data['x'][i-1], data['y'][i-1], color='green')
|
plt.scatter(data["x"][i - 1], data["y"][i - 1], color="green")
|
||||||
plt.text(data['x'][i-1], data['y'][i-1],
|
plt.text(
|
||||||
'P'+str(i), ha='center', va='bottom')
|
data["x"][i - 1],
|
||||||
plt.xlabel('X')
|
data["y"][i - 1],
|
||||||
plt.ylabel('Y')
|
"P" + str(i),
|
||||||
plt.title('DBSCAN Clustering')
|
ha="center",
|
||||||
plt.legend(['Core','Noise'])
|
va="bottom",
|
||||||
|
)
|
||||||
|
plt.xlabel("X")
|
||||||
|
plt.ylabel("Y")
|
||||||
|
plt.title("DBSCAN Clustering")
|
||||||
|
plt.legend(["Core", "Noise"])
|
||||||
plt.show()
|
plt.show()
|
||||||
print("Plotted Successfully")
|
print("Plotted Successfully")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user