#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Fri Dec 3 15:28:19 2021 @author: pfaure """ import os import matplotlib.pyplot as plt import scipy.cluster.hierarchy as shc def print_3d_data(data, dataset_name: str = "", method_name: str = "", k: int = 0, stop: bool = True, save: bool = False, c=None): f0 = data[:, 0] # tous les élements de la première colonne f1 = data[:, 1] # tous les éléments de la deuxième colonne f2 = data[:, 2] # tous les éléments de la troisième colonne fig = plt.figure() ax = fig.gca(projection='3d') # Affichage en 3D if c is None: ax.scatter(f0, f1, f2, label='Courbe', marker='d') plt.title("Données initiales : " + dataset_name) else: ax.scatter(f0, f1, f2, c=c, label='Courbe', marker='d') plt.title("Graphique de " + str(k) + " clusters avec la méthode " + method_name + " sur le jeu de données " + dataset_name) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.tight_layout() if save: if c is None: save_path = "IMG/DATA_VISUALISATION/" if not os.path.exists(save_path): os.makedirs(save_path) plt.savefig(save_path + dataset_name + ".png") else: save_path = "IMG/" + method_name + "/" + dataset_name + "/CLUSTERS/" if not os.path.exists(save_path): os.makedirs(save_path) plt.savefig(save_path + "k=" + str(k) + ".png") plt.close() else: plt.show(block=stop) def print_2d_data(data, dataset_name: str = "", method_name: str = "", k = 0, stop: bool = True, save: bool = False, c=None): f0 = data[:, 0] # tous les élements de la première colonne f1 = data[:, 1] # tous les éléments de la deuxième colonne plt.figure() # utilisation d'une décimale si float k_str = str(round(k, 1)) if isinstance(k, float) else str(k) # plt.hist2d(f0, f1) if c is None: plt.scatter(f0, f1, s=8) plt.title("Données initiales : " + dataset_name) else: plt.scatter(f0, f1, c=c, s=8) plt.title("Graphique de " + k_str + " clusters avec la méthode " + method_name + " sur le jeu de données " + dataset_name) if save: if c is None: save_path = "IMG/DATA_VISUALISATION/" if not os.path.exists(save_path): os.makedirs(save_path) plt.savefig(save_path + dataset_name + ".png") else: save_path = "IMG/" + method_name + "/" + dataset_name + "/CLUSTERS/" if not os.path.exists(save_path): os.makedirs(save_path) plt.savefig(save_path + "k=" + k_str + ".png") plt.close() else: plt.show(block=stop) def print_1d_data(x, y, x_name: str = "toto", y_name: str = "tata", x_unit: str = "", y_unit: str = "", dataset_name: str = "", method_name: str = "", stop: bool = True, save: bool = False): plt.figure() plt.plot(x, y) plt.title(y_name + " = f(" + x_name + ") pour " + method_name + " sur les données " + dataset_name) plt.xlabel(x_name + " (" + x_unit + ")") plt.ylabel(y_name + " (" + y_unit + ")") if save: save_path = "IMG/" + method_name + "/" + dataset_name + "/EVALUATION/" if not os.path.exists(save_path): os.makedirs(save_path) plt.savefig(save_path + y_name + ".png") plt.close() else: plt.show(block=stop) def print_dendrogramme(data, dataset_name: str = "", linkage: str = "", stop: bool = True, save: bool = False): distance = shc.linkage(data, linkage) plt.figure(figsize=(12, 12)) shc.dendrogram(distance, orientation='top', distance_sort='descending', show_leaf_counts=False) plt.title("Dendrogramme du jeu de données " + dataset_name + " avec le linkage " + linkage) if save: save_path = "IMG/DENDROGRAMME/" + linkage + "/" if not os.path.exists(save_path): os.makedirs(save_path) plt.savefig(save_path + dataset_name + ".png") plt.close() else: plt.show(block=stop)