فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة 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
اختبار قصير
اختبر فهمك
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)
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
X, y = load_iris(return_X_y=True)
X = X[:, 2:]
for depth in (1, 2, 3):
tree = DecisionTreeClassifier(max_depth=depth, random_state=42).fit(X, y)
print('depth', depth, 'training accuracy', round(tree.score(X, y), 3))
Output
depth 1 training accuracy 0.667 depth 2 training accuracy 0.96 depth 3 training accuracy 0.973
التمرين 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)
def gini(counts):
total = sum(counts)
return 1 - sum((n / total) ** 2 for n in counts)
print(round(gini([30, 20, 10]), 3))
print(gini([60, 0, 0]))
Output
0.611 0.0
مشروع صغير في آخر المحاضرة
اقرأ شجرة قرار على داتا النبيذ
الداتا: 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
الخطوات
- حمّل wine وقسّمها ٧٠/٣٠ بـ stratify.
- درّب DecisionTreeClassifier(max_depth=3, random_state=0).
- اطبع دقة الـ test.
- اطبع الشجرة بـ export_text واقرأ أول سؤال: أنهي feature وأنهي حد؟
- اطبع أهم ٣ 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.