473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

K-mean clustering algorithm!

Hi!
I need help ragarding the k-mean clustering algo. Any one have a
source code in c#.. or a dll file that could be used in the
project.... I cannot seem to identify the objects that could be used
in the algo..
I have to group the diseases according to provinces, and plot them in
graph, this will show how many diseases are more in which region. Im
confused as to what shuld be my initial clusters, the
provinces(location) or the diseases. I guess the location..
Give your ideas please!

Apr 30 '07 #1
2 6694
On 30 Apr, 12:38, Shum <shumy...@gmail.comwrote:
Hi!
I need help ragarding the k-mean clustering algo. Any one have a
source code in c#.. or a dll file that could be used in the
project.... I cannot seem to identify the objects that could be used
in the algo..
I have to group the diseases according to provinces, and plot them in
graph, this will show how many diseases are more in which region. Im
confused as to what shuld be my initial clusters, the
provinces(location) or the diseases. I guess the location..
Give your ideas please!
There's an explanation here with VB6 code. The only thing to bear in
mind is C# doesn't have a power operator like VB6, so 6^2 will return
4 not 36 (it's doing a binary XOR). Use Math.Pow instead.

http://people.revoledu.com/kardi/tut...ean/index.html

Here's a really scrappy interpretation of the VB6 code. It's a mess
and I wasn't going to post it. It comes without warranty and watch for
line breaks.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace KMean
{
struct Point
{
public int Cluster;
public int X;
public int Y;
public int Count;

public Point(int pCluster, int pX, int pY)
{
Cluster = pCluster;
X = pX;
Y = pY;
Count = 0;
}
public Point(Point pPoint) : this(pPoint.Cluster, pPoint.X,
pPoint.Y)
{
}
}

public class KMean : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label One;
private System.Windows.Forms.Label Two;
private System.Windows.Forms.Label Three;
private System.ComponentModel.Container components = null;

private ArrayList _data = new ArrayList();
private Point[] _centres = new Point[3];
private Label[] _labels = new Label[3];
public KMean()
{
InitializeComponent();
_labels[0] = One;
_labels[1] = Two;
_labels[2] = Three;
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.One = new System.Windows.Forms.Label();
this.Two = new System.Windows.Forms.Label();
this.Three = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor =
System.Drawing.SystemColors.HighlightText;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(688, 438);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new
System.Windows.Forms.PaintEventHandler(this.pictur eBox1_Paint);
this.pictureBox1.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.pictur eBox1_MouseUp);
//
// One
//
this.One.BackColor = System.Drawing.Color.FromArgb(((System.Byte)
(192)), ((System.Byte)(255)), ((System.Byte)(192)));
this.One.Location = new System.Drawing.Point(128, 120);
this.One.Name = "One";
this.One.Size = new System.Drawing.Size(8, 16);
this.One.TabIndex = 1;
this.One.Text = "1";
this.One.Visible = false;
//
// Two
//
this.Two.BackColor = System.Drawing.Color.FromArgb(((System.Byte)
(192)), ((System.Byte)(255)), ((System.Byte)(192)));
this.Two.Location = new System.Drawing.Point(142, 125);
this.Two.Name = "Two";
this.Two.Size = new System.Drawing.Size(8, 16);
this.Two.TabIndex = 2;
this.Two.Text = "2";
this.Two.Visible = false;
//
// Three
//
this.Three.BackColor = System.Drawing.Color.FromArgb(((System.Byte)
(192)), ((System.Byte)(255)), ((System.Byte)(192)));
this.Three.Location = new System.Drawing.Point(176, 104);
this.Three.Name = "Three";
this.Three.Size = new System.Drawing.Size(8, 16);
this.Three.TabIndex = 3;
this.Three.Text = "3";
this.Three.Visible = false;
//
// KMean
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(688, 438);
this.Controls.Add(this.Three);
this.Controls.Add(this.Two);
this.Controls.Add(this.One);
this.Controls.Add(this.pictureBox1);
this.Font = new System.Drawing.Font("Arial", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.Name = "KMean";
this.Text = "KMean";
this.ResumeLayout(false);

}
#endregion

private void pictureBox1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
Point newData = new Point();
newData.X = e.X;
newData.Y = e.Y;
_data.Add(newData);
CalcClusters();
pictureBox1.Invalidate();
}
private void CalcClusters()
{
Point p;

switch(_data.Count)
{
case 1:
case 2:
case 3:
p = new Point((Point)_data[_data.Count-1]);
p.Cluster = _data.Count-1;
_centres[_data.Count-1] = p;
_data[_data.Count-1] = p;
break;
default:
CalcClusters2();
break;
}
}
private void CalcClusters2()
{
Boolean running = true;
double min = 10000000.0;
double distance = 0.0;
int cluster = 0;
Point p1 = (Point)_data[_data.Count-1];
foreach(Point p2 in _centres)
{
distance = Distance(p1, p2);
if(distance < min)
{
min = distance;
cluster = p2.Cluster;
}
}
p1 = (Point)_data[_data.Count-1];
p1.Cluster = cluster;
_data[_data.Count-1] = p1;

Point[] sums;
int c;
int d;

while(running)
{
sums = new Point[3];
for(c=0; c<_data.Count; c++)
{
p1 = (Point)_data[c];
sums[p1.Cluster].X+= p1.X;
sums[p1.Cluster].Y+= p1.Y;
sums[p1.Cluster].Count++;
}
for(c=0; c<=_centres.GetUpperBound(0); c++)
{
if(sums[c].Count>0)
{
_centres[c].X = sums[c].X / sums[c].Count;
_centres[c].Y = sums[c].Y / sums[c].Count;
}
}

running = false;

for(c=0; c<_data.Count; c++)
{
min = 10000000.0;
for(d=0; d<=_centres.GetUpperBound(0); d++)
{
distance = Distance((Point)_data[c], _centres[d]);
if(distance < min)
{
min = distance;
cluster = d;
}
}
p1 = (Point)_data[c];
if(p1.Cluster != cluster)
{
p1.Cluster = cluster;
running = true;
_data[c] = p1;
}
}
}
}
private double Distance(int pX1, int pY1, int pX2, int pY2)
{
return Math.Sqrt(Math.Pow((pY2 - pY1),2) + Math.Pow((pX2 - pX1),
2));
}
private double Distance(Point pP1, Point pP2)
{
return Distance(pP1.X,pP1.Y, pP2.X, pP2.Y);
}
private void pictureBox1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
for(int c=0 ; c<=_centres.GetUpperBound(0) && c<_data.Count ; c++)
{
_labels[c].Visible = true;
_labels[c].Top = _centres[c].Y;
_labels[c].Left = _centres[c].X;
}

e.Graphics.Clear(Color.White);
using(Pen p = new Pen(Brushes.Black,5.0f))
{
foreach(Point pnt in _data)
{
switch(pnt.Cluster)
{
case 0:
p.Brush = Brushes.Blue;
break;
case 1:
p.Brush = Brushes.Red;
break;
case 2:
p.Brush = Brushes.Green;
break;
default:
p.Brush = Brushes.Black;
break;
}
e.Graphics.DrawEllipse(p,pnt.X, pnt.Y,10,10);
}
}
}
}
}

