2018年11月10日 星期六

[ ML 文章收集 ] 7 ways to label a cluster plot in Python

Source From Here
Preface
This tutorial shows you 7 different ways to label a scatter plot with different groups (or clusters) of data points. I made the plots using the Python packages matplotlib and seaborn, but you could reproduce them in any software. These labeling methods are useful to represent the results of clustering algorithms, such as k-means clustering, or when your data is divided up into groups that tend to cluster together.

How-To
Here's a sneak peek of some of the plots:


You can access the Juypter notebook I used to create the plots here. I also embedded the code below. First, we need to import a few libraries and define some basic formatting:
  1. import pandas as pd  
  2. import numpy as np  
  3.   
  4. import seaborn as sns  
  5. import matplotlib.pyplot as plt  
  6. %matplotlib inline  
  7.   
  8. #set font size of labels on matplotlib plots  
  9. plt.rc('font', size=16)  
  10.   
  11. #set style of plots  
  12. sns.set_style('white')  
  13.   
  14. #define a custom palette  
  15. customPalette = ['#630C3A''#39C8C6''#D3500C''#FFB139']  
  16. sns.set_palette(customPalette)  
  17. sns.palplot(customPalette)  
CREATE LABELED GROUPS OF DATA
Next, we need to generate some data to plot. I defined four groups (A, B, C, and D) and specified their center points. For each label, I sampled nx2 data points from a gaussian distribution centered at the mean of the group and with a standard deviation of 0.5. To make these plots, each datapoint needs to be assigned a label. If your data isn't labeled, you can use a clustering algorithm to create artificial groups:
  1. #number of points per group  
  2. n = 50  
  3.   
  4. #define group labels and their centers  
  5. groups = {'A': (2,2),  
  6.           'B': (3,4),  
  7.           'C': (4,4),  
  8.           'D': (4,1)}  
  9.   
  10. #create labeled x and y data  
  11. data = pd.DataFrame(index=range(n*len(groups)), columns=['x','y','label'])  
  12. for i, group in enumerate(groups.keys()):  
  13.     #randomly select n datapoints from a gaussian distrbution  
  14.     data.loc[i*n:((i+1)*n)-1,['x','y']] = np.random.normal(groups[group],   
  15.                                                            [0.5,0.5],   
  16.                                                            [n,2])  
  17.     #add group labels  
  18.     data.loc[i*n:((i+1)*n)-1,['label']] = group  
  19.   
  20. data.head()  


STYLE 1: STANDARD LEGEND
Seaborn makes it incredibly easy to generate a nice looking labeled scatter plot. This style works well if your data points are labeled, but don't really form clusters, or if your labels are long.
  1. #plot data with seaborn  
  2. facet = sns.lmplot(data=data, x='x', y='y', hue='label',   
  3.                    fit_reg=False, legend=True, legend_out=True)  

STYLE 2: COLOR-CODED LEGEND
This is a slightly fancier version of style 1 where the text labels in the legend are also color-coded. I like using this option when I have longer labels. When I'm going for a minimal look, I'll drop the colored bullet points in the legend and only keep the colored text.
  1. #plot data with seaborn (don't add a legend yet)  
  2. facet = sns.lmplot(data=data, x='x', y='y', hue='label',   
  3.                    fit_reg=False, legend=False)  
  4.   
  5. #add a legend  
  6. leg = facet.ax.legend(bbox_to_anchor=[10.75],  
  7.                          title="label", fancybox=True)  
  8. #change colors of labels  
  9. for i, text in enumerate(leg.get_texts()):  
  10.     plt.setp(text, color = customPalette[i])  

STYLE 3: COLOR-CODED TITLE
This option can work really well in some contexts, but poorly in others. It probably isn't a good option if you have a lot of group labels or the group labels are very long. However, if you have only 2 or 3 labels, it can make for a clean and stylish option. I would use this type of labeling in a presentation or in a blog post, but I probably wouldn't use in more formal contexts like an academic paper:
  1. #plot data with seaborn  
  2. facet = sns.lmplot(data=data, x='x', y='y', hue='label',   
  3.                    fit_reg=False, legend=False)  
  4.   
  5. #define padding -- higher numbers will move title rightward  
  6. pad = 4.5  
  7.   
  8. #define separation between cluster labels  
  9. sep = 0.3  
  10.   
  11. #define y position of title  
  12. y = 5.6  
  13.   
  14. #add beginning of title in black  
  15. facet.ax.text(pad, y, 'Distributions of points in clusters:',   
  16.               ha='right', va='bottom', color='black')  
  17.   
  18. #add color-coded cluster labels  
  19. for i, label in enumerate(groups.keys()):  
  20.     text = facet.ax.text(pad+((i+1)*sep), y, label,   
  21.                          ha='right', va='bottom',  
  22.                          color=customPalette[i])  

STYLE 4: LABELS NEXT TO CLUSTERS
This is my favorite style and the labeling scheme I use most often. I generally like to place labels next to the data instead of in a legend. The only draw back of this labeling scheme is that you need to hard code where you want the labels to be positioned:
  1. #define labels and where they should go  
  2. labels = {'A': (1.25,1),  
  3.           'B': (2.25,4.5),  
  4.           'C': (4.75,3.5),  
  5.           'D': (4.75,1.5)}  
  6.   
  7. #create a new figure  
  8. plt.figure(figsize=(5,5))  
  9.   
  10. #loop through labels and plot each cluster  
  11. for i, label in enumerate(groups.keys()):  
  12.   
  13.     #add data points   
  14.     plt.scatter(x=data.loc[data['label']==label, 'x'],   
  15.                 y=data.loc[data['label']==label,'y'],   
  16.                 color=customPalette[i],   
  17.                 alpha=0.7)  
  18.       
  19.     #add label  
  20.     plt.annotate(label,   
  21.                  labels[label],  
  22.                  horizontalalignment='center',  
  23.                  verticalalignment='center',  
  24.                  size=20, weight='bold',  
  25.                  color=customPalette[i])   


STYLE 5: LABELS CENTERED ON CLUSTER MEANS
This style is advantageous if you care more about where the cluster means are than the locations of the individual points. I made the points more transparent to improve the visibility of the labels:
  1. #create a new figure  
  2. plt.figure(figsize=(5,5))  
  3.   
  4. #loop through labels and plot each cluster  
  5. for i, label in enumerate(groups.keys()):  
  6.   
  7.     #add data points   
  8.     plt.scatter(x=data.loc[data['label']==label, 'x'],   
  9.                 y=data.loc[data['label']==label,'y'],   
  10.                 color=customPalette[i],   
  11.                 alpha=0.20)  
  12.       
  13.     #add label  
  14.     plt.annotate(label,   
  15.                  data.loc[data['label']==label,['x','y']].mean(),  
  16.                  horizontalalignment='center',  
  17.                  verticalalignment='center',  
  18.                  size=20, weight='bold',  
  19.                  color=customPalette[i])   


STYLE 6: LABELS CENTERED ON CLUSTER MEANS (2) 
This style is similar to style 5, but relies on a different way to improve label visibility. Here, the background of the labels are color-coded and the text is white: 
  1. #create a new figure  
  2. plt.figure(figsize=(5,5))  
  3.   
  4. #loop through labels and plot each cluster  
  5. for i, label in enumerate(groups.keys()):  
  6.   
  7.     #add data points   
  8.     plt.scatter(x=data.loc[data['label']==label, 'x'],   
  9.                 y=data.loc[data['label']==label,'y'],   
  10.                 color=customPalette[i],   
  11.                 alpha=1)  
  12.       
  13.     #add label  
  14.     plt.annotate(label,   
  15.                  data.loc[data['label']==label,['x','y']].mean(),  
  16.                  horizontalalignment='center',  
  17.                  verticalalignment='center',  
  18.                  size=20, weight='bold',  
  19.                  color='white',  
  20.                  backgroundcolor=customPalette[i])   



STYLE 7: TEXT MARKERS
This style is a little bit odd, but it can be effective in some situations. This type of labeling scheme may be useful when there are few data points and the labels are very short.
  1. #create a new figure and set the x and y limits  
  2. fig, axes = plt.subplots(figsize=(5,5))  
  3. axes.set_xlim(0.5,5.5)  
  4. axes.set_ylim(-0.5,5.5)  
  5.   
  6. #loop through labels and plot each cluster  
  7. for i, label in enumerate(groups.keys()):  
  8.   
  9.     #loop through data points and plot each point   
  10.     for l, row in data.loc[data['label']==label,:].iterrows():  
  11.       
  12.         #add the data point as text  
  13.         plt.annotate(row['label'],   
  14.                      (row['x'], row['y']),  
  15.                      horizontalalignment='center',  
  16.                      verticalalignment='center',  
  17.                      size=11,  
  18.                      color=customPalette[i])   

Supplement 
Basic Visualization and Clustering in Python 

沒有留言:

張貼留言

[Git 文章收集] Differences between git merge and git rebase

Source From  Here Preface Merging and rebasing are the two most popular way to applying changes from one branch into another one. They bot...