Skip to content

Instantly share code, notes, and snippets.

@groz
Created March 15, 2014 01:28
Show Gist options
  • Save groz/9560496 to your computer and use it in GitHub Desktop.
Save groz/9560496 to your computer and use it in GitHub Desktop.
Demonstrates error (or my misunderstanding) of KMeans algorithm in numl.
using System;
using numl.Model;
using numl.Unsupervised;
namespace KmeansError
{
class Program
{
class DataPoint
{
public DataPoint(double x, double y)
{
this.X = x;
this.Y = y;
}
public double X { get; set; }
public double Y { get; set; }
}
static void Main(string[] args)
{
var dataSet = new[]
{
new DataPoint(10, 0),
new DataPoint(10, 0),
new DataPoint(29, 3),
new DataPoint(30, 4),
};
const int nGroups = 2;
var kmeans = new KMeans();
kmeans.Descriptor = Descriptor
.For<DataPoint>()
.With(d => d.X)
.With(d => d.Y);
int[] groups = kmeans.Generate(dataSet, nGroups);
for (int i = 0; i < groups.Length; ++i)
{
Console.WriteLine("Point {0} belongs to group {1}", i, groups[i]);
/*
Output:
Point 0 belongs to group 0
Point 1 belongs to group 0
Point 2 belongs to group 0
Point 3 belongs to group 0
*/
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment