Created
March 15, 2014 01:28
-
-
Save groz/9560496 to your computer and use it in GitHub Desktop.
Demonstrates error (or my misunderstanding) of KMeans algorithm in numl.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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