.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/neighbors/plot_nca_classification.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_neighbors_plot_nca_classification.py: ============================================================================= مقارنة أقرب الجيران مع وبدون تحليل مكونات الحي ============================================================================= مثال يقارن تصنيف أقرب الجيران مع وبدون تحليل مكونات الحي. سيقوم برسم حدود قرارات الفئة التي يحددها أقرب جيران المصنف عند استخدام المسافة الإقليدية على الميزات الأصلية، مقابل استخدام المسافة الإقليدية بعد التحول الذي تعلمه تحليل مكونات الحي. تهدف الأخيرة إلى إيجاد تحويل خطي يُضاعف دقة تصنيف أقرب الجيران (الاحتمالي) على مجموعة التدريب. .. GENERATED FROM PYTHON SOURCE LINES 16-96 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/neighbors/images/sphx_glr_plot_nca_classification_001.png :alt: KNN (k = 1) :srcset: /auto_examples/neighbors/images/sphx_glr_plot_nca_classification_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/neighbors/images/sphx_glr_plot_nca_classification_002.png :alt: NCA, KNN (k = 1) :srcset: /auto_examples/neighbors/images/sphx_glr_plot_nca_classification_002.png :class: sphx-glr-multi-img .. code-block:: Python # المؤلفون: مطوري سكايلرن # معرف الترخيص: BSD-3-Clause import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from sklearn import datasets from sklearn.inspection import DecisionBoundaryDisplay from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier, NeighborhoodComponentsAnalysis from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler n_neighbors = 1 dataset = datasets.load_iris() X, y = dataset.data, dataset.target # نأخذ فقط ميزتين. يمكننا تجنب هذا القطع القبيح # عن طريق استخدام مجموعة بيانات ثنائية الأبعاد X = X[:, [0, 2]] X_train, X_test, y_train, y_test = train_test_split( X, y, stratify=y, test_size=0.7, random_state=42 ) h = 0.05 # حجم الخطوة في الشبكة # إنشاء خرائط الألوان cmap_light = ListedColormap(["#FFAAAA", "#AAFFAA", "#AAAAFF"]) cmap_bold = ListedColormap(["#FF0000", "#00FF00", "#0000FF"]) names = ["KNN", "NCA, KNN"] classifiers = [ Pipeline( [ ("scaler", StandardScaler()), ("knn", KNeighborsClassifier(n_neighbors=n_neighbors)), ] ), Pipeline( [ ("scaler", StandardScaler()), ("nca", NeighborhoodComponentsAnalysis()), ("knn", KNeighborsClassifier(n_neighbors=n_neighbors)), ] ), ] for name, clf in zip(names, classifiers): clf.fit(X_train, y_train) score = clf.score(X_test, y_test) _, ax = plt.subplots() DecisionBoundaryDisplay.from_estimator( clf, X, cmap=cmap_light, alpha=0.8, ax=ax, response_method="predict", plot_method="pcolormesh", shading="auto", ) # قم برسم نقاط التدريب والاختبار أيضًا plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold, edgecolor="k", s=20) plt.title("{} (k = {})".format(name, n_neighbors)) plt.text( 0.9, 0.1, "{:.2f}".format(score), size=15, ha="center", va="center", transform=plt.gca().transAxes, ) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.221 seconds) .. _sphx_glr_download_auto_examples_neighbors_plot_nca_classification.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/neighbors/plot_nca_classification.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/neighbors/plot_nca_classification.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_nca_classification.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_nca_classification.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_nca_classification.zip ` .. include:: plot_nca_classification.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_