scikit-learn: Finding the features that contribute to each KMeans cluster

后端 未结 5 671
生来不讨喜
生来不讨喜 2021-01-31 20:19

Say you have 10 features you are using to create 3 clusters. Is there a way to see the level of contribution each of the features have for each of the clusters?

What I w

5条回答
  •  我在风中等你
    2021-01-31 20:30

    You can do it this way:

    >>> import numpy as np
    >>> import sklearn.cluster as cl
    >>> data = np.array([99,1,2,103,44,63,56,110,89,7,12,37])
    >>> k_means = cl.KMeans(init='k-means++', n_clusters=3, n_init=10)
    >>> k_means.fit(data[:,np.newaxis]) # [:,np.newaxis] converts data from 1D to 2D
    >>> k_means_labels = k_means.labels_
    >>> k1,k2,k3 = [data[np.where(k_means_labels==i)] for i in range(3)] # range(3) because 3 clusters
    >>> k1
    array([44, 63, 56, 37])
    >>> k2
    array([ 99, 103, 110,  89])
    >>> k3
    array([ 1,  2,  7, 12])
    

提交回复
热议问题