O.r*_*rka 15 python machine-learning hierarchical-clustering dendrogram scipy
我想用我用字典(即{leaf: color}
)形式制作的彩色地图为我的星团着色.
我试过跟随https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/,但由于某种原因颜色搞砸了.默认情节看起来不错,我只是想以不同方式分配这些颜色.我看到有一个link_color_func
,但当我尝试使用我的色彩图(D_leaf_color
字典)时,我得到一个错误b/c它不是一个功能.我创建D_leaf_color
了自定义与特定群集关联的叶子的颜色.在我的实际数据集中,颜色意味着什么,所以我正在转向任意颜色分配.
我不想color_threshold
在我的实际数据中使用b/c,我有更多的簇并SciPy
重复颜色,因此这个问题...
如何使用我的叶色字典来自定义树形图簇的颜色?
我做了一个GitHub问题https://github.com/scipy/scipy/issues/6346,在那里我进一步详细阐述了解释SciPy层次聚类树形图输出的叶子着色方法 ?(也许发现了一个错误...)但我仍然无法弄清楚如何实际:(i)使用树状图输出用我指定的颜色字典重建我的树形图或(ii)重新格式化我的D_leaf_color
字典link_color_func
参数.
# Init
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
# Load data
from sklearn.datasets import load_diabetes
# Clustering
from scipy.cluster.hierarchy import dendrogram, fcluster, leaves_list
from scipy.spatial import distance
from fastcluster import linkage # You can use SciPy one too
%matplotlib inline
# Dataset
A_data = load_diabetes().data
DF_diabetes = pd.DataFrame(A_data, columns = ["attr_%d" % j for j in range(A_data.shape[1])])
# Absolute value of correlation matrix, then subtract from 1 for disimilarity
DF_dism = 1 - np.abs(DF_diabetes.corr())
# Compute average linkage
A_dist = distance.squareform(DF_dism.as_matrix())
Z = linkage(A_dist,method="average")
# Color mapping
D_leaf_colors = {"attr_1": "#808080", # Unclustered gray
"attr_4": "#B061FF", # Cluster 1 indigo
"attr_5": "#B061FF",
"attr_2": "#B061FF",
"attr_8": "#B061FF",
"attr_6": "#B061FF",
"attr_7": "#B061FF",
"attr_0": "#61ffff", # Cluster 2 cyan
"attr_3": "#61ffff",
"attr_9": "#61ffff",
}
# Dendrogram
# To get this dendrogram coloring below `color_threshold=0.7`
D = dendrogram(Z=Z, labels=DF_dism.index, color_threshold=None, leaf_font_size=12, leaf_rotation=45, link_color_func=D_leaf_colors)
# TypeError: 'dict' object is not callable
Run Code Online (Sandbox Code Playgroud)
这里有一个使用返回矩阵Z
的解决方案linkage()
(早期描述但在文档中有点隐藏)和link_color_func
:
# see question for code prior to "color mapping"
# Color mapping
dflt_col = "#808080" # Unclustered gray
D_leaf_colors = {"attr_1": dflt_col,
"attr_4": "#B061FF", # Cluster 1 indigo
"attr_5": "#B061FF",
"attr_2": "#B061FF",
"attr_8": "#B061FF",
"attr_6": "#B061FF",
"attr_7": "#B061FF",
"attr_0": "#61ffff", # Cluster 2 cyan
"attr_3": "#61ffff",
"attr_9": "#61ffff",
}
# notes:
# * rows in Z correspond to "inverted U" links that connect clusters
# * rows are ordered by increasing distance
# * if the colors of the connected clusters match, use that color for link
link_cols = {}
for i, i12 in enumerate(Z[:,:2].astype(int)):
c1, c2 = (link_cols[x] if x > len(Z) else D_leaf_colors["attr_%d"%x]
for x in i12)
link_cols[i+1+len(Z)] = c1 if c1 == c2 else dflt_col
# Dendrogram
D = dendrogram(Z=Z, labels=DF_dism.index, color_threshold=None,
leaf_font_size=12, leaf_rotation=45, link_color_func=lambda x: link_cols[x])
Run Code Online (Sandbox Code Playgroud)
用于将自定义颜色图应用于集群分支的两行:
import matplotlib as mpl
from matplotlib.pyplot import cm
from scipy.cluster import hierarchy
cmap = cm.rainbow(np.linspace(0, 1, 10))
hierarchy.set_link_color_palette([mpl.colors.rgb2hex(rgb[:3]) for rgb in cmap])
Run Code Online (Sandbox Code Playgroud)
然后,您可以用任何 cmap替换Rainbow,并将 10 更改为您想要的集群数量。
归档时间: |
|
查看次数: |
5600 次 |
最近记录: |