Dans cet article on va voir un exemple de graphique SPLOM (Scatter Plot Matrices) réalisé avec Matplotlib. Les graphiques SPLOM sont utilisés en statistique descriptive et également dans le domaine de l'apprentissage automatique (ou machine learning) pour la sélection de caractéristiques. Dans cet exemple (SPLOM de taille (2,2) pour deux caractéristiques: x1 et x2 avec deux classes notées c1 et c2), un premier script python trace tous les éléments de la matrice qui sont donc ici au nombre de 4 (2*2):

from random import gaussimport numpy as npimport matplotlib.pyplot as pltc1_color = (0.69411766529083252, 0.3490196168422699, 0.15686275064945221, 1.0)c2_color = (0.65098041296005249, 0.80784314870834351, 0.89019608497619629, 1.0)#----- Data -----#mean = [0,0]cov = [[10,10],[20,50]]x1_c1,x2_c1 = np.random.multivariate_normal(mean,cov,5000).Tmean = [20,0]cov = [[10,10],[20,50]]x1_c2,x2_c2 = np.random.multivariate_normal(mean,cov,5000).T#--------------- SPLOM ---------------##----- Plot (1,1) -----#min = x1_c1.min()if x1_c2.min() < min:min = x1_c2.min()max = x1_c1.max()if x1_c2.max() > max:max = x1_c2.max()width = 0.5*(max-min)/50hist1 = np.histogram(x1_c1, bins=np.linspace(min, max, 50), normed=1)plt.bar(hist1[1][:-1], hist1[0], width=width,color=c1_color, label='Class 1')hist2 = np.histogram(x1_c2, bins=np.linspace(min, max, 50), normed=1)plt.bar(hist1[1][:-1]+width, hist2[0], width=width,color=c2_color, label='Class 2')plt.xlabel('x1')plt.legend()plt.savefig('SPLOM_11.png')#plt.show()plt.close()#----- Plot (2,1) -----#plt.plot(x1_c1,x2_c1,'o',color=c1_color, label='Class 1')plt.plot(x1_c2,x2_c2,'o',color=c2_color, label='Class 2')plt.axis('equal')plt.xlabel('x1')plt.ylabel('x2')plt.legend()plt.savefig('SPLOM_21.png')#plt.show()plt.close()#----- Plot (1,2) -----#plt.plot(x2_c1,x1_c1,'o',color=c1_color, label='Class 1')plt.plot(x2_c2,x1_c2,'o',color=c2_color, label='Class 2')plt.axis('equal')plt.xlabel('x2')plt.ylabel('x1')plt.legend()plt.savefig('SPLOM_12.png')#plt.show()plt.close()#----- Plot (2,2) -----#min = x2_c1.min()if x2_c2.min() < min:min = x2_c2.min()max = x2_c1.max()if x2_c2.max() > max:max = x2_c2.max()width = 0.5*(max-min)/50hist1 = np.histogram(x2_c1, bins=np.linspace(min, max, 50), normed=1)plt.bar(hist1[1][:-1], hist1[0], width=width,color=c1_color, label='Class 1')hist2 = np.histogram(x2_c2, bins=np.linspace(min, max, 50), normed=1)plt.bar(hist1[1][:-1]+width, hist2[0], width=width,color=c2_color, label='Class 2')plt.xlabel('x2')plt.legend()plt.savefig('SPLOM_22.png')#plt.show()plt.close()
Puis finalement un second script python qui vient créer le graphique SPLOM (figure ci-contre) en utilisant le module PIL de python:
from PIL import Imageim1 = Image.open('SPLOM_11.png')im_size = im1.sizeim2 = Image.open('SPLOM_12.png')im3 = Image.open('SPLOM_21.png')im4 = Image.open('SPLOM_22.png')print "Taille de l'image: ", im_size#---------- Trim Image ----------#pix = im2.load()x_min = im_size[0]x_max = 0y_min = im_size[1]y_max = 0for x in range(im_size[0]):for y in range(im_size[1]):if pix[x,y] != (255, 255, 255, 255) :if x < x_min :x_min = xif x > x_max :x_max = xif y < y_min :y_min = yif y > y_max :y_max = y#print x_min, x_max#print y_min, y_maxadd_pad = 10x_min = x_min - add_padx_max = x_max + add_pady_min = y_min - add_pady_max = y_max + add_padleft = x_mintop = y_minwidth = x_max - x_minheight = y_max - y_minbox = (left, top, left+width, top+height)area = im1.crop(box)area.save("SPLOM_11_adjusted.png", "PNG")area = im2.crop(box)area.save("SPLOM_12_adjusted.png", "PNG")area = im3.crop(box)area.save("SPLOM_21_adjusted.png", "PNG")area = im4.crop(box)area.save("SPLOM_22_adjusted.png", "PNG")#---------- Create Image ----------#im1 = Image.open('SPLOM_11_adjusted.png')im2 = Image.open('SPLOM_12_adjusted.png')im3 = Image.open('SPLOM_21_adjusted.png')im4 = Image.open('SPLOM_22_adjusted.png')im_size = im1.sizenew_im = Image.new('RGB', (im_size[0]*2,im_size[1]*2),(255,255,255))new_im.paste(im1, (0,0))new_im.paste(im2, (im_size[0],0))new_im.paste(im3, (0,im_size[1]))new_im.paste(im4, (im_size[0],im_size[1]))new_im.save("SPLOM.png", "PNG")
Recherches associées
| Liens | Site |
|---|---|
| matplotlib | matplotlib doc |
| An Introduction to Variable and Feature Selection | Paper |
| Sélection de caractéristiques: méthodes et applications de Hassan CHOUAIB | Thesis |
