.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cluster/plot_bisect_kmeans.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_cluster_plot_bisect_kmeans.py: ============================================================= 二分 K-Means 和常规 K-Means 性能比较 ============================================================= 此示例展示了常规 K-Means 算法和二分 K-Means 之间的差异。 当增加 n_clusters 时,K-Means 聚类会有所不同,而二分 K-Means 聚类则建立在之前的聚类基础上。因此,它往往会创建具有更规则的大规模结构的聚类。这种差异可以直观地观察到:对于所有数量的聚类,二分 K-Means 都会有一条将整体数据云分成两部分的分割线,而常规 K-Means 则没有。 .. GENERATED FROM PYTHON SOURCE LINES 11-61 .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_bisect_kmeans_001.png :alt: Bisecting K-Means : 4 clusters, Bisecting K-Means : 8 clusters, Bisecting K-Means : 16 clusters, K-Means : 4 clusters, K-Means : 8 clusters, K-Means : 16 clusters :srcset: /auto_examples/cluster/images/sphx_glr_plot_bisect_kmeans_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt from sklearn.cluster import BisectingKMeans, KMeans from sklearn.datasets import make_blobs print(__doc__) # 生成样本数据 n_samples = 10000 random_state = 0 X, _ = make_blobs(n_samples=n_samples, centers=2, random_state=random_state) # KMeans 和 BisectingKMeans 的聚类中心数量 n_clusters_list = [4, 8, 16] # 算法比较 clustering_algorithms = { "Bisecting K-Means": BisectingKMeans, "K-Means": KMeans, } # 为每个变体制作子图 fig, axs = plt.subplots( len(clustering_algorithms), len(n_clusters_list), figsize=(12, 5) ) axs = axs.T for i, (algorithm_name, Algorithm) in enumerate(clustering_algorithms.items()): for j, n_clusters in enumerate(n_clusters_list): algo = Algorithm(n_clusters=n_clusters, random_state=random_state, n_init=3) algo.fit(X) centers = algo.cluster_centers_ axs[j, i].scatter(X[:, 0], X[:, 1], s=10, c=algo.labels_) axs[j, i].scatter(centers[:, 0], centers[:, 1], c="r", s=20) axs[j, i].set_title(f"{algorithm_name} : {n_clusters} clusters") # 隐藏顶部图的 x 标签和刻度标签以及右侧图的 y 刻度。 for ax in axs.flat: ax.label_outer() ax.set_xticks([]) ax.set_yticks([]) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.841 seconds) .. _sphx_glr_download_auto_examples_cluster_plot_bisect_kmeans.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/cluster/plot_bisect_kmeans.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_bisect_kmeans.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_bisect_kmeans.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_bisect_kmeans.zip ` .. include:: plot_bisect_kmeans.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_