Some questions on dendrogram - python (Scipy)

孤人 提交于 2020-01-03 16:45:11

问题


I am new to scipy but I managed to get the expected dendrogram. I am some more questions;

  1. In the dendrogram, distance between some points are 0 but its not visible due to image border. How can I remove the border and make the lower limit of y-axis to -1, so that it is clearly visible. e.g. distance between these points are 0 (13,17), (2,10), (4,8,19)
  2. How can I prune/truncate on a particular distance. for e.g. prune at 0.4
  3. How to write these clusters(after pruning) to a file

My python code:

import scipy
import pylab
import scipy.cluster.hierarchy as sch
import numpy as np

D = np.genfromtxt('LtoR.txt', dtype=None)
def llf(id):
    return str(id)
fig = pylab.figure(figsize=(10,10))
Y = sch.linkage(D, method='single')
Z1 = sch.dendrogram(Y,leaf_label_func=llf,leaf_rotation=90)
fig.show()
fig.savefig('dendrogram.png')

Dendrogram:

thank you.


回答1:


1.fig.gca().set_ylim(-0.4,1.2) Here gca() returns the current axes object, so you can give it a name

ax=fig.gca()
ax.set_ylim(-0.4,ax.get_ylim()[1])



回答2:


  1. You can prune the dendrogram and obtain your clusters using fcluster. To prune at a distance of 0.4:

    clusters = sch.fcluster(Y,t = 0.4,criterion = 'distance')

  2. The resulting array (clusters) contains the cluster label for every observation in your data. You can write the array using numpy.savetxt:

    np.savetxt('clusters.txt', clusters, delimiter=',')




回答3:


The border is shown because of the axis. So you can remove the border using the following command:

fig = plt.figure(figsize=(10, 8))
ax2 = fig.add_axes([0.3, 0.71, 0.6, 0.2])
Y = sch.linkage(D, method='ward')
Z2 = sch.dendrogram(Y)
ax2.set_xticks([])
ax2.set_yticks([])
ax2.axis('off')

ax.axis('off') hides the border.



来源:https://stackoverflow.com/questions/9708630/some-questions-on-dendrogram-python-scipy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!