Showing posts with label DBSCAN. Show all posts
Showing posts with label DBSCAN. Show all posts

Tuesday, February 20, 2024

DBSCAN IN MACHINE LEARNING/PYTHON/ARTIFICIAL INTELLIGENCE

 DBSCAN

  • Clustering Techniques Overview
  • DBSCAN Algorithm Steps
  • Density-Based Clustering Methods
  • Evaluation Metrics for DBSCAN
  • Advantages of DBSCAN
  • Disadvantages of DBSCAN
  • Summary of DBSCAN

Clustering with dbscan is a method for unsupervised learning that groups data items according to their patterns. Various clustering methods exist, utilizing differential evolution techniques. Fundamentally, all clustering methods follow a similar approach by initially computing similarities and then using this information to group data points sharing similar attributes or properties. The DBSCAN approach, or Density-based Spatial Clustering of Applications with Noise, is the main focus of this field.

Clusters represent regions with dense concentrations of data points, delineated by areas where point density is comparatively lower. DBSCAN operates on the fundamental concepts of "clusters" and "noise." It employs a local cluster criterion, particularly density-connected points.

DBSCAN, or Density-Based Spatial Clustering of Applications with Noise, categorizes data points according to their local density within a defined radius ε. Points situated in areas with low density between two clusters are classified as noise. The ε neighborhood of an object refers to the region within a distance ε from that object. A core object is identified if its ε neighborhood includes at least a specified minimum number, MinPts, of other objects.

Why DBSCAN?

Approaches like as hierarchical clustering and partitioning are effective when used to convex or spherically shaped clusters. Therefore, it can be concluded that the method proves effective for datasets featuring tightly clustered and distinct groups. Moreover, the presence of outliers and noise within the dataset significantly influences the results.

In real life, the data may contain irregularities, like:

  • Clusters can be of any shape, we see the below image.
  • Data may have noise.
Image source original

In the provided image, the dataset exhibits clusters with non-convex shapes and includes outlier points. Finding clusters accurately in datasets with outliers and unevenly shaped data is a challenge for the k-means method.

Real-world Example for DBSCAN

Let's Assume that a megacity has a downtown and many buildings. This downtown is famous for having a wide variety of shops, from busy boutiques to attractive cafes. But the district's appeal increased along with the difficulty of controlling its ever-changing surface..

To face these challenges we hired Alex, a skilled urban planner, his work is to revitalize the downtown plaza. Knowing that he needed a more data-driven approach, Alex turned to the DBSCAN clustering algorithm to analyze the district’s spatial data.

Alex started by collecting data on various aspects of Downtown Plaza including foot traffic, business types, and customer demographics, after gaining this information, Alex applied DBSCAN to identify the cluster of similar businesses and areas with high foot traffic.

By using DBSCAN, Alex discovered several distinct clusters within the downtown plaza. He found a cluster of trendy cafes and eateries, a cluster of boutique shops, and even a cluster of street performers and artists. Additionally, DBSCAN highlighted bustling intersections and pedestrian hotspots where foot traffic was highest.

Using this information, Alex now can develop a strategic plan to enhance Downtown Plaza. He proposed pedestrian-friendly zones around high-traffic areas, encouraging outdoor seating and street performances. He also suggested promoting collaboration among businesses with the same clusters, fostering a sense of community and synergy. here we study about dbscan clustering example.

Parameters Required For DBSCAN Algorithm

EPS: This parameter is used to specify the near vicinity of a data point. Two points that are separated by 'eps' or less are said to be in the same neighborhood. Selecting a low value for eps may cause a sizable percentage of the data to be regarded as outliers. On the other hand, clusters may combine if eps is too big, resulting in the majority of data points falling into a single cluster. Using the k-distance graph is one method for calculating the eps value.

MinPts: MinPts represents the least number of neighboring data points within the specified eps radius. When dealing with larger datasets, it's advisable to choose a higher value for MinPts. Typically, MinPts is found by ensuring that it is more than or equal to D+1. This is done by using the dimension count (D) of the dataset. For MinPts, a minimum value of 3 is typically seen to be ideal.

