ajout de knn pour la monoclassification et calcul du temp

demo/4
ludovic.castglia 4 months ago
parent 0730fd8b91
commit edb706659f

@ -25,6 +25,18 @@ class Knn:
value[i] = value[i] / ttPart value[i] = value[i] / ttPart
return value return value
def getLabelOfPoint(self, coord,nbNearest):
points, dists = self.getNNearest(coord, nbNearest)
label = {}
for idx in range(len(points)):
tvalue = points[idx].value[0]
if tvalue in label:
label[tvalue] += 1/(dists[idx]+1)
else:
label[tvalue] = 1/(dists[idx]+1)
value = max(label, key=label.get)
return value
def getNNearest(self, coord, nbNearest): def getNNearest(self, coord, nbNearest):
dist = np.copy(self.space) dist = np.copy(self.space)
dist = np.frompyfunc(lambda x: x.getDistFromCoord(coord), 1, 1)(dist) dist = np.frompyfunc(lambda x: x.getDistFromCoord(coord), 1, 1)(dist)

@ -2,11 +2,11 @@ from nnnar import *
from knn import * from knn import *
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from time import time
def runOneTest(trainTestRatio): def runOneTest(trainTestRatio):
nnnar = Nnnar(4, 0, 100.001, 10)# le 100.001 est pour être sûr d'être au dessus de la valeur max (100.0) nnnar = Nnnar(4, 0, 100.001, 10)# le 100.001 est pour être sûr d'être au dessus de la valeur max (100.0)
knn = Knn()
df = pd.read_csv('./data/Iris.csv') df = pd.read_csv('./data/Iris.csv')
df = df.iloc[:, 1:] df = df.iloc[:, 1:]
# Normalisation des données # Normalisation des données
@ -45,9 +45,14 @@ def runOneTest(trainTestRatio):
return 100 - nbError / len(coord) * 100 return 100 - nbError / len(coord) * 100
t1 = time()
nbRepetition = 100 nbRepetition = 100
accuracy = 0 accuracy = 0
for i in range(nbRepetition): for i in range(nbRepetition):
accuracy += runOneTest(0.8) accuracy += runOneTest(0.8)
print("accuracy moyenne: ",str(accuracy/nbRepetition)) t2 = time()
print("accuracy moyenne: ",str(accuracy/nbRepetition))
print("delta temps: ",str(t2-t1))
Loading…
Cancel
Save