DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Density-Based Clustering and Identifying Arbitrarily Shaped Distributions Using R

Density-Based Clustering and Identifying Arbitrarily Shaped Distributions Using R

We take a look at how R can help us analyze, make sense of, and visualize data using density-based clustering algorithms.

Abhijit Telang user avatar by
Abhijit Telang
CORE ·
Mar. 08, 19 · Tutorial
Like (4)
Save
Tweet
Share
5.60K Views

Join the DZone community and get the full member experience.

Join For Free

I was startled to see a group of three blind people who were trying to navigate along a busy corridor. The first one was leading the way, perhaps owing to his familiarity with the terrain, while the other two hung on to the shoulder of the one before them, thus forming a trio that patiently and painstakingly navigated their journey. This coordination and commitment helped them safely overcome the spurious twists and turns, the wide and narrow passages filled with people moving in various directions.

The three-nearest neighbor concept in statistics is intuitively similar. If we have a data set that contains arbitrary shapes or patterns which are yet to be discovered, this can be envisioned similar to the navigation problem mentioned above.

If I do not know the caverns, passages, twists, and turns the pathways have to offer, I cannot rely on a predefined, cookie-cutter approach, where I must know how many clusters exist in advance, and then assign observations to those respective clusters.

I must then, compute in real-time and adapt on the fly, as the shapes and passages change during my navigational journey. Finding the nearest neighbor is then similar to extending the hand to reach out in the darkness so that some sense can be gained through proximity and touch. 

You can derive the same from principles used in rock climbing. The rocks must be felt by hands or feet in order to assess their position, strength, and suitability to as a foot-hold.

So, density-based clustering is suitable when the distributions are arbitrary and must be scaled on the fly in order to recognize and map them. This is where algorithms like DBSCAN excel and simple K-means clustering approaches fail (if all you have is a cutter that can cut out a shape that is predefined and fixed, that's what you get. You learn nothing more than what you already know).

Now, let's look at the core concepts in this algorithm.

You have:

(i)A set of core observations, around which (ii) non-core, yet directly reachable (traversable as in graph theory) observations arrange themselves. (iii) Observations who belong to neither (i) nor (ii) are considered noise or irrelevant.

Now, to distinguish between core and non-core observations, we need to have a minPts criterion, that determines the minimum number of points or observations that must exist within a "critical," or epsilon, distance from a core point.

In order to find the nearest neighbors, we must define how we are going to measure the proximity. 

R provides two ways to measure proximity or "nearness": the first is obviously the Euclidean distance and second is the KD-Tree approach.

The latter is similar to decision trees in concept, where we iteratively continue partitioning the space around a given observation, in order to reach it. The space itself can be specified through n dimensions, and hence we must then choose a sequence among such dimensions for partitioning.

We will be working with a predefined data set named DS3.

## install package DBSCAN and get access to data set DS3
library(DBSCAN)
data("DS3")

Once you get the data set into the environment, you can compute the 3nn, a.k.a. 3 nearest neighbors, for each observation. So, effectively minPts for core versus non-core classification becomes equal to 3.

## do a know your neighbors ( for each observation)
nn3<-kNN(DS3,k=3,search="kdtree")

## visualize nearest neighbors ( 3 in this case)
plot(nn3,DS3)

We will compute the neighbors using the kd-tree distance criterion and the result can be visualized through the plot below.

Data visualization

You can easily see how arbitrary the shapes can be almost magically discovered, through the principle of the nearest neighbor search.

The magic happens because the methodical approach of meeting and greeting the neighbors discovers more and more neighbors (and hence the visualization becomes denser and denser) as per the formation of the shape, and on the other hand, sparser and sparser as the traversal approaches the contours of those very shapes. The sparseness around the dense shapes provides the much-needed contrast to discover hidden shapes.

Compare this with the cookie cutter approach of K-Means clustering.

Now, since I do not know anything about the denseness or the sparseness, I must start with a random number of clusters, and, depending on that number, my partitioning and corresponding assignments will change. 

Here is how distribution looks with, say, 25 clusters.

Data visualization

And here is how the partitions and assignments look like with just 5 pre-defined clusters.

Data visualization

The knowledge that can be gained from K-means clustering is limited to the size (and dimensions) of the cut, rather than the actual shape of the distribution. 

We can complete the rest of the visualization leveraging the distance plot in R.

kNNdistplot(DS3,k=3)

Data visualization

This will allow us to arrive at an estimate of epsilon distance or the "critical" distance around each core classification, within which the minimum number of nearest neighbors must exist (in this case, minPts=3, i.e. the three nearest neighbors).

Those which fall outside this "critical" distance will be classified as non-core, which in turn can be further classified as either fringe or those on the fence, yet reachable from core ones. Those that belong to neither the core or fringe classification will be classified as "noise."

abline(h=6,col="RED",lty=2)

This may need a bit of trial and error.

Data visualization

> ## recognize shapes through clustering
> 
> plot(DS3,col=res$cluster,pch=24)
> res<-dbscan(DS3,eps=6,minPts=3)
> plot(DS3,col=res$cluster,pch=24)

Data visualization

That's it.

Another example of the simplistic elegance and efficiency with which R can perform complex data analysis and visualization.

R (programming language) clustering Distribution (differential geometry) Neighbors (app) Data set

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Elasticsearch Works
  • mTLS Everywere
  • Introduction to Container Orchestration
  • What Are the Benefits of Java Module With Example

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: