Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách đưa ma trận khoảng cách tính sẵn vào sns.clustermap trong Matplotlib

Dùng sns.clustermap() trực tiếp trên DataFrame (tự tính khoảng cách) hoặc truyền ma trận khoảng cách.

  • sns.set_theme(color_codes=True).
  • Load dataset iris, tách cột species bằng pop().
  • g = sns.clustermap(iris) → tự động clustering và heatmap.
  • show().

Ví dụ

from matplotlib import pyplot as plt
import seaborn as sns
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
sns.set_theme(color_codes=True)
iris = sns.load_dataset("iris")
species = iris.pop("species")
g = sns.clustermap(iris)
plt.show()

Kết quả

Clustered heatmap (dendrogram + heatmap) của bộ dữ liệu Iris 4 đặc trưng.