0% found this document useful (0 votes)
9 views5 pages

DBSCAN Clustering in Python Code

This document details an experiment on Density-based spatial clustering (DBSCAN) conducted by Anvita Singh. It includes Python code for data preprocessing, applying PCA for dimensionality reduction, and implementing the DBSCAN algorithm to identify clusters and noise in a dataset. The results are visualized using scatter plots and count plots to illustrate the clustering output.

Uploaded by

Anvita Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

DBSCAN Clustering in Python Code

This document details an experiment on Density-based spatial clustering (DBSCAN) conducted by Anvita Singh. It includes Python code for data preprocessing, applying PCA for dimensionality reduction, and implementing the DBSCAN algorithm to identify clusters and noise in a dataset. The results are visualized using scatter plots and count plots to illustrate the clustering output.

Uploaded by

Anvita Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Pattern Recognition & Anomaly Detection

Lab
EXPERIMENT – 12
Density-based spatial clustering(DBSCAN)

NAME – ANVITA SINGH

ROLL NO – R2142221063

SAP_ID – 500107712

BATCH – 8

CODE -
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

from sklearn.model_selection import train_test_split


from [Link] import StandardScaler
from [Link] import PCA
from [Link] import DBSCAN
from [Link] import confusion_matrix, ConfusionMatrixDisplay

# Load dataset
df = pd.read_csv("/content/city_day.csv")
print("Initial Data Sample:")
print([Link]())

# Remove missing values


[Link](inplace=True)

# Feature Selection (only numeric columns)


X = df.select_dtypes(include=['float64', 'int64'])
# Feature Scaling
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Apply PCA for dimensionality reduction


pca = PCA(n_components=2) # You can choose the number of components (2
for 2D, or more for higher dimensions)
X_pca = pca.fit_transform(X_scaled)

# Explained variance ratio


print("\nExplained Variance Ratio of the PCA Components:")
print(pca.explained_variance_ratio_)

# Train-Test Split (optional for DBSCAN, but we'll do it for


visualization)
X_train, X_test = train_test_split(X_pca, test_size=0.2,
random_state=42)

# DBSCAN Model
dbscan = DBSCAN(eps=0.5, min_samples=5) # You can adjust eps and
min_samples based on your data
[Link](X_train)

# Predict Clusters
y_pred_train = dbscan.labels_ # DBSCAN assigns labels, where -1
represents noise (outliers)

# Show sample predictions


print("\nSample DBSCAN Clusters (Noise = -1, Clusters = 0, 1,
2, ...):")
print(y_pred_train[:10])

# Count of Clusters vs Noise


unique, counts = [Link](y_pred_train, return_counts=True)
result_counts = dict(zip(unique, counts))

print("\nCluster Counts:")
print(result_counts)

# Visualize Clusters and Noise


[Link](figsize=(8,5))
[Link](x=y_pred_train)
[Link]("DBSCAN Clustering Output")
[Link]("Cluster/Noise")
[Link]("Count")
[Link]()
# Visualizing the PCA-reduced data with clusters highlighted
[Link](figsize=(10, 6))
[Link](x=X_train[:, 0], y=X_train[:, 1], hue=y_pred_train,
palette="coolwarm", style=y_pred_train, legend="full")
[Link]("DBSCAN Clustering on PCA-reduced Data (Train Set)")
[Link]("Principal Component 1")
[Link]("Principal Component 2")
[Link]()

# Visualizing the test set with clusters


y_pred_test = dbscan.fit_predict(X_test) # DBSCAN on the test set

# Visualizing the PCA-reduced data with anomalies (clusters)


highlighted for the test set
[Link](figsize=(10, 6))
[Link](x=X_test[:, 0], y=X_test[:, 1], hue=y_pred_test,
palette="coolwarm", style=y_pred_test, legend="full")
[Link]("DBSCAN Clustering on PCA-reduced Data (Test Set)")
[Link]("Principal Component 1")
[Link]("Principal Component 2")
[Link]()

print("✅ DBSCAN Clustering Model Trained, Clusters Identified, and


Visualized.")

OUTPUT –

Common questions

Powered by AI

The explained variance ratio from PCA indicates how much of the total variance in the data is captured by each principal component. Higher explained variance ratios suggest that the component captures more significant patterns in the data. By examining these ratios, one can decide to retain only the components that together capture a substantial portion of the variance, ensuring that important information is maintained while reducing dimensionality for clustering .

Feature scaling impacts the performance of DBSCAN by ensuring that all features contribute equally to the distance calculations that determine neighborhood relations. Without scaling, features with larger ranges can disproportionately affect the clustering outcome, potentially skewing the results. Scaling normalizes these differences, allowing DBSCAN to more accurately detect clusters based solely on the inherent structure of the data .

The key parameters of the DBSCAN algorithm are 'eps' and 'min_samples'. 'eps' defines the maximum distance between two samples to be considered as neighbors, and 'min_samples' indicates the minimum number of points required to form a dense region. These parameters influence the clustering outcome by determining how clusters are formed: a small 'eps' may lead to more noise being identified, while a larger 'eps' can merge distinct clusters; similarly, a higher 'min_samples' can prevent small, potentially insignificant clusters from forming .

Dividing data into train and test sets when implementing DBSCAN can provide insights into the algorithm's performance on unseen data. A key benefit is validating the generalization of the identified clusters. However, since DBSCAN does not rely on a deterministic training phase, this split is not always typical for unsupervised learning, potentially leading to misinterpretation. A drawback could be the loss of information from not applying clustering on the entire dataset, which might impact the robustness of the clustering result .

Adjusting 'eps' and 'min_samples' for your study data is significant because these parameters are sensitive to the data’s density and distribution. Different datasets might have varying densities and clustering structures, making it essential to fine-tune these parameters to accurately identify clusters and filter noise. Incorrect settings could lead to misidentification of clusters or an excess of noise points, therefore tuning is crucial for effective analysis .

Using a countplot to visualize DBSCAN results helps in understanding the distribution of data points across different clusters, including noise. By displaying the number of points in each identified cluster or noise, it provides a quick summary of how dense or sparse each identified group is, aiding in interpreting the clustering outcome and detecting potential outliers or noise .

PCA is applied before DBSCAN clustering to reduce the dimensionality of the data, making it easier to visualize and process. By transforming the data into a lower-dimensional space while preserving as much variance as possible, PCA helps to improve the computational efficiency and may enhance the cluster separation, which is critical for DBSCAN's neighbor-based approach .

Removing missing values before conducting DBSCAN clustering is important because missing data can distort the distance calculations, leading to incorrect neighborhood structures. This can result in erroneous cluster assignment, particularly because DBSCAN relies heavily on the density and spatial configuration of data points. By preprocessing and removing missing values, the integrity and accuracy of the clustering process are maintained .

Visualizing DBSCAN clustering results, especially after PCA, is critical because it allows for the assessment of the separation and density of clusters. Visualization helps in confirming whether the PCA-reduced dimensions effectively capture the cluster structures and if DBSCAN parameters are appropriately set to detect these clusters. Without visualization, interpreting the complex multi-dimensional relationships within the data would be more challenging .

DBSCAN algorithm handles noise by assigning a label of -1 to data points that do not belong to any clusters. These are identified as outliers or noise. This is beneficial because it allows the algorithm to differentiate between points that are genuinely part of a cluster and those that are anomalous, ensuring more accurate and meaningful clustering results .

You might also like