public ClusteringPoint(ClusteringPoint p) { this.values = new double[p.values.Length]; for (int i = 0; i < p.values.Length; i++) { this.values[i] = p.values[i]; } this.Index = p.Index; this.Id = p.Id; }
public void AddCluster(int count) { if (this.points == null) { return; } if (this.clusters == null) { this.clusters = new List <ClusteringPoint>(); } Random rnd = new Random(); for (int i = 0; i < count; i++) { ClusteringPoint p = this.points[rnd.Next(this.points.Length)]; this.clusters.Add(new ClusteringPoint(p)); } }
public Color3[] Clusters(int count) { Color3[] l = new Color3[count]; int p = 0; for (int i = 0; i < this.clusters.Count; i++) { ClusteringPoint cp = this.clusters[i]; l[p++] = new Color3((float)cp[0], (float)cp[1], (float)cp[2]); if (p >= count) { break; } } while (p < count) { l[p++] = new Color3(0f, 0f, 0f); } return(l); }
public double Distance(ClusteringPoint p) { int l = this.values.Length; if (l != p.values.Length) { throw new Exception("Point dimensions must be the same"); } double[] v = new double[l]; for (int i = 0; i < l; i++) { v[i] = this.values[i] - p.values[i]; } double f = 0f; for (int i = 0; i < l; i++) { f += (v[i] * v[i]); } return(Math.Sqrt(f)); }
public void Step() { List <int>[] clusterPoints = new List <int> [this.clusters.Count]; for (int i = 0; i < clusterPoints.Length; i++) { clusterPoints[i] = new List <int>(); } int cc = this.clusters.Count; for (int i = 0; i < this.points.Length; i++) { int c = -1; double dist = 0f; ClusteringPoint p = this.points[i]; for (int j = 0; j < cc; j++) { double x = p.Distance(this.clusters[j]); if ((j == 0) || (x < dist)) { c = j; dist = x; } } clusterPoints[c].Add(i); this.points[i].Index = c; } int size = this.points[0].Size; for (int i = 0; i < cc; i++) { List <int> l = clusterPoints[i]; if (l.Count == 0) { continue; } double[] v = new double[size]; for (int k = 0; k < size; k++) { v[k] = 0f; } for (int j = 0; j < l.Count; j++) { ClusteringPoint p0 = this.points[l[j]]; for (int k = 0; k < size; k++) { v[k] += p0[k]; } } ClusteringPoint c = this.clusters[i]; double f = (double)l.Count; for (int k = 0; k < size; k++) { c[k] = v[k] / f; } this.clusters[i] = c; } }