Apr 30 '07 #2
Im trying to understand the code.. its uncommented so hard to know
whats happening.
Thanks anyway.
DeveloperX wrote:
On 30 Apr, 12:38, Shum <shumy...@gmail.comwrote:
Hi!
I need help ragarding the k-mean clustering algo. Any one have a
source code in c#.. or a dll file that could be used in the
project.... I cannot seem to identify the objects that could be used
in the algo..
I have to group the diseases according to provinces, and plot them in
graph, this will show how many diseases are more in which region. Im
confused as to what shuld be my initial clusters, the
provinces(location) or the diseases. I guess the location..
Give your ideas please!

There's an explanation here with VB6 code. The only thing to bear in
mind is C# doesn't have a power operator like VB6, so 6^2 will return
4 not 36 (it's doing a binary XOR). Use Math.Pow instead.

http://people.revoledu.com/kardi/tut...ean/index.html

Here's a really scrappy interpretation of the VB6 code. It's a mess
and I wasn't going to post it. It comes without warranty and watch for
line breaks.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace KMean
{
struct Point
{
public int Cluster;
public int X;
public int Y;
public int Count;

public Point(int pCluster, int pX, int pY)
{
Cluster = pCluster;
X = pX;
Y = pY;
Count = 0;
}
public Point(Point pPoint) : this(pPoint.Cluster, pPoint.X,
pPoint.Y)
{
}
}

public class KMean : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label One;
private System.Windows.Forms.Label Two;
private System.Windows.Forms.Label Three;
private System.ComponentModel.Container components = null;

private ArrayList _data = new ArrayList();
private Point[] _centres = new Point[3];
private Label[] _labels = new Label[3];
public KMean()
{
InitializeComponent();
_labels[0] = One;
_labels[1] = Two;
_labels[2] = Three;
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.One = new System.Windows.Forms.Label();
this.Two = new System.Windows.Forms.Label();
this.Three = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor =
System.Drawing.SystemColors.HighlightText;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(688, 438);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new
System.Windows.Forms.PaintEventHandler(this.pictur eBox1_Paint);
this.pictureBox1.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.pictur eBox1_MouseUp);
//
// One
//
this.One.BackColor = System.Drawing.Color.FromArgb(((System.Byte)
(192)), ((System.Byte)(255)), ((System.Byte)(192)));
this.One.Location = new System.Drawing.Point(128, 120);
this.One.Name = "One";
this.One.Size = new System.Drawing.Size(8, 16);
this.One.TabIndex = 1;
this.One.Text = "1";
this.One.Visible = false;
//
// Two
//
this.Two.BackColor = System.Drawing.Color.FromArgb(((System.Byte)
(192)), ((System.Byte)(255)), ((System.Byte)(192)));
this.Two.Location = new System.Drawing.Point(142, 125);
this.Two.Name = "Two";
this.Two.Size = new System.Drawing.Size(8, 16);
this.Two.TabIndex = 2;
this.Two.Text = "2";
this.Two.Visible = false;
//
// Three
//
this.Three.BackColor = System.Drawing.Color.FromArgb(((System.Byte)
(192)), ((System.Byte)(255)), ((System.Byte)(192)));
this.Three.Location = new System.Drawing.Point(176, 104);
this.Three.Name = "Three";
this.Three.Size = new System.Drawing.Size(8, 16);
this.Three.TabIndex = 3;
this.Three.Text = "3";
this.Three.Visible = false;
//
// KMean
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(688, 438);
this.Controls.Add(this.Three);
this.Controls.Add(this.Two);
this.Controls.Add(this.One);
this.Controls.Add(this.pictureBox1);
this.Font = new System.Drawing.Font("Arial", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.Name = "KMean";
this.Text = "KMean";
this.ResumeLayout(false);

}
#endregion

private void pictureBox1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
Point newData = new Point();
newData.X = e.X;
newData.Y = e.Y;
_data.Add(newData);
CalcClusters();
pictureBox1.Invalidate();
}
private void CalcClusters()
{
Point p;

switch(_data.Count)
{
case 1:
case 2:
case 3:
p = new Point((Point)_data[_data.Count-1]);
p.Cluster = _data.Count-1;
_centres[_data.Count-1] = p;
_data[_data.Count-1] = p;
break;
default:
CalcClusters2();
break;
}
}
private void CalcClusters2()
{
Boolean running = true;
double min = 10000000.0;
double distance = 0.0;
int cluster = 0;
Point p1 = (Point)_data[_data.Count-1];
foreach(Point p2 in _centres)
{
distance = Distance(p1, p2);
if(distance < min)
{
min = distance;
cluster = p2.Cluster;
}
}
p1 = (Point)_data[_data.Count-1];
p1.Cluster = cluster;
_data[_data.Count-1] = p1;

Point[] sums;
int c;
int d;

while(running)
{
sums = new Point[3];
for(c=0; c<_data.Count; c++)
{
p1 = (Point)_data[c];
sums[p1.Cluster].X+= p1.X;
sums[p1.Cluster].Y+= p1.Y;
sums[p1.Cluster].Count++;
}
for(c=0; c<=_centres.GetUpperBound(0); c++)
{
if(sums[c].Count>0)
{
_centres[c].X = sums[c].X / sums[c].Count;
_centres[c].Y = sums[c].Y / sums[c].Count;
}
}

running = false;

for(c=0; c<_data.Count; c++)
{
min = 10000000.0;
for(d=0; d<=_centres.GetUpperBound(0); d++)
{
distance = Distance((Point)_data[c], _centres[d]);
if(distance < min)
{
min = distance;
cluster = d;
}
}
p1 = (Point)_data[c];
if(p1.Cluster != cluster)
{
p1.Cluster = cluster;
running = true;
_data[c] = p1;
}
}
}
}
private double Distance(int pX1, int pY1, int pX2, int pY2)
{
return Math.Sqrt(Math.Pow((pY2 - pY1),2) + Math.Pow((pX2 - pX1),
2));
}
private double Distance(Point pP1, Point pP2)
{
return Distance(pP1.X,pP1.Y, pP2.X, pP2.Y);
}
private void pictureBox1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
for(int c=0 ; c<=_centres.GetUpperBound(0) && c<_data.Count ; c++)
{
_labels[c].Visible = true;
_labels[c].Top = _centres[c].Y;
_labels[c].Left = _centres[c].X;
}

e.Graphics.Clear(Color.White);
using(Pen p = new Pen(Brushes.Black,5.0f))
{
foreach(Point pnt in _data)
{
switch(pnt.Cluster)
{
case 0:
p.Brush = Brushes.Blue;
break;
case 1:
p.Brush = Brushes.Red;
break;
case 2:
p.Brush = Brushes.Green;
break;
default:
p.Brush = Brushes.Black;
break;
}
e.Graphics.DrawEllipse(p,pnt.X, pnt.Y,10,10);
}
}
}
}
}
May 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: willie | last post by:
Hi all: I have a clustering SQL Server on Node1 and Node2, the Node1 has named Instance1 and Node2 has named Instance2, no default instance. We tested it that everthing is OK, then we decide to...
5
by: Paolino | last post by:
I have a self organizing net which aim is clustering words. Let's think the clustering is about their 2-grams set. Words then are instances of this class. class clusterable(str): def...
10
by: querypk | last post by:
Can someone help me with C programming here. If I have x is a 2D array of 0's and 1's ex: x = array(, , , ) what I want is a boundingbox over the region where we find clusters of 1's.So for...
3
by: Zarathustra | last post by:
I'm sure there must be a simple algorithm for giving the number of "ones" that are connected, for example in an array like this: int a = {0}; a = a = a = a = a = a= a = a = 1; with all the rest...
5
by: al pacino | last post by:
Hi all, which is the better suited clustering(data mining )algorithm to use for web server log file mining. i am using python to parse and clean the log file and using oracle to save the...
11
by: chmmr | last post by:
Hi, I am currently in the process of gathering info/experiences for an incoming Linux DB2 clustering phase we actually know nothing about (since we are doing it for the first time ever), so I...
3
by: dejavue82 | last post by:
Hi, Does anybody know of a software package that allows for several servers, running asp.net 2.0 to be clustered, regardless of where they are located (ie. without a hardware load balancer)....
5
by: Lakesider | last post by:
Hi NG, I have a question about data: I have travel-times from A to B like this from | to | sec. A B 17 A B 18 A B 30 A B 32
3
by: Manish | last post by:
I think this question has been asked number of times. However, I am looking for some specific information. Perhaps some of you can help close the gap. Or perhaps you can point me towards right...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.