.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/classification/plot_digits_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_classification_plot_digits_classification.py: ====================================== التعرف على الأرقام المكتوبة بخط اليد ====================================== هذا المثال يوضح كيفية استخدام scikit-learn للتعرف على صور الأرقام المكتوبة بخط اليد، من 0 إلى 9. .. GENERATED FROM PYTHON SOURCE LINES 10-21 .. code-block:: Python # المؤلفون: مطوري scikit-learn # معرف الترخيص: BSD-3-Clause # استيراد مكتبات بايثون العلمية import matplotlib.pyplot as plt # استيراد مجموعات البيانات، والتصنيفات، ومقاييس الأداء from sklearn import datasets, metrics, svm from sklearn.model_selection import train_test_split .. GENERATED FROM PYTHON SOURCE LINES 22-28 مجموعة بيانات الأرقام -------------- تتكون مجموعة بيانات الأرقام من صور بكسل بحجم 8x8. يحتوي خاصية "images" في مجموعة البيانات على مصفوفات 8x8 من قيم التدرج الرمادي لكل صورة. سنستخدم هذه المصفوفات لعرض أول 4 صور. يحتوي خاصية "target" في مجموعة البيانات على الرقم الذي تمثله كل صورة، ويتم تضمينه في عنوان الرسوم البيانية الأربعة أدناه. ملاحظة: إذا كنا نعمل مع ملفات الصور (مثل ملفات 'png')، فسنقوم بتحميلها باستخدام: func:`matplotlib.pyplot.imread`. .. GENERATED FROM PYTHON SOURCE LINES 28-37 .. code-block:: Python digits = datasets.load_digits() _, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3)) for ax, image, label in zip(axes, digits.images, digits.target): ax.set_axis_off() ax.imshow(image, cmap=plt.cm.gray_r, interpolation="nearest") ax.set_title("Training: %i" % label) .. image-sg:: /auto_examples/classification/images/sphx_glr_plot_digits_classification_001.png :alt: Training: 0, Training: 1, Training: 2, Training: 3 :srcset: /auto_examples/classification/images/sphx_glr_plot_digits_classification_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-44 التصنيف -------------- لتطبيق مصنف على هذه البيانات، نحتاج إلى تسطيح الصور، وتحويل كل مصفوفة 2-D من قيم التدرج الرمادي من الشكل ``(8, 8)`` إلى الشكل ``(64,)``. وبالتالي، ستكون مجموعة البيانات بأكملها على الشكل ``(n_samples, n_features)``، حيث ``n_samples`` هو عدد الصور و ``n_features`` هو العدد الإجمالي للبكسلات في كل صورة. بعد ذلك، يمكننا تقسيم البيانات إلى مجموعتين فرعيتين للتدريب والاختبار وتدريب مصنف ناقل الدعم على عينات التدريب. يمكن استخدام المصنف المدرب بعد ذلك للتنبؤ بقيمة الرقم لعينات مجموعة الاختبار. .. GENERATED FROM PYTHON SOURCE LINES 44-63 .. code-block:: Python # تسطيح الصور n_samples = len(digits.images) data = digits.images.reshape((n_samples, -1)) # إنشاء مصنف: مصنف ناقل الدعم clf = svm.SVC(gamma=0.001) # تقسيم البيانات إلى 50% للتدريب و50% للاختبار X_train, X_test, y_train, y_test = train_test_split( data, digits.target, test_size=0.5, shuffle=False ) # تدريب المصنف على مجموعة التدريب clf.fit(X_train, y_train) # التنبؤ بقيمة الرقم في مجموعة الاختبار predicted = clf.predict(X_test) .. GENERATED FROM PYTHON SOURCE LINES 64-65 نعرض أدناه أول 4 عينات من مجموعة الاختبار ونظهر قيمة الرقم المتوقع في العنوان. .. GENERATED FROM PYTHON SOURCE LINES 65-73 .. code-block:: Python _, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3)) for ax, image, prediction in zip(axes, X_test, predicted): ax.set_axis_off() image = image.reshape(8, 8) ax.imshow(image, cmap=plt.cm.gray_r, interpolation="nearest") ax.set_title(f"Prediction: {prediction}") .. image-sg:: /auto_examples/classification/images/sphx_glr_plot_digits_classification_002.png :alt: Prediction: 8, Prediction: 8, Prediction: 4, Prediction: 9 :srcset: /auto_examples/classification/images/sphx_glr_plot_digits_classification_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-76 :func:`~sklearn.metrics.classification_report` ينشئ تقريراً نصياً يظهر مقاييس التصنيف الرئيسية. .. GENERATED FROM PYTHON SOURCE LINES 76-82 .. code-block:: Python print( f"تقرير التصنيف للمصنف {clf}:\n" f"{metrics.classification_report(y_test, predicted)}\n" ) .. rst-class:: sphx-glr-script-out .. code-block:: none تقرير التصنيف للمصنف SVC(gamma=0.001): precision recall f1-score support 0 1.00 0.99 0.99 88 1 0.99 0.97 0.98 91 2 0.99 0.99 0.99 86 3 0.98 0.87 0.92 91 4 0.99 0.96 0.97 92 5 0.95 0.97 0.96 91 6 0.99 0.99 0.99 91 7 0.96 0.99 0.97 89 8 0.94 1.00 0.97 88 9 0.93 0.98 0.95 92 accuracy 0.97 899 macro avg 0.97 0.97 0.97 899 weighted avg 0.97 0.97 0.97 899 .. GENERATED FROM PYTHON SOURCE LINES 83-85 يمكننا أيضاً رسم مصفوفة الارتباك لل قيم الأرقام الحقيقية والمتنبأ بها. .. GENERATED FROM PYTHON SOURCE LINES 85-97 .. code-block:: Python disp = metrics.ConfusionMatrixDisplay.from_predictions(y_test, predicted) disp.figure_.suptitle("Confusion Matrix") print(f"Confusion matrix:\n{disp.confusion_matrix}") plt.show() disp = metrics.ConfusionMatrixDisplay.from_predictions(y_test, predicted) disp.figure_.suptitle("Confusion Matrix") print(f"Confusion matrix:\n{disp.confusion_matrix}") plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/classification/images/sphx_glr_plot_digits_classification_003.png :alt: Confusion Matrix :srcset: /auto_examples/classification/images/sphx_glr_plot_digits_classification_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/classification/images/sphx_glr_plot_digits_classification_004.png :alt: Confusion Matrix :srcset: /auto_examples/classification/images/sphx_glr_plot_digits_classification_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Confusion matrix: [[87 0 0 0 1 0 0 0 0 0] [ 0 88 1 0 0 0 0 0 1 1] [ 0 0 85 1 0 0 0 0 0 0] [ 0 0 0 79 0 3 0 4 5 0] [ 0 0 0 0 88 0 0 0 0 4] [ 0 0 0 0 0 88 1 0 0 2] [ 0 1 0 0 0 0 90 0 0 0] [ 0 0 0 0 0 1 0 88 0 0] [ 0 0 0 0 0 0 0 0 88 0] [ 0 0 0 1 0 1 0 0 0 90]] Confusion matrix: [[87 0 0 0 1 0 0 0 0 0] [ 0 88 1 0 0 0 0 0 1 1] [ 0 0 85 1 0 0 0 0 0 0] [ 0 0 0 79 0 3 0 4 5 0] [ 0 0 0 0 88 0 0 0 0 4] [ 0 0 0 0 0 88 1 0 0 2] [ 0 1 0 0 0 0 90 0 0 0] [ 0 0 0 0 0 1 0 88 0 0] [ 0 0 0 0 0 0 0 0 88 0] [ 0 0 0 1 0 1 0 0 0 90]] .. GENERATED FROM PYTHON SOURCE LINES 98-102 إذا كانت نتائج تقييم المصنف مخزنة على شكل مصفوفة ارتباك وليس على شكل `y_true` و `y_pred`، يمكننا مع ذلك إنشاء تقرير تصنيف باستخدام: func:`~sklearn.metrics.classification_report` كما يلي: .. GENERATED FROM PYTHON SOURCE LINES 102-119 .. code-block:: Python # قوائم القيم الحقيقية والمتنبأ بها y_true = [] y_pred = [] cm = disp.confusion_matrix # لكل خلية في مصفوفة الارتباك، أضف القيم الحقيقية والمتنبأ بها # إلى القوائم for gt in range(len(cm)): for pred in range(len(cm)): y_true += [gt] * cm[gt][pred] y_pred += [pred] * cm[gt][pred] print( "تقرير التصنيف المعاد بناؤه من مصفوفة الارتباك:\n" f"{metrics.classification_report(y_true, y_pred)}\n" ) .. rst-class:: sphx-glr-script-out .. code-block:: none تقرير التصنيف المعاد بناؤه من مصفوفة الارتباك: precision recall f1-score support 0 1.00 0.99 0.99 88 1 0.99 0.97 0.98 91 2 0.99 0.99 0.99 86 3 0.98 0.87 0.92 91 4 0.99 0.96 0.97 92 5 0.95 0.97 0.96 91 6 0.99 0.99 0.99 91 7 0.96 0.99 0.97 89 8 0.94 1.00 0.97 88 9 0.93 0.98 0.95 92 accuracy 0.97 899 macro avg 0.97 0.97 0.97 899 weighted avg 0.97 0.97 0.97 899 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.770 seconds) .. _sphx_glr_download_auto_examples_classification_plot_digits_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/classification/plot_digits_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/classification/plot_digits_classification.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_digits_classification.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_digits_classification.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_digits_classification.zip ` .. include:: plot_digits_classification.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_