كل المقررات

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

الفصل 7 (الجزء 1): Ensemble Learning and Random Forests

Voting, bagging and Random Forests

الفصل ده عن حكمة الجماعة: بدل نموذج واحد، بنجمع نماذج كتير. في الجزء ده هنشوف الـ Voting والـ Bagging والـ Random Forest.

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

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

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

أمثلة Python

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

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

المثال ١: Voting على moons

Sample output (press Run to produce your own):
lr    0.827
svc   0.88
tree  0.867
soft voting 0.873

المثال ٢: Bagging و OOB

Sample output (press Run to produce your own):
single tree test accuracy: 0.827
bagging OOB score        : 0.917
bagging test accuracy    : 0.873

المثال ٣: Random Forest وأهم الـ features

Sample output (press Run to produce your own):
test accuracy: 0.942
worst perimeter        0.141
worst area             0.132
worst concave points   0.122

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

اختبار قصير

اختبر فهمك

1. Soft voting combines models by:

2. Bagging samples the training set:

3. Out-of-bag examples are:

4. What extra randomness does a Random Forest add?

5. Ensembles work best when the models:

تمارين برمجة

جرّب بإيدك

التمرين 1

على داتا moons قارن RandomForest بـ n_estimators = 5 و 50 و 300 واطبع دقة الـ test.

On the moons data compare a RandomForest with n_estimators = 5, 50 and 300 and print the test accuracy.

(output appears here)

التمرين 2

جرّب voting='hard' بدل 'soft' في المثال ١ واطبع الدقة.

Try voting='hard' instead of 'soft' in example 1 and print the accuracy.

(output appears here)

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

غابة عشوائية على داتا النبيذ مع OOB

الداتا: Wine recognition dataset: 178 samples, 13 features, 3 varieties. Source: UCI.

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

الخطوات

  1. حمّل wine وقسّمها ٧٠/٣٠ بـ stratify.
  2. درّب شجرة واحدة واطبع دقة الـ test.
  3. درّب RandomForestClassifier(n_estimators=300, oob_score=True, random_state=0).
  4. اطبع الـ OOB score ودقة الـ test.
  5. اطبع أهم ٣ features.
Expected output (press Run to produce your own):
single tree: 0.944
forest OOB: 0.96 | test: 1.0
proline 0.185
color_intensity 0.172
flavanoids 0.153

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.