Giter Site home page Giter Site logo

leanerr / dataanalyse_k_means_vs_dbscan_clustering Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 202 KB

Comparsion Between k_means and DBSCAN Clustering based on their accuracy

Jupyter Notebook 15.55% HTML 84.45%
jupyter-notebook python data-science dbscan-clustering k-means-clustering

dataanalyse_k_means_vs_dbscan_clustering's Introduction

DataAnalyse_k_means_vs_DBSCAN_clustering

Comparsion Between k_means and DBSCAN Clustering based on their accuracy

DBSCAN

Implementation of DBSCAN Algorithm in Python.

Input:

It takes two inputs. First one is the .csv file which contains the data (no headers). In 'main.py' change line 12 to:

DATA = '/path/to/csv/file.csv'

And the second is the config file which contains few parameters necessary for the algorithm. More details inside 'config' file. You can change the 'config' file as per your requirement.

UPDATE: July 13, 2017 - The code has been updated to support 3D points. Although technically, it can be used to perform multi-demensional clustering (might need to tweak the code more) - it is the visualization part will not work as expected.

Few Snapshots

2-D Clustering in action

3-D Clustering in action

python-kmeans

python implementation of k-means clustering. k-means is an unsupervised learning technique that attempts to group together similar data points in to a user specified number of groups. The below example shows the progression of clusters for the Iris data set using the k-means++ centroid initialization algorithm.

results

Description

k-means attempts to identify a user specified k(<N) number of clusters from a set of N d-dimensional real valued vectors. The algorithm proceeds by attempting to minimize the sum of squared distances from a cluster center, to the cluster members. The canonical algorithm proceeds in three phases:

  1. Initialise k random centroids (cluster centers);
  2. assign data points to nearest cluster according to distance metric (typically Euclidean distance);
  3. update the centroids to the mean of the members of the cluster;
  4. repeat steps 2 & 3 until the assignments from step 2 do not change.

The output of the algorithm is a cluster assignment for each data point, and a final level of "distortion". The algorithm does not produce a provably optimal solution, and initial cluster centers may cause the algorithm to get stuck in a locally optimum solution that is clearly sub-optimal (see the basic 2d example in the Results section).

Much research has focused on:

  • selecting initial cluster centers. K-Means++ is a well known method, and is included in this implementation, the algorithm is outlined in the following sub-section.
  • computing distances, i.e. using measures other than Euclidean see here.

K-Means++

Rather than initialize random centroids as in step 1 above, k-means++ probabilistically spreads out the initial centroids to avoid poor initial configuration, the algorithm is:

  1. Choose first centroid randomly.
  2. For each data point x, compute the distance d(x), from x to the nearest centroid that has already been chosen.
  3. Select a data point to be the next centroid using a weighted probability proportional to d(x)2.

This technique gives favor to data points which are not near another initial centroids, and uses a selection policy that is reminiscent of roulette wheel (or fitness proportionate) selection that is often used in genetic algorithms.

Resources

Basic Algorithm

Initialization of Clusters

Why not use SciPy?

SciPy has a k-means implementation. The objective of this work is to build a pure python implementation for the purposes of learning, and helping others learn the k-means algorithm. Interested readers with only minimal python experience will be able to read, and step over this code without the added complexity of a library such as SciPy. It is not by any means intended for production use :)

Running the code

Dependencies

  • python 3.6.3
  • matplotlib 2.1.1 - see here for installation instructions.

Execution

Run the code with the python interpreter:

python kmeans.py ./resources/<config.cfg>

Where config.cfg is a plain text configuration file. The format of the config file is a python dict with the following fields:

{
   'data_file' : '\\resources\\iris.csv',
   'data_project_columns' : ['sepal_length','sepal_width','petal_length','petal_width','class'],
   'k' : 3,
   'cluster_atts' : ['sepal_length','sepal_width','petal_length','petal_width'],
   'init_cluster_func' : 'kmeans_plus_plus',
   'plot_config' :
    {'output_file_prefix' : 'iris',
     'plots_configs': [
        {'plot_atts' : ['sepal_length','sepal_width']},
        {'plot_atts' : ['sepal_length','petal_length']},
        {'plot_atts' : ['sepal_length','petal_width']},
        {'plot_atts' : ['sepal_width','petal_length']},
        {'plot_atts' : ['sepal_width','petal_width']},
        {'plot_atts' : ['sepal_width','petal_width']}
     ]
   }
}

You have to specify:

  • a csv data file;
  • a subset of fields to project from the file;
  • the number of clusters to form, k;
  • the subset of attributes used in the clustering process;
  • optionally specify an initial cluster func (default='rand_init_centroids'), interested authors made add their own to the code and specify it here;
  • a plot config that includes
    • prefix for png files created during the process in the working directory, if this isn't specified, images will not be produced;
    • the individual plot configurations, limited to 2 dimensions per plot.

Results

Iris Data Set

The Iris data set (iris.config), from Lichman, M. (2013). UCI Machine Learning Repository . Irvine, CA: University of California, School of Information and Computer Science., is a very well known data set in the machine learning community. Here are the results of my random initial clusters:

iris_init_results iris_final_results

Basic Synthetic 2D data

This data was generated for debugging purposes (see basic2d.config), and illustrates the effect of having a poor choice of initial random clusters. The below results demonstrate an initial centroid configuration that prevents the algorithm from reaching the obvious cluster assignment. In this case the placement of the red centroid means the blue centroid captures all of the data points in the lower left, and lower right quadrants.

basic_init basic_interim basic_final

dataanalyse_k_means_vs_dbscan_clustering's People

Contributors

leanerr avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.