Issue
scikit-learn gives an example of python code to generate a dendogram. I copy/paste this code bellow. This code generates a dendogram. This dendogram display 3 differents colors: blue, green, and orange.
Question: which code associated with this dendogram code example, could automaticaly deliver:
- the number of colors generated by the dendrogram ?
- the list of those of those colors (or their code number) ?
import numpy as np
from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import dendrogram
from sklearn.datasets import load_iris
from sklearn.cluster import AgglomerativeClustering
def plot_dendrogram(model, **kwargs):
# Create linkage matrix and then plot the dendrogram
# create the counts of samples under each node
counts = np.zeros(model.children_.shape[0])
n_samples = len(model.labels_)
for i, merge in enumerate(model.children_):
current_count = 0
for child_idx in merge:
if child_idx < n_samples:
current_count += 1 # leaf node
else:
current_count += counts[child_idx - n_samples]
counts[i] = current_count
linkage_matrix = np.column_stack(
[model.children_, model.distances_, counts]
).astype(float)
# Plot the corresponding dendrogram
dendrogram(linkage_matrix, **kwargs)
iris = load_iris()
X = iris.data
# setting distance_threshold=0 ensures we compute the full tree.
model = AgglomerativeClustering(distance_threshold=0, n_clusters=None)
model = model.fit(X)
plt.title("Hierarchical Clustering Dendrogram")
# plot the top three levels of the dendrogram
plot_dendrogram(model, truncate_mode="level", p=3)
plt.xlabel("Number of points in node (or index of point if no parenthesis).")
plt.show()
Solution
If you read the documentation here, the number of colors is determined by color_threshold
, which is defaulted to 0.7*max(Z[:,2])
. So you only have to find the number of merges higher than that:
First modify your code to get the linkage matrix:
def get_linkage(model):
# Create linkage matrix
# create the counts of samples under each node
counts = np.zeros(model.children_.shape[0])
n_samples = len(model.labels_)
for i, merge in enumerate(model.children_):
current_count = 0
for child_idx in merge:
if child_idx < n_samples:
current_count += 1 # leaf node
else:
current_count += counts[child_idx - n_samples]
counts[i] = current_count
linkage_matrix = np.column_stack(
[model.children_, model.distances_, counts]
).astype(float)
return linkage_matrix
iris = load_iris()
X = iris.data
# setting distance_threshold=0 ensures we compute the full tree.
model = AgglomerativeClustering(distance_threshold=0, n_clusters=None)
model = model.fit(X)
linkage_matrix = get_linkage(model)
Then calculate the number of colors from it:
from scipy.cluster.hierarchy import cut_tree
color_threshold = 0.7 * max(linkage_matrix[:, 2])
n_color = 1 + len(np.unique(cut_tree(linkage_matrix, height = color_threshold)))
color_codes = ['C' + str(i) for i in range(n_color)] # this is simply the matplotlib default color code
Answered By - Z Li
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.