Density-Based Spatial Clustering of Applications with Noise (DBSCAN)
Originally Posted by wikipedia
Density-based spatial clustering of applications with noise (DBSCAN) is a data clustering algorithm. It is a density-based clustering algorithm because it finds a number of clusters starting from the estimated density distribution of corresponding nodes. DBSCAN is one of the most common clustering algorithms.
1. Add Dbscan.cs to your project.
2. Add Dbscan.txt to your project, set the Copy to Output Directory property to Copy always on it.
3. Add a chart (from the System.Windows.Forms.DataVisualization namespace) to the form.
4. Add the code below to your form. (VB Version Here)
Code:
public partial class Form1 : Form
{
#region fields
private static Color[] ChartColors = new Color[] { Color.FromArgb(0x41, 140, 240), Color.FromArgb(0xfc, 180, 0x41),
Color.FromArgb(0xe0, 0x40, 10), Color.FromArgb(5, 100, 0x92),
Color.FromArgb(0xbf, 0xbf, 0xbf), Color.FromArgb(0x1a, 0x3b, 0x69),
Color.FromArgb(0xff, 0xe3, 130), Color.FromArgb(0x12, 0x9c, 0xdd),
Color.FromArgb(0xca, 0x6b, 0x4b), Color.FromArgb(0, 0x5c, 0xdb),
Color.FromArgb(0xf3, 210, 0x88), Color.FromArgb(80, 0x63, 0x81),
Color.FromArgb(0xf1, 0xb9, 0xa8), Color.FromArgb(0xe0, 0x83, 10),
Color.FromArgb(120, 0x93, 190) };
#endregion
#region constructors
public Form1()
{
this.InitializeComponent();
this.InitializeChart();
}
#endregion
#region methods
private void InitializeChart()
{
Series series = this.chart1.Series[0];
series.ChartType = SeriesChartType.Point;
series.MarkerStyle = MarkerStyle.Circle;
series.MarkerSize = 10;
this.chart1.Legends[0].Enabled = false;
this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "N3";
this.chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
this.chart1.ChartAreas[0].AxisY.LabelStyle.Format = "N3";
this.chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// generate chart based on Dbscan.txt
this.GenerateClusters();
}
private void GenerateClusters()
{
List<DbscanPoint> points = new List<DbscanPoint>();
// change path as appropriate
var lines = System.IO.File.ReadAllLines("Dbscan.txt");
foreach (var line in lines)
{
var split = line.Split(' ');
points.Add(new DbscanPoint(Convert.ToDouble(split[1]),
Convert.ToDouble(split[2])));
}
// customize the epsilion radius and minimum cluster points here
// note: if you are using http://people.cs.nctu.edu.tw/~rsliang/dbscan/testdatagen.html
// to generate data, make sure these parameters match the parameters on the site
// otherwise you will get different results
DbscanAlgorithm.Dbscan(points.ToArray(), 4d, 6);
var series = this.chart1.Series[0];
foreach (var item in points)
{
var p = series.Points[series.Points.AddXY(item.X, item.Y)];
if (item.IsNoise)
p.Color = SystemColors.Control;
else
p.Color = Form1.ChartColors[(item.ClusterId.Value - 1) % Form1.ChartColors.Length];
}
}
#endregion
}