.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cluster/plot_ward_structured_vs_unstructured.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 JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_cluster_plot_ward_structured_vs_unstructured.py: =========================================================== العنقدة الهرمية: العنقدة المنظمة وغير المنظمة =========================================================== مثال يبني مجموعة بيانات Swiss Roll ويقوم بتشغيل العنقدة الهرمية على موقعها. لمزيد من المعلومات، راجع :ref:`hierarchical_clustering`. في الخطوة الأولى، يتم تنفيذ العنقدة الهرمية بدون قيود الاتصال على البنية وتستند فقط على المسافة، في حين أن في الخطوة الثانية، يتم تقييد العنقدة إلى رسم k-Nearest Neighbors البياني: إنها عنقدة هرمية ذات بنية مسبقة. بعض المجموعات التي تم تعلمها بدون قيود الاتصال لا تحترم بنية Swiss Roll وتمتد عبر طيات مختلفة من الدوال. على العكس، عند معارضة قيود الاتصال، تشكل المجموعات تقسيمًا جيدًا لسويس رول. .. GENERATED FROM PYTHON SOURCE LINES 21-34 .. code-block:: Python # المؤلفون: مطوري scikit-learn # معرف الترخيص: BSD-3-Clause from sklearn.neighbors import kneighbors_graph import matplotlib.pyplot as plt from sklearn.cluster import AgglomerativeClustering import time as time # الاستيراد التالي مطلوب # لعمل الإسقاط ثلاثي الأبعاد مع matplotlib < 3.2 import mpl_toolkits.mplot3d # noqa: F401 import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 35-39 توليد البيانات ------------- نبدأ بتوليد مجموعة بيانات Swiss Roll. .. GENERATED FROM PYTHON SOURCE LINES 39-47 .. code-block:: Python from sklearn.datasets import make_swiss_roll n_samples = 1500 noise = 0.05 X, _ = make_swiss_roll(n_samples, noise=noise) # Make it thinner X[:, 1] *= 0.5 .. GENERATED FROM PYTHON SOURCE LINES 48-53 حساب العنقدة ------------------ نحن نؤدي AgglomerativeClustering الذي يأتي تحت العنقدة الهرمية بدون أي قيود اتصال. .. GENERATED FROM PYTHON SOURCE LINES 53-63 .. code-block:: Python print("Compute unstructured hierarchical clustering...") st = time.time() ward = AgglomerativeClustering(n_clusters=6, linkage="ward").fit(X) elapsed_time = time.time() - st label = ward.labels_ print(f"Elapsed time: {elapsed_time:.2f}s") print(f"Number of points: {label.size}") .. rst-class:: sphx-glr-script-out .. code-block:: none Compute unstructured hierarchical clustering... Elapsed time: 0.03s Number of points: 1500 .. GENERATED FROM PYTHON SOURCE LINES 64-67 رسم النتيجة ----------- رسم العنقدة الهرمية غير المنظمة. .. GENERATED FROM PYTHON SOURCE LINES 67-83 .. code-block:: Python fig1 = plt.figure() ax1 = fig1.add_subplot(111, projection="3d", elev=7, azim=-80) ax1.set_position([0, 0, 0.95, 1]) for l in np.unique(label): ax1.scatter( X[label == l, 0], X[label == l, 1], X[label == l, 2], color=plt.cm.jet(float(l) / np.max(label + 1)), s=20, edgecolor="k", ) _ = fig1.suptitle(f"بدون قيود الاتصال (الوقت {elapsed_time:.2f}s)") .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_ward_structured_vs_unstructured_001.png :alt: بدون قيود الاتصال (الوقت 0.03s) :srcset: /auto_examples/cluster/images/sphx_glr_plot_ward_structured_vs_unstructured_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 84-86 نحن نحدد k-Nearest Neighbors مع 10 جيران ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 86-91 .. code-block:: Python connectivity = kneighbors_graph(X, n_neighbors=10, include_self=False) .. GENERATED FROM PYTHON SOURCE LINES 92-96 حساب العنقدة ------------------ نحن نؤدي AgglomerativeClustering مرة أخرى مع قيود الاتصال. .. GENERATED FROM PYTHON SOURCE LINES 96-106 .. code-block:: Python print("Compute structured hierarchical clustering...") st = time.time() ward = AgglomerativeClustering( n_clusters=6, connectivity=connectivity, linkage="ward" ).fit(X) elapsed_time = time.time() - st label = ward.labels_ print(f"Elapsed time: {elapsed_time:.2f}s") print(f"Number of points: {label.size}") .. rst-class:: sphx-glr-script-out .. code-block:: none Compute structured hierarchical clustering... Elapsed time: 0.08s Number of points: 1500 .. GENERATED FROM PYTHON SOURCE LINES 107-111 رسم النتيجة ----------- رسم العنقدة الهرمية المنظمة. .. GENERATED FROM PYTHON SOURCE LINES 111-127 .. code-block:: Python fig2 = plt.figure() ax2 = fig2.add_subplot(121, projection="3d", elev=7, azim=-80) ax2.set_position([0, 0, 0.95, 1]) for l in np.unique(label): ax2.scatter( X[label == l, 0], X[label == l, 1], X[label == l, 2], color=plt.cm.jet(float(l) / np.max(label + 1)), s=20, edgecolor="k", ) fig2.suptitle(f"With connectivity constraints (time {elapsed_time:.2f}s)") plt.show() .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_ward_structured_vs_unstructured_002.png :alt: With connectivity constraints (time 0.08s) :srcset: /auto_examples/cluster/images/sphx_glr_plot_ward_structured_vs_unstructured_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.400 seconds) .. _sphx_glr_download_auto_examples_cluster_plot_ward_structured_vs_unstructured.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_ward_structured_vs_unstructured.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/cluster/plot_ward_structured_vs_unstructured.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_ward_structured_vs_unstructured.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_ward_structured_vs_unstructured.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_ward_structured_vs_unstructured.zip ` .. include:: plot_ward_structured_vs_unstructured.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_