集成学习02:bagging与随机森林

一、原理分析

先看的西瓜书bagging与随机森林部分,形成了一个大概的思维导图。
在这里插入图片描述
Notes:
在使用树模型时,决策树的节点划分过程中所用的指标主要是信息增益和GINI系数。
信息增益:衡量的是划分前后信息不确定性程度的减小。信息不确定程度一般使用信息熵来度量。信息增益IG越大,说明使用该特征划分数据所获得的信息量变化越大,子节点的样本“纯度”越高。
Gini指数:衡量数据的不纯度,一般来说,选择使得划分后Gini指数最小的特征。

二、案例分析

创建一个含有1000个样本20维特征的随机分类数据集

# test classification dataset
from sklearn.datasets import make_classification
# define dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, 
                           n_redundant=5, random_state=5)
# summarize the dataset
print(X.shape, y.shape)

(1000, 20) (1000,)

使用重复的分层k-fold交叉验证来评估该模型,一共重复3次,每次有10个fold。我们将评估该模型在所有重复交叉验证中性能的平均值和标准差
RepeatedStratifiedKFold()函数
功能:交叉验证

参数说明
n_splitsint, default=5 折数,必须至少为2
n_repeatsint, default=10 交叉验证器需要重复的次数。
random_stateint or RandomState instance, default=None 为每次重复设置随机数种子。为多个函数的调用传递可重复输出的int值。
# evaluate bagging algorithm for classification
from numpy import mean
from numpy import std
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import BaggingClassifier
# define dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=5)
# define the model
model = BaggingClassifier()
# evaluate the model
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
n_scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1, error_score='raise')
# report performance
print('Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))

Accuracy: 0.866 (0.040)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值