In the algorithm there are 3 different verities of data points they are:

Core point: A data point is deemed a core point if its epsilon (ε) radius contains at least MinPts surrounding points.

Border Point: A border point is characterized as having fewer surrounding points inside its epsilon (ε) radius than MinPts, but sharing an ε-neighborhood with a core point.

Noise or outliers: it is a point that is not a core point or border point.

Steps used in the DBSCAN algorithm

  1. Initially, the algorithm identifies all neighboring points located within the ε radius of each data point and identifies core points, which are those with more than MinPts neighbors.
  2. To house core points that are not presently assigned to a different cluster, develop a fresh cluster.
  3. Next, the algorithm proceeds recursively to identify all density-connected points that are linked to the core points. The density link between points 'a' and 'b' is formed when there is another point 'c' nearby with enough neighbors and both 'a' and 'b' are within the distance of ε. Based on the assumption that 'b' is a neighbor of 'a' if 'c' is a neighbor of 'd', 'e' is a neighbor of 'b,' and so on, a hierarchical structure is formed.
  4. After that, the program looks at the dataset's remaining unexplored locations. Any points that were not assigned to a cluster in the previous operations are referred to as "noise".

Pseudocode for DBSCAN clustering algorithm

below is the Pseudocode for dbscan this pseudocode can be used for dbscan clustering algorithm Python code development

DBSCAN (dataset, eps, MinPts){

# cluster index

For each unvisited point p in the dataset {

            Mark p as visited

            # find neighbors

            Neighbors N = find a neighboring points of p

            If |N| >= MinPts:

                        N = N U N' (N union N')

                        If p' is not a member of any cluster:

                                    Add p' to cluster C

}

Major features of Density-Based Clustering

  • Density-based clustering is a scan method.
  • It needs density parameters as a termination.
  • It is used to manage noise in data clusters.
  • Density-based clustering is used to identify clusters of different sizes.

Density-Based Clustering Methods

The DBSCAN algorithm depends on a density-based notion of cluster. It can identify clusters of different sizes in the spatial database which can have outliers.


Image source original

OPTICS, short for Ordering Points To Identify the Clustering Structure, is instrumental in delineating the density-based clustering structure within a dataset. It sequentially organizes the database, highlighting the density-based clustering patterns under different parameter settings. OPTICS techniques are beneficial for both automated and interactive cluster analysis tasks, facilitating the detection of inherent clustering patterns within the data.

DENCLUE, developed by Hinnebirg and Kiem, is another density-based clustering method. It offers a concise mathematical representation of clusters with irregular shapes within high-dimensional data sets. Particularly useful for datasets containing substantial noise, DENCLUE excels in describing arbitrarily shaped clusters amidst noisy data

Evaluation Metrics For DBSCAN Algorithm In Machine Learning

We assess the effectiveness of the DBSCAN clustering approach using the Silhouette score and the Adjusted Rand score. A data point is considered well-clustered if its Silhouette score is about 1 (ranging from -1 to 1), which indicates that it is close to points within its own cluster and far from points outside of it. Conversely, a score close to -1 indicates poorly grouped data, and a result close to 0 indicates an overlapping cluster. Lastly, a Rand score, absolute or modified, ranges from zero to one. The below points signify exceptional recovery, whereas scores above 0.8 imply very good recovery. When the score is less than 0.5 the cluster recovery is poor.

When should we use DBSCAN Over K-Means In Clustering Analysis?

When we are not certain about the cluster's number then we should use DBSCAN over K-means because K-means need predefined numbers for clusters. DBSCAN can manage arbitrary form clusters far better than k-means, hence we should also utilize it when we are unsure about the cluster's shape. Furthermore, as DBSCAN can handle outliers and noise far better, we should also utilize it when the data contains a lot of them.

Advantages of DBSCAN

