private static unsafe double[][] SolveKMeans(PocketData[] pockets) { Kml.Init(null); Kml.Parameters kmParams = new Kml.Parameters(); kmParams.SetDefaultTerm(); kmParams.dim = _cmdLine.Dim; kmParams.term_st_a = _cmdLine.Stages; kmParams.term_st_b = kmParams.term_st_c = kmParams.term_st_d = 0; kmParams.seed = 1; kmParams.k = _cmdLine.K; kmParams.n = _cmdLine.UsePocketCounts ? 1326: 169; kmParams.Allocate(); Console.WriteLine("Data passed to kml:"); int p = 0; for (int pocket = 0; pocket < pockets.Length; ++pocket) { int count = _cmdLine.UsePocketCounts ? HePocket.KindToRange((HePocketKind)pocket).Length : 1; for (int i = 0; i < count; ++i) { for (int d = 0; d < _cmdLine.Dim; ++d) { double value = pockets[pocket].Value[d]; *kmParams.GetPoint(p, d) = value; Console.Write(value.ToString(CultureInfo.InvariantCulture) + " "); } Console.WriteLine(); ++p; } } Console.WriteLine(); Debug.Assert(!_cmdLine.UsePocketCounts || p == 1326); Kml.KML_Hybrid(&kmParams); double[][] centers = new double[_cmdLine.K][].Fill(i => new double[_cmdLine.Dim]); for (int c = 0; c < kmParams.k; ++c) { for (int d = 0; d < kmParams.dim; ++d) { centers[c][d] = *kmParams.GetCenter(c, d); } } kmParams.Free(); return(centers); }
private void VerifyResult(Kml.Parameters p, double[] points, double[] expCenters, int[] expCenterAssignments) { Assert.AreEqual(expCenters.Length / p.dim, p.k); for (int i = 0; i < p.k; ++i) { for (int d = 0; d < p.dim; ++d) { Assert.AreEqual(expCenters[i * p.dim + d], *p.GetCenter(i, d), 0.001); } } Assert.AreEqual(expCenterAssignments.Length, p.n); int[] centerAssigments = p.CalculateCenterAssignments(); Assert.AreEqual(centerAssigments.Length, p.n); for (int i = 0; i < p.n; ++i) { Assert.AreEqual(expCenterAssignments[i], centerAssigments[i], i.ToString()); } }