كل المقررات

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

الفصل 6 (الجزء 2): Decision Trees

Regularizing trees, regression trees and their weak spots

في الجزء ده هنشوف إزاي نمنع الشجرة من الـ overfitting، وإزاي الشجرة بتتوقّع أرقام، وإيه نقط ضعفها.

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

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

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

أمثلة Python

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

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

المثال ١: عمق الشجرة والـ overfitting

Sample output (press Run to produce your own):
max_depth 2    | train 0.92 | test 0.8
max_depth 4    | train 0.93 | test 0.79
max_depth 8    | train 0.99 | test 0.82
max_depth None | train 1.0 | test 0.81

المثال ٢: min_samples_leaf

Sample output (press Run to produce your own):
min_samples_leaf 1   | leaves 35 | test 0.81
min_samples_leaf 5   | leaves 19 | test 0.86
min_samples_leaf 20  | leaves 9 | test 0.78

المثال ٣: Regression tree

Sample output (press Run to produce your own):
max_depth 2    | train R2 0.49 | test R2 0.07
max_depth 4    | train R2 0.64 | test R2 0.05
max_depth None | train R2 1.0 | test R2 -0.34

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

اختبار قصير

اختبر فهمك

1. An unrestricted decision tree usually:

2. Which change makes a tree simpler?

3. What does a regression tree predict in a leaf?

4. Trees are called unstable because:

5. What reduces tree instability?

تمارين برمجة

جرّب بإيدك

التمرين 1

على داتا moons جرّب max_leaf_nodes = 4 و 8 و 30 واطبع دقة الـ test.

On the moons data try max_leaf_nodes = 4, 8 and 30 and print the test accuracy.

(output appears here)

التمرين 2

استخدم cross_val_score لاختيار أحسن max_depth من 1 لـ 6 على داتا wine.

Use cross_val_score to pick the best max_depth from 1 to 6 on the wine data.

(output appears here)

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

اضبط شجرة على داتا السرطان بالـ cross-validation

الداتا: Breast Cancer Wisconsin (Diagnostic): 569 samples, 30 features. Source: UCI.

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

الخطوات

  1. حمّل الداتا وقسّمها ٧٠/٣٠ بـ stratify.
  2. درّب شجرة من غير حدود واطبع دقة الـ training والـ test.
  3. جرّب max_depth من 1 لـ 8 بـ cv=5 على الـ training.
  4. اختار أحسن عمق وقيس الـ test.
  5. اشرح الفرق بين الشجرة الحرة والمضبوطة.
Expected output (press Run to produce your own):
unrestricted: train 1.0 test 0.918
depth 1 cv 0.905
depth 2 cv 0.945
depth 3 cv 0.945
depth 4 cv 0.932
depth 5 cv 0.932
depth 6 cv 0.935
depth 7 cv 0.927
depth 8 cv 0.927
best depth 2 -> test 0.918

مشروع نهاية الفصل

ملف المشروع العملي للفصل 6

ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (PDF)

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.