.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/neighbors/plot_caching_nearest_neighbors.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_caching_nearest_neighbors.py: ========================= الاحتفاظ بأقرب الجيران في الذاكرة المؤقتة ========================= هذا المثال يوضح كيفية حساب أقرب الجيران مسبقًا قبل استخدامهم في KNeighborsClassifier. يمكن لـ KNeighborsClassifier حساب أقرب الجيران داخليًا، ولكن حسابهم مسبقًا يمكن أن يوفر عدة فوائد، مثل التحكم الدقيق في المعاملات، والاحتفاظ بالنتائج للاستخدامات المتعددة، أو التطبيقات المخصصة. هنا نستخدم خاصية الاحتفاظ بالنتائج في الأنابيب لحفظ أقرب الجيران بين عمليات التهيئة المتعددة لـ KNeighborsClassifier. تكون المكالمة الأولى بطيئة لأنها تحسب مخطط الجيران، بينما تكون المكالمات اللاحقة أسرع لأنها لا تحتاج إلى إعادة حساب المخطط. هنا تكون المدة قصيرة لأن مجموعة البيانات صغيرة، ولكن يمكن أن يكون التحسن أكثر وضوحًا عندما تنمو مجموعة البيانات بشكل أكبر، أو عندما تكون شبكة المعاملات المراد البحث فيها كبيرة. .. GENERATED FROM PYTHON SOURCE LINES 11-60 .. image-sg:: /auto_examples/neighbors/images/sphx_glr_plot_caching_nearest_neighbors_001.png :alt: دقة التصنيف, وقت التهيئة (مع الاحتفاظ بالنتائج) :srcset: /auto_examples/neighbors/images/sphx_glr_plot_caching_nearest_neighbors_001.png :class: sphx-glr-single-img .. code-block:: Python # المؤلفون: مطوري scikit-learn # معرف الترخيص: BSD-3-Clause from tempfile import TemporaryDirectory import matplotlib.pyplot as plt from sklearn.datasets import load_digits from sklearn.model_selection import GridSearchCV from sklearn.neighbors import KNeighborsClassifier, KNeighborsTransformer from sklearn.pipeline import Pipeline X, y = load_digits(return_X_y=True) n_neighbors_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # يحسب المحول مخطط أقرب الجيران باستخدام أكبر عدد من الجيران # الضروري في البحث الشبكي. يقوم نموذج التصنيف بتصفية # مخطط أقرب الجيران كما يتطلب معامل n_neighbors الخاص به. graph_model = KNeighborsTransformer(n_neighbors=max(n_neighbors_list), mode="distance") classifier_model = KNeighborsClassifier(metric="precomputed") # لاحظ أننا نعطي الذاكرة المؤقتة دليلًا لحفظ حساب المخطط # الذي سيتم استخدامه عدة مرات عند ضبط معاملات نموذج التصنيف. with TemporaryDirectory(prefix="sklearn_graph_cache_") as tmpdir: full_model = Pipeline( steps=[("graph", graph_model), ("classifier", classifier_model)], memory=tmpdir ) param_grid = {"classifier__n_neighbors": n_neighbors_list} grid_model = GridSearchCV(full_model, param_grid) grid_model.fit(X, y) # رسم نتائج البحث الشبكي. fig, axes = plt.subplots(1, 2, figsize=(8, 4)) axes[0].errorbar( x=n_neighbors_list, y=grid_model.cv_results_["mean_test_score"], yerr=grid_model.cv_results_["std_test_score"], ) axes[0].set(xlabel="n_neighbors", title="دقة التصنيف") axes[1].errorbar( x=n_neighbors_list, y=grid_model.cv_results_["mean_fit_time"], yerr=grid_model.cv_results_["std_fit_time"], color="r", ) axes[1].set(xlabel="n_neighbors", title="وقت التهيئة (مع الاحتفاظ بالنتائج)") fig.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.839 seconds) .. _sphx_glr_download_auto_examples_neighbors_plot_caching_nearest_neighbors.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_caching_nearest_neighbors.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_caching_nearest_neighbors.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_caching_nearest_neighbors.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_caching_nearest_neighbors.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_caching_nearest_neighbors.zip ` .. include:: plot_caching_nearest_neighbors.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_