<jupyter_start><jupyter_text>***Unsupervised Learning*** **Clustering**Clustering is the task of dividing the population or data points into a number of groups such that data points in the same groups are more similar to other data points in the same group and dissimilar to the data points in other groups. It is basically a collection of objects on the basis of similarity and dissimilarity between them. **DBSCAN**Density-based spatial clustering of applications with noise (DBSCAN) is a well-known data clustering algorithm that is commonly used in data mining and machine learning.Based on a set of points (let’s think in a bidimensional space as exemplified in the figure), DBSCAN groups together points that are close to each other based on a distance measurement (usually Euclidean distance) and a minimum number of points. It also marks as outliers the points that are in low-density regions.  **Getting Started with DBSCAN Clustering**<jupyter_code>#Importing Libraries
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from sklearn.neighbors import NearestNeighbors
from sklearn import datasets
import numpy as np<jupyter_output><empty_output><jupyter_text>**Loading Dataset**<jupyter_code>x,y = datasets.make_moons(n_samples=2500, noise= 0.05)

x1 = x[:,0]
x2 = x[:,1]<jupyter_output><empty_output><jupyter_text>**Visualization of dataset**<jupyter_code>plt.scatter(x1,x2,s=5)
plt.show()<jupyter_output><empty_output><jupyter_text>**Finding an Optimal value of Eps**<jupyter_code>neigh = NearestNeighbors(n_neighbors=2)
nbrs = neigh.fit(x)
distances, indices = nbrs.kneighbors(x)
distances = np.sort(distances, axis=0)
distances = distances[:,1]
plt.plot(distances)<jupyter_output><empty_output><jupyter_text>**Training with DBSCAN Model**<jupyter_code>model = DBSCAN(eps = 0.050)
model.fit(x)
y_pred = model.labels_.astype(np.int)<jupyter_output><empty_output><jupyter_text>**Visualization of Clusters**<jupyter_code>colors = np.array(['#ff0000','#00ff00'])
print("Clusters:\n")
plt.scatter(x1,x2,s=5,color = colors [y_pred])
plt.show()<jupyter_output>Clusters:
{'fraction_non_alphanumeric': 0.09947393591582974, 'fraction_numerical': 0.015303682448589193, 'mean_word_length': 7.334661354581673, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '262918', 'license': 'MIT', 'n_tokens_mistral': 709, 'n_tokens_neox': 670, 'n_words': 208, 'path': '16_DBSCAN_Clustering/16_DBSCAN_Clustering.ipynb', 'repo_name': 'mhuzaifadev/mlzero_to_hero', 'size': '2091'}