From 12ac966b635e3b33b93ddbfc75799345126e62b8 Mon Sep 17 00:00:00 2001 From: tkgowtham Date: Tue, 1 Oct 2024 21:20:12 +0530 Subject: [PATCH] Update dbscan.py --- machine_learning/dbscan.py | 91 ++++++++++++-------------------------- 1 file changed, 28 insertions(+), 63 deletions(-) diff --git a/machine_learning/dbscan.py b/machine_learning/dbscan.py index 8bc9bf92b..9b5f76456 100644 --- a/machine_learning/dbscan.py +++ b/machine_learning/dbscan.py @@ -2,10 +2,8 @@ import pandas as pd import math import matplotlib.pyplot as plt from typing import dict, list - - -class dbScan: - """ +class DbScan: + ''' DBSCAN Algorithm : Density-Based Spatial Clustering Of Applications With Noise Refer this website for more details : https://en.wikipedia.org/wiki/DBSCAN @@ -20,13 +18,12 @@ class dbScan: To create a object ------------------ import dbscan - obj = dbscan.dbscan(minpts, radius, file) + obj = dbscan.DbScan(minpts, radius, file) obj.print_dbscan() obj.plot_dbscan() - """ - - def __init__(self, minpts: int, radius: int, file: str) -> None: - """ + ''' + def __init__(self, minpts : int, radius : int, file : str) -> None: + ''' Constructor Attributes: @@ -54,16 +51,13 @@ class dbScan: 6 | 4 7 | 3 ----- - """ + ''' self.minpts = minpts self.radius = radius self.file = file self.dict1 = self.perform_dbscan() - def perform_dbscan(self) -> dict[int, list[int]]: - """ - >>>perform_dbscan() - + ''' Parameters: ----------- None @@ -72,85 +66,56 @@ class dbScan: -------- Dictionary with points and the list of points that lie in its radius - """ + ''' data = pd.read_csv(self.file) - - minpts = self.minpts e = self.radius - dict1 = {} for i in range(len(data)): for j in range(len(data)): - dist = math.sqrt( - pow(data["x"][j] - data["x"][i], 2) - + pow(data["y"][j] - data["y"][i], 2) - ) + dist = math.sqrt(pow(data['x'][j] - data['x'][i],2) + pow(data['y'][j] - data['y'][i],2)) if dist < e: - if i + 1 in dict1: - dict1[i + 1].append(j + 1) + if i+1 in dict1: + dict1[i+1].append(j+1) else: - dict1[i + 1] = [ - j + 1, - ] - + dict1[i+1] = [j+1,] return dict1 - def print_dbscan(self) -> None: - """ + ''' Outputs: -------- Prints each point and if it is a core or a noise (w/ border) - """ + ''' for i in self.dict1: - print(i, " ", self.dict1[i], end=" ---> ") + print(i," ",self.dict1[i], end=' ---> ') if len(self.dict1[i]) >= self.minpts: print("Core") else: for j in self.dict1: - if ( - i != j - and len(self.dict1[j]) >= self.minpts - and i in self.dict1[j] - ): + if i != j and len(self.dict1[j]) >= self.minpts and i in self.dict1[j]: print("Noise ---> Border") break else: print("Noise") - def plot_dbscan(self) -> None: - """ + ''' Output: ------- A matplotlib plot that show points as core and noise along with the circle that lie within it. - """ + ''' data = pd.read_csv(self.file) e = self.radius for i in self.dict1: if len(self.dict1[i]) >= self.minpts: - plt.scatter(data["x"][i - 1], data["y"][i - 1], color="red") - circle = plt.Circle( - (data["x"][i - 1], data["y"][i - 1]), e, color="blue", fill=False - ) + plt.scatter(data['x'][i-1], data['y'][i-1], color='red') + circle = plt.Circle((data['x'][i-1], data['y'][i-1]), e, color='blue', fill=False) plt.gca().add_artist(circle) - plt.text( - data["x"][i - 1], - data["y"][i - 1], - "P" + str(i), - ha="center", - va="bottom", - ) + plt.text(data['x'][i-1], data['y'][i-1], 'P'+str(i), ha='center', va='bottom') else: - plt.scatter(data["x"][i - 1], data["y"][i - 1], color="green") - plt.text( - data["x"][i - 1], - data["y"][i - 1], - "P" + str(i), - ha="center", - va="bottom", - ) - plt.xlabel("X") - plt.ylabel("Y") - plt.title("DBSCAN Clustering") - plt.legend(["Core", "Noise"]) + plt.scatter(data['x'][i-1], data['y'][i-1], color='green') + plt.text(data['x'][i-1], data['y'][i-1], 'P'+str(i), ha='center', va='bottom') + plt.xlabel('X') + plt.ylabel('Y') + plt.title('DBSCAN Clustering') + plt.legend(['Core','Noise']) plt.show()