You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3nar/demo4.py

69 lines
1.7 KiB

import os
import sys
parent_dir_name = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(parent_dir_name + "/3nar/code")
from nnnar import *
from knn import *
import numpy as np
import pandas as pd
from time import time
df = pd.read_csv('./data/Iris.csv')
df = df.iloc[:, 1:]
# Normalisation des données
df.iloc[:, 0] = df.iloc[:, 0] - df.iloc[:, 0].min()
df.iloc[:, 1] = df.iloc[:, 1] - df.iloc[:, 1].min()
df.iloc[:, 2] = df.iloc[:, 2] - df.iloc[:, 2].min()
df.iloc[:, 3] = df.iloc[:, 3] - df.iloc[:, 3].min()
df.iloc[:, 0] = df.iloc[:, 0] / df.iloc[:, 0].max()
df.iloc[:, 1] = df.iloc[:, 1] / df.iloc[:, 1].max()
df.iloc[:, 2] = df.iloc[:, 2] / df.iloc[:, 2].max()
df.iloc[:, 3] = df.iloc[:, 3] / df.iloc[:, 3].max()
df.iloc[:, 0:4] = df.iloc[:, 0:4] * 100
def runOneTest(model,df):
# Création des données d'entrainement et de test
train = df.sample(frac=0.8)
test = df.drop(train.index)
# Entrainement du modèle
trainCoord = train.iloc[:, :-1].values
trainValue = train.iloc[:, -1].values
# Test du modèle
coord = test.iloc[:, :-1].values
value = test.iloc[:, -1].values
for i in range(len(trainCoord)):
model.addPoint(np.array(trainCoord[i]), np.array([trainValue[i]]))
nbError = 0
for i in range(len(coord)):
if model.getLabelOfPoint(np.array(coord[i]), 5) != value[i]:
nbError += 1
return 100 - nbError / len(coord) * 100
if (sys.argv[1] == "knn"):
model = Knn()
else:
model = Nnnar(4, 0, 100.001, 5)
t1 = time()
nbRepetition = 100
accuracy = 0
for i in range(nbRepetition):
model.reset()
accuracy += runOneTest(model,df)
t2 = time()
print("accuracy moyenne:",str(accuracy/nbRepetition))
print("delta temps:",str(t2-t1))