sklearn.ensemble 是 Scikit-learn 提供的一个模块,用于实现各种 集成学习 方法。它支持 Bagging、Boosting、随机森林等常用集成学习算法,能够高效地解决分类、回归等任务。
下面详细介绍 sklearn.ensemble 中的主要算法、类和它们的用法。
1. Bagging 方法
Bagging(Bootstrap Aggregating)通过对数据集进行随机采样训练多个弱学习器,最后结合它们的预测结果。
1.1 BaggingClassifier
- 描述:适用于分类问题的 Bagging 方法。
- 主要参数:
base_estimator: 基模型,例如DecisionTreeClassifier(默认是None,表示使用决策树)。n_estimators: 基模型数量。max_samples: 每次采样的数据比例。max_features: 每次采样的特征比例。
- 代码示例:
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
# 创建数据
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Bagging 分类器
model = BaggingClassifier(
base_estimator=DecisionTreeClassifier(),
n_estimators=10,
max_samples=0.8,
random_state=42
)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
1.2 BaggingRegressor
- 描述:适用于回归问题的 Bagging 方法。
- 主要参数:与
BaggingClassifier类似。 - 代码示例:
from sklearn.ensemble import BaggingRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.datasets import make_regression
# 创建数据
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Bagging 回归器
model = BaggingRegressor(
base_estimator=DecisionTreeRegressor(),
n_estimators=10,
max_samples=0.8,
random_state=42
)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
2. 随机森林
随机森林是基于 Bagging 的一种算法,采用决策树作为基模型,并在训练时引入随机性。
2.1 RandomForestClassifier
- 描述:随机森林分类器。
- 主要参数:
n_estimators: 树的数量。max_depth: 每棵树的最大深度。max_features: 用于分裂的最大特征数。bootstrap: 是否使用有放回采样。n_jobs: 并行运行的任务数。
- 代码示例:
from sklearn.ensemble import RandomForestClassifier
# 随机森林分类
model = RandomForestClassifier(
n_estimators=100,
max_depth=10,
random_state=42
)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
2.2 RandomForestRegressor
- 描述:随机森林回归器。
- 代码示例:
from sklearn.ensemble import RandomForestRegressor
# 随机森林回归
model = RandomForestRegressor(
n_estimators=100,
max_depth=10,
random_state=42
)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
3. Boosting 方法
Boosting 是一种串行集成方法,每个基模型关注上一个模型未能正确处理的样本。
3.1 AdaBoost
- 描述:一种经典的 Boosting 方法,基于加权分类器。
- 主要类:
AdaBoostClassifier: 适用于分类任务。AdaBoostRegressor: 适用于回归任务。
- 主要参数:
base_estimator: 基模型(默认是DecisionTreeClassifier)。n_estimators: 基模型数量。learning_rate: 每个弱学习器的权重缩减率。
- 代码示例:
from sklearn.ensemble import AdaBoostClassifier
# AdaBoost 分类
model = AdaBoostClassifier(
base_estimator=DecisionTreeClassifier(max_depth=2),
n_estimators=50,
learning_rate=1.0,
random_state=42
)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
3.2 GradientBoosting
- 描述:梯度提升算法,通过逐步优化模型损失函数。
- 主要类:
GradientBoostingClassifier: 适用于分类任务。GradientBoostingRegressor: 适用于回归任务。
- 主要参数:
learning_rate: 学习率。n_estimators: 基模型数量。max_depth: 每棵树的最大深度。
- 代码示例:
from sklearn.ensemble import GradientBoostingClassifier
# 梯度提升分类
model = GradientBoostingClassifier(
n_estimators=100,
learning_rate=0.1,
max_depth=3,
random_state=42
)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
4. Stacking
Stacking 使用多种基模型,并通过一个元模型(meta-model)结合它们的输出结果。
4.1 StackingClassifier
- 描述:用于分类任务的 Stacking 方法。
- 主要参数:
estimators: 基模型列表(模型名及模型实例)。final_estimator: 元模型(默认是LogisticRegression)。
- 代码示例:
from sklearn.ensemble import StackingClassifier
from sklearn.linear_model import LogisticRegression
# 基模型
estimators = [
('rf', RandomForestClassifier(n_estimators=10, random_state=42)),
('gb', GradientBoostingClassifier(n_estimators=10, random_state=42))
]
# Stacking 分类
model = StackingClassifier(
estimators=estimators,
final_estimator=LogisticRegression()
)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
4.2 StackingRegressor
- 描述:用于回归任务的 Stacking 方法。
- 代码示例:
from sklearn.ensemble import StackingRegressor
from sklearn.linear_model import Ridge
# 基模型
estimators = [
('rf', RandomForestRegressor(n_estimators=10, random_state=42)),
('gb', GradientBoostingRegressor(n_estimators=10, random_state=42))
]
# Stacking 回归
model = StackingRegressor(
estimators=estimators,
final_estimator=Ridge()
)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
5. Voting
Voting 通过简单的投票或加权投票结合多个模型的结果。
5.1 VotingClassifier
- 描述:适用于分类任务。
- 主要参数:
estimators: 基模型列表。voting:'hard'(多数投票)或'soft'(加权投票)。
- 代码示例:
from sklearn.ensemble import VotingClassifier
# Voting 分类
model = VotingClassifier(
estimators=[
('rf', RandomForestClassifier(n_estimators=10, random_state=42)),
('gb', GradientBoostingClassifier(n_estimators=10, random_state=42))
],
voting='soft'
)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
总结
sklearn.ensemble 提供了丰富的集成学习方法,可以应对多种场景:
- Bagging:注重降低方差,适合不稳定的基模型。
- Boosting:注重降低偏差,适合处理复杂任务。
- Stacking:灵活组合多个模型。
- Voting:简单快速的集成方法。
这些方法在分类、回归任务中都有广泛应用,用户可以根据需求选择合适的算法,并通过调节参数优化性能。
1590

被折叠的 条评论
为什么被折叠?



