كل المقررات

الرئيسية / مقرر Machine Learning / الفصل 3 الجزء 1

الفصل 3 (الجزء 1): Classification

Binary classifiers, accuracy, confusion matrix, precision and recall

في الجزء ده هنبدأ التصنيف: classifier ثنائي، ليه الـ accuracy ممكن تخدعنا، الـ confusion matrix، وPrecision وRecall وF1.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

أمثلة Python

أمثلة صغيرة بالـ scikit-learn

الكود والنتيجة معروضين هنا كنص ثابت (النتائج اتجرّبت فعلاً). لتشغيل الكود بنفسك انسخه في Google Colab المجاني. تقدر كمان تفتح الدفتر الرسمي للكتاب (ترخيص Apache-2.0) من الرابط تحت.

المثال ١: فخ الـ accuracy

Sample output (press Run to produce your own):
share of 5s: 0.101
always-say-'not 5' accuracy: 0.898
SGD accuracy: 0.989

المثال ٢: confusion matrix و precision و recall

Sample output (press Run to produce your own):
[[484   1]
 [  5  50]]
precision: 0.98
recall:    0.909
F1:        0.943

المثال ٣: نفس الأرقام بإيدك

Sample output (press Run to produce your own):
precision: 0.889
recall:    0.8
F1:        0.842

افتح الدفتر الرسمي في Colab

اختبار قصير

اختبر فهمك

1. If 90% of examples are negative, a model that always predicts negative has accuracy about:

2. Precision answers:

3. Recall answers:

4. In a confusion matrix the rows are usually:

5. F1 score is high when:

تمارين برمجة

جرّب بإيدك

التمرين 1

غيّر الهدف لـ «هل الرقم ٣؟» بدل ٥ واطبع precision و recall بـ SGDClassifier.

Change the target to 'is the digit 3?' instead of 5 and print precision and recall with SGDClassifier.

(output appears here)

التمرين 2

اعمل DummyClassifier(strategy='most_frequent') لمهمة «هل الرقم ٣؟» واطبع دقته. ليه الرقم عالي رغم إنه مش بيتعلّم حاجة؟

Fit a DummyClassifier(strategy='most_frequent') for the 'is it 3?' task and print its accuracy. Why is it high even though it learns nothing?

(output appears here)

مشروع صغير في آخر المحاضرة

كاشف رقم ٥: Binary classifier

الداتا: Optical Recognition of Handwritten Digits (1,797 images, 8x8). Source: UCI.

License / source: Ships with scikit-learn; open data from the UCI repository. Dataset page

الخطوات

  1. حمّل digits واعمل target جديد: True لو الرقم ٥.
  2. قسّم الداتا ٨٠/٢٠ بـ stratify.
  3. درّب LogisticRegression (max_iter=2000).
  4. اطبع accuracy و confusion matrix و precision و recall.
  5. اكتب: ليه الـ accuracy لوحدها بتضلّلنا هنا؟ (الفئة ٥ حوالي ١٠٪ بس)
Expected output (press Run to produce your own):
accuracy: 0.992
[[322   2]
 [  1  35]]
precision: 0.946
recall: 0.972

Credits: topic order follows the public chapter list of Aurélien Géron's book "Hands-On Machine Learning with Scikit-Learn, Keras and TensorFlow" (3rd ed.). All explanations and code here are original.