Import necessary libraries

import numpy as np import matplotlib.pyplot as plt from scipy.cluster.hierarchy import dendrogram, linkage

Set random seed for reproducibility

np.random.seed(0)

Generate random data

X = np.random.random((10, 2))

Perform hierarchical clustering using Ward's method

Z = linkage(X, 'ward')

Plot dendrogram

fig, ax = plt.subplots(figsize=(10, 5)) dendrogram(Z) plt.show()

import numpy as npimport matplotlibpyplot as pltfrom scipyclusterhierarchy import dendrogram linkage# 生成随机数据nprandomseed0X = nprandomrandom10 2# 层次聚类Z = linkageX ward# 绘制树状图fig ax = pltsubplotsfigsize

原文地址: https://www.cveoy.top/t/topic/bMgx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录