/ddbscan

Discrete DBSCAN algorithm optimized for discrete and bounded data.

Primary LanguagePythonMIT LicenseMIT

Discrete DBSCAN

https://travis-ci.org/cloudwalkio/ddbscan.svg?branch=master Latest Version Downloads on PyPI License

This is a version of DBSCAN clustering algorithm optimized for discrete, bounded data, reason why we call it Discrete DBSCAN (DDBSCAN). The base for the current implementation is from this source. The algorithm code is in file ddbscan/ddbscan.py and can easily be read. The main algorithm itself is in method compute(), and can be understood following the links above or reading papers describing it.

Another feature of this implementation is that it is designed towards online learning. As a result, when we add points to our DDBSCAN object, we must pass one point each time to method add_point. See usage below.

Optimization for discrete and bounded data

Our main optimization to the vanilla algorithm described in the links above is based on the fact that for discrete and bounded data, we expect to see many times the same point occurring, so we can keep track of how many times the point ocurred and optimize our algorithm to use that information.

To speed up insertions of new points and computation of clusters, each DDBSCAN object keeps, for each point, the index of its neighbours and the neighbourhood size (the sum of the counts of the neighbours points). So, when we insert a new point, we see if it is an already existing pair and just increment its counter and the neighbourhood size of its neighbours. We recompute a KDTree with the points in case a new pair is inserted, updating the point data for its neighbours.

Parameters

A DBSCAN model has two parameters:

  • min_pts: minimum amount of neighbours of a point to create a cluster.
  • eps : radius to look for neighbours.

By tunning the two parameters we are, in fact, setting the anomaly (outlier) detection sensitiveness. A greater value for min_pts implies that to recognize a new pattern as a cluster, instead of an anomaly, we must see a larger amount of points with that pattern. A greater value for eps implies bigger clusters can form easier, so that points in less dense areas can be recognized as clusters members given this large eps. Given the importance of tunning this parameters, we have a method to set them, called set_params(), which updates the internal state of the model accordingly.

Install

To just install the package the easist way is to use pip:

$ pip install ddbscan

Another option is to clone this repo and run

$ python setup.py install

To run the tests:

$ python setup.py test

Usage

A typical example would be as following:

import ddbscan

# Create a DDBSCAN model with eps = 2 and min_pts = 5
scan = ddbscan.DDBSCAN(2, 5)

# Add points to model
data = [[1,  2], [2,  2], [1,  3], [2, 3], [3, 3], [8, 9],
        [7,  6], [9,  7], [6, 9], [6, 8], [5, 5], [7, 8]]

for point in data:
    scan.add_point(point=point, count=1, desc="")

# Compute clusters
scan.compute()

print 'Clusters found and its members points index:'
cluster_number = 0
for cluster in scan.clusters:
    print '=== Cluster %d ===' % cluster_number
    print 'Cluster points index: %s' % list(cluster)
    cluster_number += 1

print '\nCluster assigned to each point:'
for i in xrange(len(scan.points)):
    print '=== Point: %s ===' % scan.points[i]
    print 'Cluster: %2d' % scan.points_data[i].cluster,
    # If a point cluster is -1, it's an anomaly
    if scan.points_data[i].cluster == -1:
        print '\t <== Anomaly found!'
    else:
        print

# Print ASCII plot
print
print ' ===== Data plot ====='
print
print 'Legend => x: anomaly'

symbols = 'x#=~+-opwbcnkjhf@uyt()987654321' # Symbols used to plot points
cluster_number = 0
for cluster in scan.clusters:
    print '          %s: cluster %d' % (symbols[cluster_number + 1], cluster_number)
    cluster_number += 1
print

print '-----------------------'
for x in range(0,10):
    print '|',
    for y in range(0, 10):
        if [x, y] in data:
            index = data.index([x,y])
            cluster = scan.points_data[index].cluster + 1
            print symbols[cluster],
        else:
            print ' ',
    print '|'
print '-----------------------'

License

The MIT License (MIT)

Copyright (c) 2015 CloudWalk, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.