DBSCAN has several advantages:

  • Robust to outliers: DBSCAN can handle noise and outliers effectively. It doesn’t force data points into clusters if they don’t fit well.
  • DBSCAN does not require cluster count to be pre-specified. Different from K-means, DBSCAN finds clusters based on data density rather than requiring a set number. This flexibility allows DBSCAN to adapt to the inherent structure of the data without imposing a fixed cluster count.
  • DBSCAN excels at handling clusters with diverse shapes and sizes. Unlike certain algorithms limited to spherical clusters, DBSCAN can effectively detect clusters of arbitrary shapes and densities. This versatility allows it to capture complex patterns present in the data, irrespective of their geometric configuration.
  • Flexibility in defining clusters: DBSCAN doesn’t assume clusters to be globular or with a specific shape. It identifies clusters based on density-reachability, allowing for more flexible cluster definitions that make cluster dbscan.
  • Works well with varying densities: it can identify clusters of varying densities, adapting to regions of higher and lower density in the data.
  • Efficient in processing large databases: it’s scalable and efficient in handling large datasets by focusing on the neighborhood of each point rather than the entire dataset.
  • Minimal sensitivity to parameter choices: while it has parameters like epsilon and minimum points, their values can often be chosen based on domain knowledge or heuristics without significantly impacting results.
  • Natural handling of noise: it explicitly labels points that do not belong to any cluster as noise, proving a clear distinction between actual clusters and noisy data.
  • Good for discovering clusters of complex shapes: Especially useful in scenarios where clusters might not be easily separable or have convoluted shapes in higher-dimensional spaces.

Disadvantages of DBSCAN

  • One important feature of DBSCAN is parameter sensitivity; its efficacy depends on the careful choice of parameters such as epsilon (ε) and the minimal number of points required to form a cluster (minPts). Mistakes in parameter selection might result in less-than-ideal segmentation results, sometimes leaving clusters either too separated or too fragmented. Thus, finding the right balance between these parameters is crucial for achieving desirable clustering results.
  • Difficulty with varying density and high-dimensional data: DBSCAN may struggle with datasets where clusters have varying densities or when dealing with high-dimensional data. It might not perform optimally in these scenarios without careful parameter tuning.
  • Struggles with very large datasets: while it’s efficient in handling large datasets, extremely large datasets might pose computational challenges, especially in situations where the data does not have clear density separations.
  • Requires careful preprocessing: prior normalization and scaling of data might be necessary for DBSCAN to perform effectively, as it calculates distances between points. Inaccuracies in these steps might affect clustering results.
  • Difficulty handling data of uniform density: in cases where the data is uniformly dense, DBSCAN might not be able to distinguish meaningful clusters from noise effectively.
  • Not ideal for clusters with varying densities and sizes: while it’s robust to arbitrary shapes, clusters with significantly different densities or sizes might be challenging for DBSCAN to cluster effectively.
  • DBSCAN's performance can be influenced by the selection of the distance metric used, such as Euclidean distance. Different datasets may necessitate the use of specific distance measures tailored to their characteristics. This sensitivity to the choice of distance metric can introduce variations in the clustering outcomes produced by DBSCAN, emphasizing the importance of selecting an appropriate distance measure to suit the dataset's characteristics.

Applications

DBSCAN applications are:

Cluster Analysis: DBSCAN is widely used in cluster analysis, a subfield of data mining and machine learning. It is capable of identifying any size and shape of cluster in geographical datasets.

DBSCAN Anomaly Detection: DBSCAN may be used to examine datasets for anomalies or points that are not part of any cluster or are situated in areas with low population density. It reliably identifies outliers without assuming anything about the distribution of the data. we often search anomaly detection dbscan on the internet because it is widely used for this.

DBSCAN is widely used in geographic information systems (GIS) for spatial data analysis, which involves locating regional clusters of events or phenomena, such as epidemics or crime hotspots.

Image Segmentation: DBSCAN is an effective image processing method that separates images into regions of similar hue or intensity. It makes it easier to identify objects or regions of interest in images.

Customer Segmentation: Using transactional data, DBSCAN may assist businesses with customer segmentation tasks, such as identifying groups of customers with comparable purchasing patterns or preferences.

Recommendation Systems: DBSCAN may be utilized in recommendation systems to categorize individuals or goods based on similar attributes. Enhancing recommendation accuracy may be achieved by dividing customers into subgroups based on common preferences.

