From c9e82a395d8da21772dc160ad59521fa2acd39f9 Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Sun, 5 May 2019 15:50:24 +0200 Subject: [PATCH] mise en forme du code --- 455-Codage_Sources/Cours/chapA.tex | 6 +++++- 455-Codage_Sources/algo_code/LBG.py | 16 +++++----------- 455-Codage_Sources/algo_code/llyod_max.py | 9 ++------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/455-Codage_Sources/Cours/chapA.tex b/455-Codage_Sources/Cours/chapA.tex index 7b11b0f..35b0cf3 100644 --- a/455-Codage_Sources/Cours/chapA.tex +++ b/455-Codage_Sources/Cours/chapA.tex @@ -14,10 +14,14 @@ \section{Quantification} \subsection{Quantification uniforme} \inputminted{python}{../algo_code/quantif.py} +\newpage \subsection{Algorithme de Llyod-max} \inputminted{python}{../algo_code/llyod_max.py} +\newpage \subsection{Algorithme LBG} -en 2D , ne pas essayer de tracer les cellule de voronoi +\inputminted{python}{../algo_code/LBG.py} + + \section{Codeur prédictif} Construire un schéma de prédiction en boucle fermée à fenêtre glissante dasn lequel $M$et $p$ sont paramétrisable. On utilisera un quantificateur à zone morte de pas $\Delta$ paramétrisable. \section{KLT} diff --git a/455-Codage_Sources/algo_code/LBG.py b/455-Codage_Sources/algo_code/LBG.py index 35b36f2..0291353 100755 --- a/455-Codage_Sources/algo_code/LBG.py +++ b/455-Codage_Sources/algo_code/LBG.py @@ -2,9 +2,8 @@ # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt - from scipy.spatial import Voronoi, voronoi_plot_2d -# +#initialisations clusters M = 20; N =100; #point par cluster K = N*M @@ -17,15 +16,12 @@ for m in range(M): X[m*N:(m+1)*N] = xi plt.plot(xi[:,0],xi[:,1],'+') plt.plot(means[:,0],means[:,1],'ob') - mean= np.mean(X,axis=0) - Y0 = np.random.multivariate_normal(mean, 10*cov, M) plt.show() -print(Y0) -Y0= means +Y0= means #triche plt.plot(Y0[:,0],Y0[:,1],'ok') - +plt.show() def LBG(X,Y0,eps=1e-5,maxiter=1000): Y = Y0.copy() old_dist = np.inf @@ -41,17 +37,15 @@ def LBG(X,Y0,eps=1e-5,maxiter=1000): dist += sum((X[k]-quant_min)**2) for j in range(len(Y)): Y[j,:] = np.mean(X[cluster_index==j],axis=0) - print(Y) if dist-old_dist < eps: break else: old_dist = dist return Y Y = LBG(X,Y0) -vor = Voronoi(Y) +vor = Voronoi(Y)# black magic voronoi_plot_2d(vor,show_vertices=False) -print(Y) plt.plot(X[:,0],X[:,1],'+') plt.plot(Y[:,0],Y[:,1],'ob') plt.plot(Y0[:,0],Y0[:,1],'ok') -plt.show() +plt.show() \ No newline at end of file diff --git a/455-Codage_Sources/algo_code/llyod_max.py b/455-Codage_Sources/algo_code/llyod_max.py index 0c9d1a9..2337d8b 100755 --- a/455-Codage_Sources/algo_code/llyod_max.py +++ b/455-Codage_Sources/algo_code/llyod_max.py @@ -5,8 +5,7 @@ from scipy.stats import norm import matplotlib.pyplot as plt def ddp(x): - mean = 0, - sigma = 1 + mean = 0; sigma = 1 return norm.pdf(x,mean,sigma) def quant(centroids, X): @@ -34,7 +33,6 @@ def llyodMax(X,M,maxiter=1000,eps=1e-6): /integrate.quad(lambda x: ddp(x),bornes[i],bornes[i+1])[0] bornes[1:-1] = (centroids[:-1]+centroids[1:])/2 err = np.linalg.norm(centroids-old_centroids) - print(err) if err < eps : break return centroids @@ -43,10 +41,7 @@ M = 4 X = np.random.normal(0,1,1000) centroids = llyodMax(X,M) bornes = (centroids[:-1]+centroids[1:])/2 -bornes = np.insert(bornes,0,-np.inf) -bornes = np.append(bornes,np.inf) - -print(centroids, bornes) +bornes = np.insert(bornes,0,-np.inf); bornes = np.append(bornes,np.inf) plt.figure() plt.plot(X) plt.plot(quant(bornes,X))