Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何知道scipy中树状图分类后的具体结果_Python_Python 3.x_Scipy - Fatal编程技术网

Python 如何知道scipy中树状图分类后的具体结果

Python 如何知道scipy中树状图分类后的具体结果,python,python-3.x,scipy,Python,Python 3.x,Scipy,我想用scipy软件包做一个层次聚类,但是用树状图后才得到可视化的结果, 图中显示: 问题是,如果我首先假设聚类数,我想知道每个样本的预测标签。例如,如果我在这张图片上画一条红线(这意味着我假设一开始有5个簇),那么我会 import matplotlib.pyplot as plt from scipy.cluster.hierarchy import dendrogram, linkage ytdist = np.array([662., 877., 255., 412., 996.,

我想用scipy软件包做一个层次聚类,但是用树状图后才得到可视化的结果, 图中显示:

问题是,如果我首先假设聚类数,我想知道每个样本的预测标签。例如,如果我在这张图片上画一条红线(这意味着我假设一开始有5个簇),那么我会

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

ytdist = np.array([662., 877., 255., 412., 996., 295., 468., 268., 400., 754., 564., 138., 219., 869., 669.])

#'single','complete','average'
linked = linkage(ytdist, 'single')
plt.figure()
dendrogram(linked, orientation='top', distance_sort='descending',show_leaf_counts=True)

plt.show()
生成样本数据

np.random.seed(1)
x = np.concatenate((
    np.random.uniform(30, 35, 5),
    np.random.uniform(40, 45, 5),
    np.random.uniform(50, 55, 5),
))
np.random.seed(42)
y = np.concatenate((
    np.random.uniform(40, 55, 5),
    np.random.uniform(30, 35, 5),
    np.random.uniform(35, 40, 5),
))
df = pd.DataFrame({'x': x,'y': y})
plt.scatter(x, y);

连锁和树状图

Z = linkage(df)
dn = dendrogram(Z);

我们选择3个集群(例如,您可以使用弯头方法),并将集群标签添加到
df

df['cluster_labels'] = fcluster(Z, 3, criterion='maxclust')
sns.scatterplot(x='x', y='y', hue='cluster_labels', data=df)
plt.show()

df['cluster_labels'] = fcluster(Z, 3, criterion='maxclust')
sns.scatterplot(x='x', y='y', hue='cluster_labels', data=df)
plt.show()