Whether analyzing social networks or any other type of network, DBSCAN may be used to find clusters of nodes with a high density of connections.

These examples highlight how DBSCAN's ability to recognize clusters of any form, control noise, and employ fewer parameters than more traditional clustering algorithms like k-means shows its flexibility in many areas.

Summary

DBSCAN stands out as an unsupervised clustering technique renowned for its adeptness in recognizing clusters based on density rather than predefined cluster numbers. It operates without needing the user to specify the cluster count in advance and exhibits resilience against outliers. DBSCAN accomplishes clustering by identifying densely populated areas, delineating clusters where points are closely packed, and demarcating them from regions of sparse density. If we want to build a machine learning model using dbscan then we can write dbscan algorithm Python code. However, its efficacy can be influenced by parameter selections, especially when dealing with clusters of varying densities or datasets with high dimensions. Additionally, it may necessitate meticulous preprocessing steps to yield optimal results. Despite these challenges, DBSCAN excels in handling clusters with irregular shapes and effectively mitigating noise presence within datasets. 

Python Code

Below is the code for clustering dbscan Python: -



K-MEANS CLUSTERING IN MACHINE LEARNING/PYTHON/ARTIFICIAL INTELLIGENCE

K-means Clustering

  • Unsupervised Machine Learning
  • K-Means Algorithm Working
  • Choosing the Value of "K" in K-Means
  • Advantages of K-Means Clustering
  • Disadvantages of K-Means Clustering

Unsupervised machine learning is the autonomous pattern identification within unlabeled data by computers. This method avoids the machine learning using already labeled data. Its task is to organize unstructured data, detecting patterns, relationships, and variations independently. Various algorithms are employed for this purpose, with one such algorithm being K-Means clustering in machine learning.

One kind of unsupervised learning approach called K-Means clustering is intended to divide unlabeled datasets into discrete clusters. 'K' is the number of clusters the method seeks to find. For example, setting K=2 results in two clusters, while K=3 yields three clusters, and so on. Through iterative steps, the algorithm assigns data points to K clusters based on their similarities, ensuring each point belongs to a distinct cluster with similar characteristics. We apply k means clustering for customer segmentation because in customer segmentation using k means clustering, it becomes easier to work and understand.

As a centroid-oriented method, K-Means assigns a centroid to each cluster to minimize the total distance between data points and their corresponding clusters. An unlabeled dataset is first split into K clusters according to how similar the data points are to one another. Through iterative refinement, the algorithm adjusts the centroids until optimal clusters are achieved, with the specified value of K dictating the number of clusters formed.

Real-world example for k-means clustering

Let’s take a real-world example, there is a farmer’s market. However, as the market grew, it became more disorganized, making it difficult for vendors and customers to navigate. To end this problem the mayor of the village takes help from a data scientist Emily.

Emily started her work by meticulously gathering data on the products sold at the market, noting down details such as type, color, and price. After gaining these pieces of information, she apply k-means clustering algorithm to group similar products. Fruits, vegetables, grains, dairy products, and more began to form distinct clusters, creating a structured organization within the market.

After the clusters were identified the Mayor and Emily collaborated to redesign the layout of the market. They arranged vendor stalls according to the clusters, creating designed zones for each product category. Signs and labels were added to guide customers, ensuring a seamless shopping experience. They also use k means clustering for customer segmentation.

The k-means clustering algorithm mainly does two tasks:

  • The iterative process of K-Means involves determining the optimal number of centroids or K center points. Through this iterative approach, the algorithm refines the value of K by evaluating various cluster configurations until an optimal solution is found.
  • The K-Means algorithm assigns each data point to the nearest centroid or k-center. This process results in the formation of clusters where data points that are closest to a particular centroid are grouped together.
Therefore, similar data points belong to the same cluster and away from different data points or clusters.

Image source original

To calculate the similarity the algorithm will use the Euclidean distance as a measurement. The algorithm works as follows:
  • Initially, the K-Means algorithm randomly selects k points from the dataset, designating them as means or cluster centroids.
  • Subsequently, each item in the dataset is assigned to the nearest mean, grouping them into clusters. Following this, the means' coordinates are updated to the averages of all items within their respective clusters.
  • The process iterates for a specified number of iterations, continuously refining the clusters with each iteration until convergence is achieved. At the end of the iterations, the algorithm produces the final clusters based on the updated means and the assignment of data points to their nearest centroids.

In this scenario, the term "points" representing means denotes the average value of the items grouped within the clusters. There are various methods to initialize these means. One method involves randomly selecting items from the dataset to serve as initial means. Another approach is to randomly generate mean values within the range of the dataset's boundaries as the starting points.

Here is the pseudocode of the K-means clustering algorithm:

Initialize k means with random values

  • For a given number of iterations:
    • Iterate through items:
      • Find the mean closest to the item by calculating the Euclidean distance of the item with each of the means
      • Assign item to mean
      • Update mean by shifting it to the average of the items in that cluster
How K-Means algorithm work?

Let’s look at the steps of the k-means algorithm in machine learning and in general

Step 1 – Determine the appropriate value for K, which indicates the desired number of clusters to be created.

Step 2 – Choose K random points or centroids as the initial cluster centers, which may or may not be selected from the input dataset.

Step 3 – Assign each data point to the centroid that is closest to it, thereby grouping the points into K predefined clusters.

Step 4 – Compute the variance within each cluster and position a new centroid at the center of each cluster based on the calculated variance.

Step 5 – Proceed to the third stage iteratively, assigning each data point, using the revised centroids, to the closest centroid of the matching cluster.

Step 6 – If throughout the iteration any data points are moved to other centroids, the algorithm goes back to step 4 to recalculate the centroids. In any other case, it goes on to the last phase.

Step 7 –  After doing all of the above steps now we can say that our model is ready.

We can easily write the above clustering algorithm in Python.

Let’s understand the algorithm using images:

Let us have two variables n1 and n2. The x-y axis of the scatter plot of these two variables is shown below image:

Image source original

Now, to create a specified number 'k' of clusters (e.g., if k=2, then two clusters are to be formed), we proceed by selecting 'k' random points or centroids that will establish these clusters. These points can either be existing data points from the dataset or any arbitrary points. In the illustration below, two points have been randomly chosen as the 'k' points; it's important to note that these points, as shown in the image, are not part of the dataset's data points.

Image source original

It is now necessary to designate each point on the scatter plot to its nearest centroid or K-point. The process involves using mathematical formulas to determine the separation between two spots, thus we must draw a median between their centroids. displayed in the picture below.

Image source original

Data points on the left side of the line are closer to the K1 or cyan centroid, while those on the right side are closer to the violate centroid, as can be seen in the below image.

Image source original


We must choose "a new centroid" and repeat the process to locate the closest cluster. The centroids' center of gravity was used to choose the new centroids, which are displayed in the image below:

Image source original

All of the data points must be moved to the centroid nearest to them as part of the process. This necessitates recalculating the median line. After that, as can be seen below, the image displays the updated data point distribution:

Image source original

In the image above, it's noticeable that one violate point is situated above the median line, placing it on the side associated with the cyan centroid. Similarly, some cyan points are positioned on the side attributed to the violated centroid. Consequently, these points necessitate reassignment to the appropriate new centroids.


Image source original

Following the reassignment observed in the depicted image, the algorithm progresses back to the step involving the determination of new centroids or K-points.

The iterative process continues as the algorithm recalculates the center of gravity for the centroids, resulting in new centroids depicted in the image below:


Image source original

Once the new centroids are determined, the algorithm redraws the median line and reassigns the data points accordingly. This process alters the visualization of the data points, as illustrated in the following image:

Image source original

The diagram above demonstrates that each data point is correctly assigned to its respective cluster, with no dissimilar points appearing on opposite sides of the line. This outcome signifies the successful formation of our model.

Image source original

With our model now complete, we can remove the assumed centroids, revealing the final clusters as depicted in the image below.

Image source original

How to choose the value of “K” in K-means clustering?

