فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
أمثلة 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
اختبار قصير
اختبر فهمك
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)
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
X, y = make_moons(n_samples=500, 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)
from sklearn.ensemble import RandomForestClassifier
for n in (5, 50, 300):
rf = RandomForestClassifier(n_estimators=n, random_state=0).fit(X_tr, y_tr)
print(n, round(rf.score(X_te, y_te), 3))
Output
5 0.86 50 0.873 300 0.873
التمرين 2
جرّب voting='hard' بدل 'soft' في المثال ١ واطبع الدقة.
Try voting='hard' instead of 'soft' in example 1 and print the accuracy.
(output appears here)
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
X, y = make_moons(n_samples=500, 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)
from sklearn.ensemble import VotingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
models = [('lr', LogisticRegression()), ('svc', SVC(random_state=0)), ('tree', DecisionTreeClassifier(max_depth=5, random_state=0))]
vote = VotingClassifier(models, voting='hard').fit(X_tr, y_tr)
print('hard voting', round(vote.score(X_te, y_te), 3))
Output
hard voting 0.873
مشروع صغير في آخر المحاضرة
غابة عشوائية على داتا النبيذ مع 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
الخطوات
- حمّل wine وقسّمها ٧٠/٣٠ بـ stratify.
- درّب شجرة واحدة واطبع دقة الـ test.
- درّب RandomForestClassifier(n_estimators=300, oob_score=True, random_state=0).
- اطبع الـ OOB score ودقة الـ test.
- اطبع أهم ٣ 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.