From 4143969ff29fbac8fef840e8a7e34c791c84e2d3 Mon Sep 17 00:00:00 2001 From: mandala21 Date: Wed, 2 Oct 2019 09:30:31 -0300 Subject: [PATCH 1/2] add Nearest Neighbor --- allalgorithms/classification/__init__.py | 0 .../classification/nearest_neighbor.py | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 allalgorithms/classification/__init__.py create mode 100644 allalgorithms/classification/nearest_neighbor.py diff --git a/allalgorithms/classification/__init__.py b/allalgorithms/classification/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/allalgorithms/classification/nearest_neighbor.py b/allalgorithms/classification/nearest_neighbor.py new file mode 100644 index 0000000..f3a0baf --- /dev/null +++ b/allalgorithms/classification/nearest_neighbor.py @@ -0,0 +1,35 @@ +from math import sqrt + +class KNN(): + """ + This is my first classifier :) + """ + def fit(self,data,labels): + self.data = data + self.labels = labels + + return self + + def predict(self,x_test): + predicts = [] + for row in x_test: + label = self.closest(row) + predicts.append(label) + return predicts + + def euc(self,a,b): + return sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2) + + + def closest(self,row): + #initial best distance is alwas first + best_distance = self.euc(row,self.data[0]) + best_index = 0 + #intering in data + for i in range(1,len(self.data)): + dist = self.euc(row,self.data[i]) + if dist < best_distance: + best_distance = dist + best_index = i + print(dist,best_distance, best_index) + return self.labels[best_index] \ No newline at end of file From defe811a4e7802e9f84358b3a4e86a59883b2a12 Mon Sep 17 00:00:00 2001 From: mandala21 Date: Thu, 3 Oct 2019 13:55:02 -0300 Subject: [PATCH 2/2] add test case --- .vscode/settings.json | 3 ++ .../classification/nearest_neighbor.py | 14 +++++-- tests/test_classification.py | 40 +++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 tests/test_classification.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..615aafb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "/usr/bin/python3" +} \ No newline at end of file diff --git a/allalgorithms/classification/nearest_neighbor.py b/allalgorithms/classification/nearest_neighbor.py index f3a0baf..9d018d5 100644 --- a/allalgorithms/classification/nearest_neighbor.py +++ b/allalgorithms/classification/nearest_neighbor.py @@ -1,8 +1,17 @@ +# -*- coding: UTF-8 -*- +# +# Nearest neighbour classifier one entry +# The All â–²lgorithms library for python +# +# Contributed by: Carlos Abraham Hernandez +# Github: @abranhe +# + from math import sqrt -class KNN(): +class NN(): """ - This is my first classifier :) + Nearest neighbour classifier for two-dimensional dataset """ def fit(self,data,labels): self.data = data @@ -31,5 +40,4 @@ def closest(self,row): if dist < best_distance: best_distance = dist best_index = i - print(dist,best_distance, best_index) return self.labels[best_index] \ No newline at end of file diff --git a/tests/test_classification.py b/tests/test_classification.py new file mode 100644 index 0000000..3828e05 --- /dev/null +++ b/tests/test_classification.py @@ -0,0 +1,40 @@ +import unittest +from allalgorithms.classification import nearest_neighbor + +class TesteClassifications(unittest.TestCase): + + def test_nn(self): + #datas + #the first data is Weight and second is number of wheels + features = [ + [110,2], + [125,2], + [100,2], + [110,2], + [300,4], + [278,4], + [290,4], + [260,4], + ] + #labels, the labels is classification of features line + #in this exemple 0 = Motorcicler, 1 = car + label = [ + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + ] + #instance classifier + clf = nearest_neighbor.NN() + #treaning + clf = clf.fit(features,label) + #predict + rs = clf.predict([[2,115]]) + self.assertEqual(rs,0) + +if __name__ == "__main__": + unittest.main() \ No newline at end of file