Mis a jour le 2025-04-14, 12:10

Clustering avec SciPy

Clustering hiérarchique :
  • faire from scipy.cluster import hierarchy
  • clustering : cluster = hierarchy.linkage(df, metric = 'euclidean', method = 'average')
  • on peut aussi faire clustering = hierarchy.linkage(x, method = 'single', metric = 'euclidean') avec x le retour de pdist, ou aussi la matrice des observations (m observations x n paramètres)
  • method peut être single, average, ward, etc ...
  • puis, représentation du dendrogramme : hierarchy.dendrogram(cluster, orientation = 'left', labels = df.columns, color_threshold = 0)
  • on peut colorer les labels en fonction d'un critère :
    for label in pyplot.gca().get_ymajorticklabels():
      if 'A' in label.get_text():      
        label.set_color('red')
         

Copyright python-simple.com
programmer en python, tutoriel python, graphes en python, Aymeric Duclert