public static void CalculateDistances(Dataset dataset) { double[,] avgdata = new double[dataset.LabelCount(), dataset.LabelCount()]; int[,] countdata = new int[dataset.LabelCount(), dataset.LabelCount()]; List <Tuple <double[], int> > data = new List <Tuple <double[], int> >(); for (int i = 0; i < dataset.Count(); i++) { data.Add(new Tuple <double[], int>(dataset.GetDatum(i), dataset.GetLabel(i))); } var query = data.GroupBy(x => x.Item2); foreach (IGrouping <int, Tuple <double[], int> > grp1 in query) { foreach (IGrouping <int, Tuple <double[], int> > grp2 in query) { if (grp1 == grp2) { continue; } foreach (var x1 in grp1) { foreach (var x2 in grp2) { double dist = UMath.L1Distance(x1.Item1, x2.Item1); avgdata[x1.Item2, x2.Item2] += dist; countdata[x1.Item2, x2.Item2]++; } } } } for (int i = 0; i < dataset.LabelCount(); i++) { for (int j = 0; j < dataset.LabelCount(); j++) { if (i == j) { continue; } avgdata[i, j] = avgdata[i, j] / (double)countdata[i, j]; } } Console.WriteLine("Distance statistics:"); for (int i = 0; i < dataset.LabelCount(); i++) { for (int j = 0; j < dataset.LabelCount(); j++) { if (i == j) { continue; } Console.WriteLine("Average distance, classes {0}-{1} = {2}", i, j, avgdata[i, j]); } } }