Python for knowledge sharing - K-means clustering algorithm in sklearn outputs the sample data contained in each cluster

Python for knowledge sharing - K-means clustering algorithm in sklearn outputs the sample data contained in each cluster

background

When we develop daily, we will encounter all kinds of strange problems (step on the pit o(╯□╰)o), this FAQ series is a series of record articles about some of the problems I encounter every day, organized here After summarizing, share it with everyone, so that his friends who are still in the deep pit can climb out with a rope.
At the same time, everyone is welcome to leave a message or private message me with the problems you encounter, and I will see if I can solve them for you.

development environment

  • System: windows10
  • Version: Python3

content

This section shares a commonly used output tool when using the clustering algorithm in sklearn to output the sample data contained in each cluster. The following is the specific implementation method:

kmeans_model = KMeans(init="k-means++",n_clusters=t)
            kmeans_model.fit(tf_matrix)  # 训练是t簇,指定数据源
            # 输出各个簇中包含的样本数据
            labels = kmeans_model.predict(tf_matrix)
            clusters = {}
            n = 0
            for item in labels:
                if item in clusters:
                    clusters[item].append(all_data[n])
                else:
                    clusters[item] = [all_data[n]]
                n +=1
            for item in clusters:
                print("输出簇: ", item)
                for i2 in clusters[item]:
                    print(i2)
This article declares:
88x31.png
Creative Commons License
This work is licensed by CN Hua Shao under the Creative Commons Attribution-Noncommercial 4.0 International License.

Guess you like

Origin blog.csdn.net/csde12/article/details/125690843