كل المقررات

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

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

How a tree learns: questions, Gini impurity and CART

الفصل ده عن الـ Decision Trees: نموذج بيسأل أسئلة بسيطة ورا بعض. في الجزء ده هنفهم إزاي الشجرة بتتبني، وإيه الـ Gini، وإزاي بتتوقّع.

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

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

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

أمثلة Python

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

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

المثال ١: درّب شجرة على iris

Sample output (press Run to produce your own):
|--- petal length <= 2.45
|   |--- class: 0
|--- petal length >  2.45
|   |--- petal width <= 1.75
|   |   |--- class: 1
|   |--- petal width >  1.75
|   |   |--- class: 2

المثال ٢: احتمالات التوقّع

Sample output (press Run to produce your own):
probabilities: [[0.    0.907 0.093]]
class: versicolor

المثال ٣: Gini بإيدينا

Sample output (press Run to produce your own):
pure node        : 0.0
two classes 50/50: 0.5
three classes    : 0.667
a node 0/49/5    : 0.168

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

اختبار قصير

اختبر فهمك

1. Gini impurity is 0 when:

2. CART is called greedy because it:

3. The probability a tree reports for a class comes from:

4. Do decision trees need feature scaling?

5. Prediction cost mainly depends on:

تمارين برمجة

جرّب بإيدك

التمرين 1

غيّر max_depth في المثال ١ إلى 1 وبعدين 3 واطبع الدقة على الـ training لكل عمق.

Change max_depth in example 1 to 1 and then 3 and print the training accuracy for each depth.

(output appears here)

التمرين 2

اكتب دالة gini تاخد قائمة أعداد وتجرّبها على [30, 20, 10] وعلى [60, 0, 0].

Write a gini function that takes a list of counts and try it on [30, 20, 10] and [60, 0, 0].

(output appears here)

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

اقرأ شجرة قرار على داتا النبيذ

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

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

الخطوات

  1. حمّل wine وقسّمها ٧٠/٣٠ بـ stratify.
  2. درّب DecisionTreeClassifier(max_depth=3, random_state=0).
  3. اطبع دقة الـ test.
  4. اطبع الشجرة بـ export_text واقرأ أول سؤال: أنهي feature وأنهي حد؟
  5. اطبع أهم ٣ features بـ feature_importances_.
Expected output (press Run to produce your own):
test accuracy: 0.963
|--- proline <= 765.00
|   |--- flavanoids <= 1.40
|   |   |--- color_intensity <= 3.72
|   |   |   |--- class: 1
|   |   |--- color_intensity >  3.72
|   |   |   |--- class: 2
|   |--- flavanoids >  1.40
|   |   |--- alcohol <= 13.17
|   |   |   |--- class: 1
|   |   |--- alcohol >  13.17
|   |   |   |--- class: 1
|--- proline >  765.00
|   |--- flavanoids <= 2.17
|   |   |--- malic_acid <= 2.33
|   |   |   |--- class: 1
|   |   |--- malic_acid >  2.33
|   |   |   |--- class: 2
|   |--- flavanoids >  2.17
|   |   |--- magnesium <= 135.50
|   |   |   |--- class: 0
|   |   |--- magnesium >  135.50
|   |   |   |--- class: 1

proline 0.422
flavanoids 0.409
color_intensity 0.109

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.