فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة 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
اختبار قصير
اختبر فهمك
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)
from sklearn.datasets import make_moons
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
X, y = make_moons(n_samples=400, noise=0.3, random_state=42)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.3, random_state=0)
for n in (4, 8, 30):
t = DecisionTreeClassifier(max_leaf_nodes=n, random_state=42).fit(X_tr, y_tr)
print('max_leaf_nodes', n, 'test', round(t.score(X_te, y_te), 2))
Output
max_leaf_nodes 4 test 0.8 max_leaf_nodes 8 test 0.83 max_leaf_nodes 30 test 0.84
التمرين 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)
from sklearn.datasets import load_wine
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_val_score
X, y = load_wine(return_X_y=True)
scores = {}
for d in range(1, 7):
scores[d] = cross_val_score(DecisionTreeClassifier(max_depth=d, random_state=0), X, y, cv=5).mean()
print('depth', d, round(scores[d], 3))
print('best depth:', max(scores, key=scores.get))
Output
depth 1 0.646 depth 2 0.821 depth 3 0.893 depth 4 0.916 depth 5 0.888 depth 6 0.888 best depth: 4
مشروع صغير في آخر المحاضرة
اضبط شجرة على داتا السرطان بالـ 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
الخطوات
- حمّل الداتا وقسّمها ٧٠/٣٠ بـ stratify.
- درّب شجرة من غير حدود واطبع دقة الـ training والـ test.
- جرّب max_depth من 1 لـ 8 بـ cv=5 على الـ training.
- اختار أحسن عمق وقيس الـ test.
- اشرح الفرق بين الشجرة الحرة والمضبوطة.
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 فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.
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.