How to print result of clustering in sklearn

前端 未结 2 1690
盖世英雄少女心
盖世英雄少女心 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 07:05

    Update: You can do it the following way

    """data= data clustered retrieved by function as you want"""
    """model = result from the data with got by KMeans"""
    """cluster = clusters formed by the model"""
    from sklearn.cluster import KMeans
    
    data = clusteredData()
    model = KMeans(n_clusters=5, init='random', max_iter=100, n_init=1, verbose=1)
    cluster = model.fit_predict(scale(data))
    
    dictionary = {}
    for index in range(len(data)): 
        if cluster[index] in dictionary:
            value = []
            value = dictionary[cluster[index]]
            value.append(data[index])
            dictionary[cluster[index]] = value
        else:
            dictionary[cluster[index]]=data[index]
    

    This will create you a dictionary with the NUMBER_OF_THE_CLUSTER as a key and the data within that cluster as a VALUE

提交回复
热议问题