def calculateProbabilityImpreciseKernel(dataset, h, epsilon, N): # TRIER LES DONNEES DU DATASET ET ENSUITE MAJ LA SOMME A CHAQUE ITERATION :) # sortedDataset = sorted(dataset) n = len(dataset) # print('n=', n) stepLinspace = 0.05 # ATTENTION Mettre au moins 0.05 pours les vrai tests ! lowProbabilities = [] hightProbabilities = [] # print('DATASET AVANT PASSAGE DANS KERNEL :', dataset) tKernelTri = KernelContext(dataset, TriangularKernel(h), stepLinspace) for pt in tKernelTri.domain: # Def des structures qui vont récolter les données (dans la boucle pour une remise à 0 à chaque cycle # lenDomain.append(len(tKernelTri.domain)) structHMin = {'potentialHValue': -1, 'minValue': -1} structHMax = {'potentialHValue': -1, 'maxedValue': -1} # Calculs de f(hMax), et f(hMin) structHMax = tKernelTri.computeHMaxFromInterval(pt, h, epsilon) structHMin = tKernelTri.computeHMinFromInterval(pt, h, epsilon) hightProbabilities.append(structHMax['maxedValue']) lowProbabilities.append(structHMin['minValue']) # print('Hight probability = ', hightProbability) return lowProbabilities, hightProbabilities, tKernelTri.domain
while appending == False: tirAlea = random.randint(0, 20) if tirAlea != centerPoint: appending = True experimentalSample.append(tirAlea) tKernel2 = TriangularKernel(0.1) #test = TriangularKernel.testUnitaires() #print(test) if 0 == 0: dist2 = [] extremizer = KernelContext(experimentalSample, tKernel2) maxStruct = extremizer.computeHMax(centerPoint) distances = [] for ind, i in enumerate(experimentalSample): distance = abs(i - centerPoint) plt.axvline(x=distance * 2, color='red') distances.append(distance * 2) plt.axvline(x=maxStruct['potentialHValue'], color="green") #distances = sorted(distances) #for ind,p in enumerate(distances): #if ind != 9: #plt.axvline(x=(distances[ind+1]+p)/2, color='green')
from statistics import stdev from classes.Kernels.TriangularKernel import TriangularKernel from classes.KernelContext import KernelContext from classes.SampleGenerator.MultimodalGenerator import MultimodalGenerator """Génération de la multimodale de test (faite à la main)""" sampleTestUni = [2, 2.62, 2.84, 2.95, 4.3, 4.7, 4.97, 5.31, 5.7, 7] """Déclaration des structures devant contenir le min et le max de h et de f(h)""" maxStruct = {'potentialHValue': -1, 'maxedValue': -1} maxStructFromInt_1 = {'potentialHValue': -1, 'minValue': -1} minStructFromInt_1 = {'potentialHValue': -1, 'minValue': -1} """Déclaraiton du Kernel utilisé """ TKernel = KernelContext(sampleTestUni, TriangularKernel(0.1), 1) """Définition des matrices des résultats""" # ComputeHMax WaitedHMaxResults_0 = [[8.478, .058976174], [6.478, .077184316], [1.205, 0.16975104], [.1, .5], [2.348888889, 0.19157943], [.06, .8333333333], [2.006666666666, .149501661], [5.522, .090546903], [7.522, .066471683], [9.522, .052509977]] # ComputeHMax/MinFromIntervalle avec h=2 et eps = 0.5 WaitedHMaxResults_1 = [[2.5, .008], [2.5, .05744], [1.5, 0.15955555], [1.5, .2048888889], [2.348888889, 0.19157943], [1.5, .24266666], [2.006666666666, .149501661], [2.5, .08288], [2.5, .0272], [2.5, 0.008]]
sample = MultimodalGenerator([(nbPointsFirstGauss,-1,1),(nbPointsSecondGauss,5,2)]).generateNormalSamples() # Def du hOpt sigma=stdev(sample) hOpt = 1.06*sigma*(nbPointsFirstGauss+nbPointsSecondGauss)**(-1/5) #print("hopt",hOpt) # Def epsilon #epsilon = 0.2*hOpt #mettre ce paramettre en fonction du hOpt trouvé. #def Kernel tKernelTri = KernelContext(sample,TriangularKernel(hOpt),stepLinspace) # Def des tableau qui vont stocker les valeurs des f(hMax), f(hOpt) et f(hMin) for epsilon in (hOpt*.05, hOpt*.1, hOpt*.2, hOpt*.4, hOpt*.6, hOpt*.9): if epsilon < hOpt*.1 : yTriHOptOnDomain = [] yTriHMaxOnDomain = [] yTriHMinOnDomain = [] yInitialBimodal = [] lenDomain=[] for pt in tKernelTri.domain: # Def des structures qui vont récolter les données (dans la boucle pour une remise à 0 à chaque cycle lenDomain.append(len(tKernelTri.domain))
sample = MultimodalGenerator([(100, -1, 1), (400, 5, 2)]).generateNormalSamples() tKernelTri = TriangularKernel(hTestTri) tKernelEll = EllipseKernel(hTestEll) defDomain = np.linspace(-4, 12, 200) yTriOnDomain = [] yTriHMinOnDomain = [] yTriHMaxOnDomain = [] yEllOnDomain = [] yEllHMinOnDomain = [] yEllHMaxOnDomain = [] """ TEST CODE PIERRE """ kc = KernelContext(sample, tKernelTri) ftest, domain_test = kc.computeTotalDensity() maxstru = kc.computeHMax(5) print(maxstru) kc.setBandwidth(maxstru['potentialHValue']) ftest2, domain_test2 = kc.computeTotalDensity() """ FIN DU TEST """ for x in defDomain: fx = 0 gx = 0 hx = 0 hMinTri = 0 hMaxTri = 0
for e in range(nbTotalIterations): # On commence par générer des données sample1 = MultimodalGenerator([(100,2,2),(100,8,2)]).generateNormalSamples() sample2 = MultimodalGenerator([(100,5,2)]).generateNormalSamples() # On sait que sur ce jeu de données, les 200 premières données sont de la classe A, et les 100 dernières sont de la classe 2 # Recherche d'une largeur optimale sigma1=stdev(sample1) sigma2=stdev(sample2) hOpt = 1.06 * (sigma1*2+sigma2)/3 * (300) ** (-1 / 5) # Création de notre kernel epaKernel = EpanechnikovKernel(hOpt) # On génère la fonction de densité de la classe A AClassContext = KernelContext(sample1, epaKernel) AClassDensity, AClassDensityDomain = AClassContext.computeTotalDensity() # Pareil pour la classe B BClassContext = KernelContext(sample2, epaKernel) BClassDensity, BClassDensityDomain = BClassContext.computeTotalDensity() # On génére notre modèle NB gnb = GaussianNB() sample = [] sample.extend(sample1) sample.extend(sample2) labels = [] for i in range(200): labels.append("A") for i in range(100):