By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Ensemble methods combine the predictions of multiple “weak” learners to produce a single, stronger model. By aggregating diverse hypotheses, they reduce variance (bagging), bias (boosting), or both (stacking), often delivering state‑of‑the‑art accuracy with relatively little feature engineering. In practice, you might use a Gradient‑Boosted Tree ensemble (e.g., XGBoost) to predict customer churn for a telecom provider, where a single decision tree would over‑fit and a linear model would under‑fit.
num_leaves
max_depth
learning_rate
python import pandas as pd df = pd.read_csv('churn.csv') df = df.dropna() X = df.drop('churn', axis=1) y = df['churn']
python from sklearn.compose import ColumnTransformer from sklearn.preprocessing import OneHotEncoder, StandardScaler preproc = ColumnTransformer([ ('cat', OneHotEncoder(handle_unknown='ignore'), cat_cols), ('num', StandardScaler(), num_cols) ]) X_enc = preproc.fit_transform(X)
python from sklearn.model_selection import train_test_split X_tr, X_te, y_tr, y_te = train_test_split( X_enc, y, test_size=0.2, stratify=y, random_state=42)
RandomForestClassifier
BaggingRegressor
AdaBoostClassifier
XGBClassifier
LGBMClassifier
StackingClassifier
GridSearchCV
Optuna
n_estimators
subsample
python from xgboost import XGBClassifier model = XGBClassifier( n_estimators=500, max_depth=6, learning_rate=0.05, subsample=0.8, colsample_bytree=0.8, reg_lambda=1.0) model.fit(X_tr, y_tr, eval_set=[(X_te, y_te)], early_stopping_rounds=30)
model.feature_importances_
joblib.dump(model, 'churn_xgb.pkl')
num_leaves ≤ 2^{max_depth}
γ
λ
Scenario: Your model’s validation loss keeps decreasing but test AUC stalls. Answer: Apply early stopping or reduce learning_rate. Why: The model is over‑fitting to the training data; early stopping halts before the over‑fit point.
Scenario: You have 10,000 features but only 500 samples, and a tree‑based model over‑fits. Answer: Use bagging with feature subsampling (e.g., max_features=0.3 in RandomForest) or switch to AdaBoost with shallow trees. Why: Subsampling reduces variance and forces the model to consider different feature subsets.
max_features=0.3
Scenario: After stacking, the meta‑learner’s accuracy is worse than the best base model. Answer: Check for data leakage – ensure meta‑features are generated from out‑of‑fold predictions, not the full training set. Why: Leakage gives the meta‑learner unrealistically optimistic training data, leading to poor generalization.
objective='binary:logistic'
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.