集成模型
Bagging思想
- 先构建,后结合(投票法,带权投票法,学习法)
- 个体学习器之间不存在强依赖关系,一系列个体学习器可以并行生成,然后使用组合策略,得到最终的集成模型,这就是Bagging的思想
Bagging分类例题
代码实现
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
import numpy as np
df = pd.DataFrame([[0,1],[1,1],[2,1],[3,-1],[4,-1],
[5,-1],[6,1],[7,1],[8,1],[9,-1]])
M =[]
n_trees = 20
for i in range(n_trees):
tmp = df.sample(frac=1,replace=True)
X = tmp.iloc[:,:-1]
Y = tmp.iloc[:,-1]
model =DecisionTreeClassifier(max_depth=2)
model.fit(X,Y)
M.append(model)
X = df.iloc[:,:-1]
Y = df.iloc[:,-1]
res = 0
for i in M:
res += i.predict(X)
res = np.sign(res)
print(res)
'''
# 效果
>>[ 1 1 1 -1 -1 -1 1 1 1 1]
'''