.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/decomposition/plot_faces_decomposition.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_decomposition_plot_faces_decomposition.py: =============================== تحليلات مجموعة بيانات الوجوه =============================== يطبق هذا المثال على :ref:`olivetti_faces_dataset` طرقًا مختلفة لتحليل المصفوفة غير الخاضعة للإشراف (تقليل الأبعاد) من الوحدة :mod:`sklearn.decomposition` (انظر فصل الوثائق :ref:`decompositions`). - المؤلفون: Vlad Niculae, Alexandre Gramfort - الترخيص: BSD 3 clause .. GENERATED FROM PYTHON SOURCE LINES 14-18 .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 19-23 إعداد مجموعة البيانات ------------------- تحميل ومعالجة مجموعة بيانات وجوه Olivetti. .. GENERATED FROM PYTHON SOURCE LINES 23-50 .. code-block:: Python import logging import matplotlib.pyplot as plt from numpy.random import RandomState from sklearn import cluster, decomposition from sklearn.datasets import fetch_olivetti_faces rng = RandomState(0) # عرض سجلات التقدم على stdout logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") faces, _ = fetch_olivetti_faces( return_X_y=True, shuffle=True, random_state=rng) n_samples, n_features = faces.shape # توسيط عام (التركيز على ميزة واحدة، توسيط جميع العينات) faces_centered = faces - faces.mean(axis=0) # توسيط محلي (التركيز على عينة واحدة، توسيط جميع الميزات) faces_centered -= faces_centered.mean(axis=1).reshape(n_samples, -1) print("تتكون مجموعة البيانات من %d وجه" % n_samples) .. rst-class:: sphx-glr-script-out .. code-block:: none downloading Olivetti faces from https://ndownloader.figshare.com/files/5976027 to /root/scikit_learn_data تتكون مجموعة البيانات من 400 وجه .. GENERATED FROM PYTHON SOURCE LINES 51-52 تعريف دالة أساسية لرسم معرض الوجوه. .. GENERATED FROM PYTHON SOURCE LINES 52-85 .. code-block:: Python n_row, n_col = 2, 3 n_components = n_row * n_col image_shape = (64, 64) def plot_gallery(title, images, n_col=n_col, n_row=n_row, cmap=plt.cm.gray): fig, axs = plt.subplots( nrows=n_row, ncols=n_col, figsize=(2.0 * n_col, 2.3 * n_row), facecolor="white", constrained_layout=True, ) fig.set_constrained_layout_pads(w_pad=0.01, h_pad=0.02, hspace=0, wspace=0) fig.set_edgecolor("black") fig.suptitle(title, size=16) for ax, vec in zip(axs.flat, images): vmax = max(vec.max(), -vec.min()) im = ax.imshow( vec.reshape(image_shape), cmap=cmap, interpolation="nearest", vmin=-vmax, vmax=vmax, ) ax.axis("off") fig.colorbar(im, ax=axs, orientation="horizontal", shrink=0.99, aspect=40, pad=0.01) plt.show() .. GENERATED FROM PYTHON SOURCE LINES 86-88 لنلقِ نظرة على بياناتنا. يشير اللون الرمادي إلى القيم السالبة، ويشير اللون الأبيض إلى القيم الموجبة. .. GENERATED FROM PYTHON SOURCE LINES 88-91 .. code-block:: Python plot_gallery("وجوه من مجموعة البيانات", faces_centered[:n_components]) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_001.png :alt: وجوه من مجموعة البيانات :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 92-101 التحليل ------------- تهيئة مقدرات مختلفة للتحليل وملاءمة كل منها على جميع الصور ورسم بعض النتائج. يستخرج كل مقدر 6 مكونات كمتجهات :math:`h \in \mathbb{R}^{4096}`. لقد عرضنا هذه المتجهات فقط في تصور سهل الاستخدام كصور 64 × 64 بكسل. اقرأ المزيد في :ref:`دليل المستخدم `. .. GENERATED FROM PYTHON SOURCE LINES 103-114 الوجوه الذاتية - PCA باستخدام SVD العشوائي ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ تقليل الأبعاد الخطي باستخدام تحليل القيمة المفردة (SVD) للبيانات لإسقاطها إلى مساحة ذات أبعاد أقل. .. note:: يوفر مقدر الوجوه الذاتية، عبر :py:mod:`sklearn.decomposition.PCA`، أيضًا `noise_variance_` عددي (متوسط التباين لكل بكسل) الذي لا يمكن عرضه كصورة. .. GENERATED FROM PYTHON SOURCE LINES 116-125 .. code-block:: Python pca_estimator = decomposition.PCA( n_components=n_components, svd_solver="randomized", whiten=True ) pca_estimator.fit(faces_centered) plot_gallery( "الوجوه الذاتية - PCA باستخدام SVD العشوائي", pca_estimator.components_[ :n_components] ) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_002.png :alt: الوجوه الذاتية - PCA باستخدام SVD العشوائي :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 126-130 المكونات غير السالبة - NMF ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ تقدير البيانات الأصلية غير السالبة كنتاج لمصفوفتين غير سالبتين. .. GENERATED FROM PYTHON SOURCE LINES 132-137 .. code-block:: Python nmf_estimator = decomposition.NMF(n_components=n_components, tol=5e-3) nmf_estimator.fit(faces) # مجموعة البيانات الأصلية غير السالبة plot_gallery("المكونات غير السالبة - NMF", nmf_estimator.components_[:n_components]) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_003.png :alt: المكونات غير السالبة - NMF :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 138-141 المكونات المستقلة - FastICA ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ يفصل تحليل المكونات المستقلة متجهات متعددة المتغيرات إلى مكونات فرعية مضافة مستقلة إلى أقصى حد. .. GENERATED FROM PYTHON SOURCE LINES 143-151 .. code-block:: Python ica_estimator = decomposition.FastICA( n_components=n_components, max_iter=400, whiten="arbitrary-variance", tol=15e-5 ) ica_estimator.fit(faces_centered) plot_gallery( "المكونات المستقلة - FastICA", ica_estimator.components_[:n_components] ) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_004.png :alt: المكونات المستقلة - FastICA :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 152-158 المكونات المتناثرة - MiniBatchSparsePCA ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ يستخرج Mini-batch sparse PCA (:class:`~sklearn.decomposition.MiniBatchSparsePCA`) مجموعة المكونات المتناثرة التي تعيد بناء البيانات بشكل أفضل. هذا المتغير أسرع ولكنه أقل دقة من :class:`~sklearn.decomposition.SparsePCA` المماثل. .. GENERATED FROM PYTHON SOURCE LINES 160-169 .. code-block:: Python batch_pca_estimator = decomposition.MiniBatchSparsePCA( n_components=n_components, alpha=0.1, max_iter=100, batch_size=3, random_state=rng ) batch_pca_estimator.fit(faces_centered) plot_gallery( "المكونات المتناثرة - MiniBatchSparsePCA", batch_pca_estimator.components_[:n_components], ) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_005.png :alt: المكونات المتناثرة - MiniBatchSparsePCA :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 170-176 تعلم القاموس ^^^^^^^^^^^^^^^^^^^ افتراضيًا، يقوم :class:`~sklearn.decomposition.MiniBatchDictionaryLearning` بتقسيم البيانات إلى مجموعات صغيرة ويحسنها بطريقة متصلة بالإنترنت عن طريق التدوير على المجموعات الصغيرة لعدد التكرارات المحدد. .. GENERATED FROM PYTHON SOURCE LINES 178-184 .. code-block:: Python batch_dict_estimator = decomposition.MiniBatchDictionaryLearning( n_components=n_components, alpha=0.1, max_iter=50, batch_size=3, random_state=rng ) batch_dict_estimator.fit(faces_centered) plot_gallery("تعلم القاموس", batch_dict_estimator.components_[:n_components]) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_006.png :alt: تعلم القاموس :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 185-193 مراكز التجميع - MiniBatchKMeans ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :class:`sklearn.cluster.MiniBatchKMeans` فعال من الناحية الحسابية وينفذ التعلم عبر الإنترنت باستخدام طريقة :meth:`~sklearn.cluster.MiniBatchKMeans.partial_fit`. لهذا السبب قد يكون من المفيد تحسين بعض الخوارزميات التي تستغرق وقتًا طويلاً باستخدام :class:`~sklearn.cluster.MiniBatchKMeans`. .. GENERATED FROM PYTHON SOURCE LINES 195-209 .. code-block:: Python kmeans_estimator = cluster.MiniBatchKMeans( n_clusters=n_components, tol=1e-3, batch_size=20, max_iter=50, random_state=rng, ) kmeans_estimator.fit(faces_centered) plot_gallery( "مراكز التجميع - MiniBatchKMeans", kmeans_estimator.cluster_centers_[:n_components], ) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_007.png :alt: مراكز التجميع - MiniBatchKMeans :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 210-216 مكونات تحليل العوامل - FA ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ يشبه :class:`~sklearn.decomposition.FactorAnalysis` :class:`~sklearn.decomposition.PCA` ولكنه يتميز بنمذجة التباين في كل اتجاه لمساحة الإدخال بشكل مستقل (ضوضاء غير متجانسة). اقرأ المزيد في :ref:`دليل المستخدم `. .. GENERATED FROM PYTHON SOURCE LINES 218-239 .. code-block:: Python fa_estimator = decomposition.FactorAnalysis( n_components=n_components, max_iter=20) fa_estimator.fit(faces_centered) plot_gallery("تحليل العوامل (FA)", fa_estimator.components_[:n_components]) # --- Pixelwise variance plt.figure(figsize=(3.2, 3.6), facecolor="white", tight_layout=True) vec = fa_estimator.noise_variance_ vmax = max(vec.max(), -vec.min()) plt.imshow( vec.reshape(image_shape), cmap=plt.cm.gray, interpolation="nearest", vmin=-vmax, vmax=vmax, ) plt.axis("off") plt.title("التباين لكل بكسل من \n تحليل العوامل (FA)", size=16, wrap=True) plt.colorbar(orientation="horizontal", shrink=0.8, pad=0.03) plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_008.png :alt: تحليل العوامل (FA) :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_008.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_009.png :alt: التباين لكل بكسل من تحليل العوامل (FA) :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_009.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 240-253 التحليل: تعلم القاموس ---------------------------------- في القسم التالي، دعونا نفكر في :ref:`DictionaryLearning` بمزيد من الدقة. تعلم القاموس هو مشكلة ترقى إلى إيجاد تمثيل متناثر لبيانات الإدخال كمزيج من العناصر البسيطة. تشكل هذه العناصر البسيطة قاموسًا. من الممكن تقييد القاموس و/أو معاملات الترميز لتكون موجبة لتتناسب مع القيود التي قد تكون موجودة في البيانات. ينفذ :class:`~sklearn.decomposition.MiniBatchDictionaryLearning` نسخة أسرع، ولكن أقل دقة من خوارزمية تعلم القاموس وهي أكثر ملاءمة لمجموعات البيانات الكبيرة. اقرأ المزيد في :ref:`دليل المستخدم `. .. GENERATED FROM PYTHON SOURCE LINES 255-258 ارسم نفس العينات من مجموعة البيانات الخاصة بنا ولكن باستخدام خريطة ألوان أخرى. يشير اللون الأحمر إلى القيم السالبة، ويشير اللون الأزرق إلى القيم الموجبة، ويمثل اللون الأبيض الأصفار. .. GENERATED FROM PYTHON SOURCE LINES 258-263 .. code-block:: Python plot_gallery("وجوه من مجموعة البيانات", faces_centered[:n_components], cmap=plt.cm.RdBu) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_010.png :alt: وجوه من مجموعة البيانات :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_010.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 264-274 على غرار الأمثلة السابقة، نقوم بتغيير المعلمات وتدريب مقدر :class:`~sklearn.decomposition.MiniBatchDictionaryLearning` على جميع الصور. بشكل عام، يقوم تعلم القاموس والترميز المتناثر بتحليل بيانات الإدخال إلى مصفوفات القاموس ومعاملات الترميز. :math:`X \approx UV`، حيث :math:`X = [x_1, . . . , x_n]`، :math:`X \in \mathbb{R}^{m×n}`، قاموس :math:`U \in \mathbb{R}^{m×k}`، معاملات الترميز :math:`V \in \mathbb{R}^{k×n}`. تظهر أدناه أيضًا النتائج عندما يكون القاموس ومعاملات الترميز مقيدة بشكل إيجابي. .. GENERATED FROM PYTHON SOURCE LINES 276-280 تعلم القاموس - قاموس إيجابي ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ في القسم التالي، نفرض الإيجابية عند إيجاد القاموس. .. GENERATED FROM PYTHON SOURCE LINES 282-297 .. code-block:: Python dict_pos_dict_estimator = decomposition.MiniBatchDictionaryLearning( n_components=n_components, alpha=0.1, max_iter=50, batch_size=3, random_state=rng, positive_dict=True, ) dict_pos_dict_estimator.fit(faces_centered) plot_gallery( "تعلم القاموس - قاموس إيجابي", dict_pos_dict_estimator.components_[:n_components], cmap=plt.cm.RdBu, ) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_011.png :alt: تعلم القاموس - قاموس إيجابي :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_011.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 298-302 تعلم القاموس - رمز إيجابي ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ أدناه نقيد معاملات الترميز كمصفوفة موجبة. .. GENERATED FROM PYTHON SOURCE LINES 304-320 .. code-block:: Python dict_pos_code_estimator = decomposition.MiniBatchDictionaryLearning( n_components=n_components, alpha=0.1, max_iter=50, batch_size=3, fit_algorithm="cd", random_state=rng, positive_code=True, ) dict_pos_code_estimator.fit(faces_centered) plot_gallery( "تعلم القاموس - رمز إيجابي", dict_pos_code_estimator.components_[:n_components], cmap=plt.cm.RdBu, ) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_012.png :alt: تعلم القاموس - رمز إيجابي :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_012.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 321-326 تعلم القاموس - قاموس ورمز إيجابيان ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ تظهر أدناه أيضًا النتائج إذا كانت قيم القاموس ومعاملات الترميز مقيدة بشكل إيجابي. .. GENERATED FROM PYTHON SOURCE LINES 328-344 .. code-block:: Python dict_pos_estimator = decomposition.MiniBatchDictionaryLearning( n_components=n_components, alpha=0.1, max_iter=50, batch_size=3, fit_algorithm="cd", random_state=rng, positive_dict=True, positive_code=True, ) dict_pos_estimator.fit(faces_centered) plot_gallery( "تعلم القاموس - قاموس ورمز إيجابيان", dict_pos_estimator.components_[:n_components], cmap=plt.cm.RdBu, ) .. image-sg:: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_013.png :alt: تعلم القاموس - قاموس ورمز إيجابيان :srcset: /auto_examples/decomposition/images/sphx_glr_plot_faces_decomposition_013.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.434 seconds) .. _sphx_glr_download_auto_examples_decomposition_plot_faces_decomposition.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/decomposition/plot_faces_decomposition.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/decomposition/plot_faces_decomposition.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_faces_decomposition.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_faces_decomposition.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_faces_decomposition.zip ` .. include:: plot_faces_decomposition.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_