How to print clustering result in sklearn

I have a sparse matrix

from scipy.sparse import *
M = csr_matrix((data_np, (rows_np, columns_np)));

then I do clustering this way

from sklearn.cluster import KMeans
km = KMeans(n_clusters=n, init='random', max_iter=100, n_init=1, verbose=1)
km.fit(M)

and my question is extremely important: how to print the result of clustering without additional information. I don't care about the plot or the distance. I just need cluster rows looking like this

Cluster 1
row 1
row 2
row 3

Cluster 2
row 4
row 20
row 1000
...

How can i get it? Excuse me for this question.

+4
source share
1 answer

Time to help yourself. After

km.fit(M)

we run

labels = km.predict(M)

, numpy.ndarray. . , . : 5, , 1 5. , {cluster_number: [row1, row2, row3],...}

# in row_dict we store actual meanings of rows, in my case it russian words
clusters = {}
    n = 0
    for item in labels:
        if item in clusters:
            clusters[item].append(row_dict[n])
        else:
            clusters[item] = [row_dict[n]]
        n +=1

for item in clusters:
    print "Cluster ", item
    for i in clusters[item]:
        print i
+10

Source: https://habr.com/ru/post/1584032/


All Articles