import math
import csv
class Kneareast:
def __init__(self, k, lista_treino1, lista_treino2, lista_a_ser_testada1, lista_a_ser_testada2):
self.k = k
self.data_base = []
self.train1 = lista_treino1
self.test1 = lista_a_ser_testada1
self.train2 = lista_treino2
self.test2 = lista_a_ser_testada2
def management(self):
for x in self.train1:
self.data_base.append(x)
predicts_test1 = self.cicle(self.data_base, self.test1)
self.sucesso(1, self.train1, predicts_test1)
predicts_test2 = self.cicle(self.data_base, self.train2)
self.sucesso(2, self.train2, predicts_test2)
predicts_test2 = self.cicle(self.data_base, self.test2)
self.sucesso(3, self.test2, predicts_test2)
return 0
def euclidean_distance(self, y1, y2):
euc = pow(y1 - y2, 2)
return euc
def cicle(self, lista1, lista2):
predicts = []
i = 0
while i < len(lista2):
j = 0
lista_minimos = []
while j < len(lista1):
n = 0
sumt = 0
while n < (len(lista1[j])-1):
sumt = sumt + self.euclidean_distance(lista1[j][n], lista2[i][n])
n = n + 1
dist = math.sqrt(sumt)
if len(lista_minimos) >= self.k:
if dist < lista_minimos[-1][0]:
lista_minimos.pop(-1)
lista_minimos.append([dist, lista1[i][-1]])
else:
lista_minimos.append([dist, lista1[i][-1]])
lista_minimos.sort()
j = j + 1
a = 0
lista_caract = []
while a < len(lista_minimos):
lista_caract.append(lista_minimos[a][-1])
a = a + 1
max = None
a = 0
while a < len(lista_caract):
if max == None:
max = [lista_caract.count(lista_caract[a]), lista_caract[a]]
else:
if max[0] < lista_caract.count(lista_caract[a]):
max = [lista_caract.count(lista_caract[a]), lista_caract[a]]
a = a + 1
predicts.append(max[1])
self.data_base.append(lista2[i])
i = i + 1
return predicts
def sucesso(self, number, lista, predicts):
certo = 0
i = 0
while i < len(lista):
if lista[i][-1] == predicts[i]:
certo += 1
i = i + 1
print(number, predicts, (certo/len(lista))*100)
return 0
this can be considered as a k-nearest neighbors algorithm in Python?
And why every time I use a base date that consists of 700 elements with this structure: [2.0, 118.0, 80.0, 0.0, 0.0, 42.9, 0.693, 21.0, 1.0], the last digit being the classification of the element, binary classification (1 or 0), no matter how much I increase the number of neighbors there is no difference in the percentage of success, being exactly the same in all attempts? Is some Logic problem?
New contributor
MattandMello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.