Thuta Learning
ရှာဖွေရန်
AdvancedData & Databasesintermediate

Random Forests & Ensemble Methods

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Random Forests & Ensemble Methods ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် scikit-learn code ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Ensemble Method ဆိုတာ model (weak learner) များစွာကို ပေါင်းစပ်ပြီး, individual model တစ်ခုထက် ပိုတည်ငြိမ်/accurate ဖြစ်တဲ့ prediction ရအောင် လုပ်တဲ့ technique ပါ — 'crowd wisdom' concept နဲ့ ဆင်တူပါတယ် (opinion များစွာရဲ့ average က individual opinion တစ်ခုတည်းထက် ပိုတည်ငြိမ်တတ်တာလိုမျိုး)။ Random Forest ကတော့ Decision Tree (Intermediate chapter) များစွာကို bagging technique (data/feature ကို random sample ယူပြီး tree တစ်ခုချင်းစီကို train) ဖြင့် ပေါင်းစပ်ထားတဲ့ ensemble algorithm ပါ — final prediction ကို tree အားလုံးရဲ့ majority vote (classification) ဒါမှမဟုတ် average (regression) ဖြင့် ဆုံးဖြတ်ပါတယ်.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

`from sklearn.ensemble import RandomForestClassifier; model = RandomForestClassifier(n_estimators=100, max_depth=5); model.fit(X_train, y_train)` လို့ ရေးရင် — tree 100 ခုကို train ပြီး (`n_estimators=100`), test data ကို predict ချတဲ့အခါ tree 100 ခုစလုံးရဲ့ vote ကို ပေါင်းစပ်ပါတယ် — single Decision Tree ထက် ပိုတည်ငြိမ်ပြီး, overfitting risk ကလည်း နည်းပါတယ်.

အတူတူ ကြည့်မယ်

python
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier

tree = DecisionTreeClassifier(max_depth=5, random_state=42)
tree.fit(X_train, y_train)

forest = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=42)
forest.fit(X_train, y_train)

print(f"Single tree accuracy: {tree.score(X_test, y_test):.2f}")
print(f"Random Forest accuracy: {forest.score(X_test, y_test):.2f}")
You should see
Single tree accuracy: 0.84
Random Forest accuracy: 0.91

၅ မိနစ် စမ်းကြည့်

Single Decision Tree vs Random Forest ကို dataset တစ်ခုတည်းအပေါ် train/compare ကြည့်ပြီး, accuracy improvement ကို confirm လုပ်ကြည့်ပါ — `model.feature_importances_` ကို ပါ ကြည့်ကြည့်ပါ (feature တစ်ခုချင်းစီရဲ့ importance ranking).

သတိလေးတစ်ချက်

Random Forest ရဲ့ default hyperparameter (`n_estimators`, `max_depth`) ကို dataset အလိုက် tune မလုပ်ဘဲ 'default ကတော့ ကောင်းလောက်ပြီ' လို့ ယူဆခြင်းသည် — Advanced chapter ရဲ့ Hyperparameter Tuning lesson ကို ဆက်လေ့လာသင့်ပါတယ်.

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `n_estimators` ကို too large (1000+) ချထားခြင်း — accuracy improvement diminishing returns ဖြစ်လာပြီး, training/prediction time ကလည်း ကြာသွားနိုင်ပါတယ်
  • Random Forest ရဲ့ interpretability ကို Decision Tree တစ်ခုတည်းလို မျှော်လင့်ခြင်း — tree ၁၀၀ ခုကို ပေါင်းစပ်ထားလို့ single tree လို ရှင်းရှင်းလင်းလင်း visualize/explain မလွယ်ကူတော့ပါ (feature_importances_ ကတော့ ကူညီပေးနိုင်ပါတယ်)

အခု ကိုယ်တိုင် စမ်းကြည့်

Single Decision Tree vs Random Forest ကို dataset တစ်ခုတည်းအပေါ် train/compare ကြည့်ပြီး, accuracy improvement ကို confirm လုပ်ကြည့်ပါ — `model.feature_importances_` ကို ပါ ကြည့်ကြည့်ပါ (feature တစ်ခုချင်းစီရဲ့ importance ranking).

You'll know it worked when: Single tree accuracy: 0.84 Random Forest accuracy: 0.91

Random Forests & Ensemble Methods | Thuta Learning