How to print result of clustering in sklearn

前端 未结 2 1669
盖世英雄少女心
盖世英雄少女心 2020-12-31 06:20

I have a sparse matrix

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

then I\'m doing clustering that way

2条回答
  •  傲寒
    傲寒 (楼主)
    2020-12-31 06:47

    Time to help myself. After

    km.fit(M)
    

    we run

    labels = km.predict(M)
    

    which returns labels, numpy.ndarray. Number of elements in this array equals number of rows. And each element means that a row belongs to the cluster. For example: if first element is 5 it means that row 1 belongs to cluster 5. Lets put our rows in a dictionary of lists looking this way {cluster_number:[row1, row2, row3], ...}

    # in row_dict we store actual meanings of rows, in my case it's 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
    

    and print the result

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

提交回复
热议问题