当做重要决定时,大家可能都会考虑吸取多个专家而不只是一个人的意见。集成学习也是如此。集成学习就是组合多个学习器,最后可以得到一个更好的学习器。
When making important decisions, everyone may consider drawing from multiple experts rather than just one person. The same is true for ensemble learning. Ensemble learning is to combine multiple learners, and finally a better learner can be obtained.
集成学习算法:
1.个体学习器之间不存在强依赖关系,装袋(bagging)
2.随机森林(Random Forest)
3.个体学习器之间存在强依赖关系,提升(boosting)
4.Stacking
Here I explain the bagging and RF.
1. bagging也叫做bootstrap aggregating,是在原始数据集选择S次后得到S个新数据集的一种技术。是一种有放回抽样。
code
#bagging的思想是对并行分类器的选择
# 导入算法包以及数据集
from sklearn import neighbors
from sklearn import datasets
from sklearn.ensemble import BaggingClassifier
from sklearn import tree
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
iris = datasets.load_iris()
x_data = iris.data[:,:2]
y_data = iris.target
x_train,x_test,y_train,y_test = train_test_split(x_data, y_data)
knn = neighbors.KNeighborsClassifier()
knn.fit(x_train, y_train)
def plot(model):
# 获取数据值所在的范围
x_min, x_max = x_data[:, 0].min() - 1, x_data[:, 0].max() + 1
y_min, y_max = x_data[:, 1].min() - 1, x_data[:, 1]