public void TestKnnMockData() { double[,] stringdata = new Double[6, 2] { { 1, 1 }, { 1, 3 }, { 2, 1 }, { 3, 4 }, { 4, 5 }, { 5, 5 } }; int[] prediction = new int[6] { 1, 1, 1, 0, 0, 0 }; double[,] predict = new double[2, 2] { { 0, 0 }, { 4, 5 } }; Knn Model = new Knn(); Model.Fit(stringdata, prediction); int[] predicted; predicted = Model.Predict(predict); int[] ShoudBe = new int[predicted.Length]; ShoudBe[0] = 1; ShoudBe[1] = 0; for (int i = 0; i < predicted.GetLength(0); i++) { AreEqual(predicted[i], ShoudBe[i]); } }
public void DatasetBenchmarkKNN() { int featuresCount = 8; const string path = BasePath + "\\heart.csv"; var lineCount = File.ReadLines(path).Count(); var reader = new StreamReader(File.OpenRead(path)); var properties = new double[lineCount, featuresCount + 1]; for (var i2 = 0; i2 < lineCount; i2++) { var line = reader.ReadLine(); for (var i = 0; i < featuresCount + 1; i++) { if (line == null) { continue; } var values = line.Split(','); properties[i2, i] = Convert.ToDouble(values[i]); } } var xTrain = new double[lineCount, featuresCount]; var yTrain = new int[lineCount]; for (var i = 0; i < lineCount; i++) { for (var j = 0; j < featuresCount; j++) { xTrain[i, j] = properties[i, j]; } yTrain[i] = (int)properties[i, featuresCount]; } var model = new Knn(); model.Fit(xTrain, yTrain); WriteLine("Training data:"); var predicted = model.Predict(xTrain); WriteLine("Accuracy:"); var accuracy = Accuracy(predicted, yTrain); WriteLine(accuracy); Greater(accuracy, 0.70); }