static void PredictByKNN(string trainPath, string[] classes, int k, string testPath, int skiplines, string predictPath, KNearestNeighbors.MetricType type, string headline = "") { StreamReader train = new StreamReader(trainPath); for (int i = 0; i < skiplines; i++) { train.ReadLine(); } List <Vector> inputData = new List <Vector>(); List <int> outputData = new List <int>(); Console.WriteLine("Read train data\n"); int rowNumber = 1; while (!train.EndOfStream) { string line = train.ReadLine(); string[] splited = line.Split(','); Vector input = new Vector(splited.Length - 1); int index = Array.IndexOf(classes, splited[0]); if (index == -1) { throw new Exception("Can't find class for '" + splited[0] + "'"); } outputData.Add(index); for (int i = 0; i < input.Length; i++) { input[i] = 0.01 + double.Parse(splited[i + 1]) / 255 * 0.99; } inputData.Add(input); Console.SetCursorPosition(0, Console.CursorTop - 1); Console.WriteLine("Read train row{0}", rowNumber); rowNumber++; } KNearestNeighbors KNN = new KNearestNeighbors(inputData.ToArray(), outputData.ToArray(), classes.Length, k, type); StreamReader reader = new StreamReader(testPath); StreamWriter writer = new StreamWriter(predictPath); for (int i = 0; i < skiplines; i++) { reader.ReadLine(); } if (headline.Length > 0) { writer.WriteLine(headline); } rowNumber = 1; Console.WriteLine("Classifing:\n"); while (!reader.EndOfStream) { string line = reader.ReadLine(); string[] splited = line.Split(','); Vector input = new Vector(splited.Length); for (int i = 0; i < input.Length; i++) { input[i] = 0.01 + double.Parse(splited[i]) / 255 * 0.99; // 0.01 .. 1 } int index = KNN.GetClassIndex(input); Console.SetCursorPosition(0, Console.CursorTop - 1); Console.WriteLine("Classified row{0} ({1})", rowNumber, classes[index]); writer.WriteLine("{0},{1}", rowNumber, classes[index]); rowNumber++; } reader.Close(); writer.Close(); }