Identifying the ideal number of clusters is crucial for the effectiveness of the k-means clustering algorithm. The "Elbow Method" is a widely used technique for determining this value, relying on the concept of WCSS, or "Within Cluster Sum of Squares." This metric quantifies the total variability within each cluster. To calculate the WCSS value, the following formula is utilized

In the above formula of WCSS

it is the sum of the square of the distance between each data point and its centroid with cluster 1 and for same goes for the other two terms.

The distance between the core and the points of data can be calculated using a variety of methods, such as the Manhattan and Euclidean distances. These distance measurements are meant to determine the degree of similarity or closeness between every cluster point of data and the center of the cluster.

To determine the optimal number of clusters using the elbow method, the following steps are typically followed:

  • The elbow strategy in the K-means clustering algorithm involves applying several values of K, typically ranging from 1 to 10, to a dataset.
  • For each value of K that may exist, the WCSS value needs to be ascertained.
  • Plotting a curve using the computed WCSS values and the cluster count K comes next after the WCSS has been calculated.
  • The curve mimics a human hum at the point where it meets the plot, which is the ideal value of K.
Image source original

Advantages of K-Means Clustering

  • Simplicity: it’s easy to implement and understand, making it a popular choice for clustering tasks.
  • Scalability: works well with large datasets and is computationally efficient, making it suitable for big data applications.
  • Versatility: can handle different types of data and can be adapted for various domains, from customer segmentation to image processing.
  • Speed: typically converges quickly, especially with large numbers of variables, making it efficient for many practical applications.
  • Flexibility: allows the user to define the number of clusters (k), providing flexibility in exploring different cluster configurations.
  • Initialization methods: offers multiple initialization methods to start the algorithm, reducing the sensitivity to initial seed points.
  • Interpretability: provides straightforward interpretation of results, as each data point is assigned to a specific cluster.
  • Robustness: can handle noisy and missing data reasonably well due to its cluster assignment approach.
  • Efficiency with spherical clusters: works effectively when clusters are spherical or close to spherical in shape.
  • Foundational algorithm: serves as a foundational clustering method, upon which various modifications and improvements, like K-medoids or fuzzy clustering, have been developed.

Disadvantages of K-means Clustering

  • Centroids are placed sensitively in the clustering process, hence even little changes in their starting locations might lead to different clustering results in the end.
  • The number of clusters (k) the user specifies determines the clustering result; this quantity may not always be known in advance and can have a big impact on the outcomes.
  • Assumption of spherical cluster: works best when clusters are roughly spherical. It struggles with clusters of irregular shapes or varying densities.
  • Vulnerable to outliers: outliers can substantially affect centroid placement, leading to potentially skewed clusters.
  • Fixed cluster boundaries: hard assignment of data points to clusters can result in misclassification, especially at the boundaries between clusters.
  • K-means clustering is sensitive to feature sizes; higher scale features may influence the grouping process more than smaller ones.
  • Restricted to Euclidean distance: mostly depends on this metric, which could not work well with all kinds of data or domains.
  • Not suitable for non-linear data – struggles to capture complex, non-linear relationships in data.
  • Difficulty with clusters of varying sizes and densities: may not perform well with clusters that have significantly different sizes or densities.
  • Convergence to local optima: the algorithm can converge to a local minimum, leading to suboptimal clustering solutions, especially in complex datasets.

Summary

K-means clustering is an easy and fast way to divide a dataset into "k" separate groups for unsupervised learning. In this method, data points are first assigned to the closest cluster center (or centroids) and then, until convergence is achieved, the centroids are constantly updated using the average of those points. K-means has a few disadvantages, such as the fact that it is prone to initial centroids selection, assumes spherical and equal-sized clusters, and is computationally efficient but requires a set number of clusters. Anomaly detection using k means clustering can also achieved.

Python Code

here is the k-means clustering Python code: - 


Featured Post

ASSOCIATION RULE IN MACHINE LEARNING/PYTHON/ARTIFICIAL INTELLIGENCE

Association rule   Rule Evaluation Metrics Applications of Association Rule Learning Advantages of Association Rule Mining Disadvantages of ...

Popular