[scikit-learn] hierarchical clustering

Juan Nunez-Iglesias jni.soma at gmail.com
Thu Nov 3 18:00:27 EDT 2016


Hi Jaime,

>From *Elegant SciPy*:

"""
The *fcluster* function takes a linkage matrix, as returned by linkage, and
a threshold, and returns cluster identities. It's difficult to know
a-priori what the threshold should be, but we can obtain the appropriate
threshold for a fixed number of clusters by checking the distances in the
linkage matrix.

from scipy.cluster.hierarchy import fcluster
n_clusters = 3
threshold_distance = (Z[-n_clusters, 2] + Z[-n_clusters+1, 2]) / 2
clusters = fcluster(Z, threshold_distance, 'distance')

"""

As an aside, I imagine this question is better placed in the SciPy mailing
list than scikit-learn (which has its own hierarchical clustering API).

Juan.

On Fri, Nov 4, 2016 at 2:16 AM, Jaime Lopez Carvajal <jalopcar at gmail.com>
wrote:

> Hi there,
>
> I am trying to do image classification using hierarchical clustering.
> So, I have my data, and apply this steps:
>
> from scipy.cluster.hierarchy import dendrogram, linkage
>
> data1 = np.array(data)
> Z = linkage(data, 'ward')
> dendrogram(Z, truncate_mode='lastp',  p=12, show_leaf_counts=False,
> leaf_rotation=90., leaf_font_size=12.,show_contracted=True)
> plt.show()
>
> So, I can see the dendrogram with 12 clusters as I want, but I dont know
> how to use this to classify the image.
> Also, I understand that funtion cluster.hierarchy.cut_tree(Z, n_clusters),
> that cut the tree at that number of clusters, but again I dont know how to
> procedd from there. I would like to have something like: cluster =
> predict(Z, instance)
>
> Any advice or direction would be really appreciate,
>
> Thanks in advance, Jaime
>
>
> --
>
> *Jaime Lopez Carvajal*
>
> _______________________________________________
> scikit-learn mailing list
> scikit-learn at python.org
> https://mail.python.org/mailman/listinfo/scikit-learn
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20161104/3256588f/attachment.html>


More information about the scikit-learn mailing list