.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cluster/plot_agglomerative_dendrogram.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_agglomerative_dendrogram.py: ========================================= رسم مخطط شجرة التجميع الهرمي ========================================= هذا المثال يرسم مخطط شجرة التجميع المقابل لتجميع هرمي باستخدام AgglomerativeClustering وطريقة dendrogram المتوفرة في scipy. .. GENERATED FROM PYTHON SOURCE LINES 12-55 .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_agglomerative_dendrogram_001.png :alt: مخطط شجرة التجميع الهرمي :srcset: /auto_examples/cluster/images/sphx_glr_plot_agglomerative_dendrogram_001.png :class: sphx-glr-single-img .. code-block:: Python import numpy as np from matplotlib import pyplot as plt from scipy.cluster.hierarchy import dendrogram from sklearn.cluster import AgglomerativeClustering from sklearn.datasets import load_iris def plot_dendrogram(model, **kwargs): # إنشاء مصفوفة الربط ثم رسم مخطط شجرة التجميع # إنشاء تعداد العينات تحت كل عقدة counts = np.zeros(model.children_.shape[0]) n_samples = len(model.labels_) for i, merge in enumerate(model.children_): current_count = 0 for child_idx in merge: if child_idx < n_samples: current_count += 1 # leaf node else: current_count += counts[child_idx - n_samples] counts[i] = current_count linkage_matrix = np.column_stack( [model.children_, model.distances_, counts] ).astype(float) # رسم مخطط شجرة التجميع المقابل dendrogram(linkage_matrix, **kwargs) iris = load_iris() X = iris.data # تعيين distance_threshold=0 يضمن حساب الشجرة الكاملة. model = AgglomerativeClustering(distance_threshold=0, n_clusters=None) model = model.fit(X) plt.title("مخطط شجرة التجميع الهرمي") # رسم المستويات الثلاثة العليا من مخطط شجرة التجميع plot_dendrogram(model, truncate_mode="level", p=3) plt.xlabel("عدد النقاط في العقدة (أو مؤشر النقطة إذا لم يكن هناك قوسين).") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.124 seconds) .. _sphx_glr_download_auto_examples_cluster_plot_agglomerative_dendrogram.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_agglomerative_dendrogram.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_agglomerative_dendrogram.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_agglomerative_dendrogram.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_agglomerative_dendrogram.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_agglomerative_dendrogram.zip ` .. include:: plot_agglomerative_dendrogram.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_