#-*-coding: utf8-*-
import numpy as np
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_gaussian_quantiles
X1,y1 = make_gaussian_quantiles(cov=2., n_samples=500, n_features=2, n_classes=2, random_state=1)
X2,y2=make_gaussian_quantiles(mean=(3, 3), cov=1.5,n_samples=400,n_features=2, n_classes=2, random_state=1)
X = np.concatenate((X1,X2))
Y = np.concatenate((y1,-y2+1))
# 多弱分类器合成强分类器使用的最大迭代次数n_estimators
# algorithm参数解析 SAMME.R使用了对样本集分类的预测概率大小来作为弱学习器权重迭代快
# 但 baseestimator必须限制使用支持概率预测的分类器 SAMME无要求
bdt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=3, min_samples_split=20, min_samples_leaf=5), algorithm="SAMME", n_estimators=300, learning_rate=1.1)
bdt.fit(X, Y)
from collections import Counter
from matplotlib import pyplot as plt
print(bdt.score(X, Y))
x_min, x_max = X[:, 0].min()-1, X[:, 0].max()+1
y_min, y_max = X[:, 1].min()-1, X[:, 1].max()+1
xx, yy = np.meshgrid(np.arange(x_min,x_max, 0.02), np.arange(y_min, y_max, 0.02))
Z = bdt.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contour(xx, yy, Z, cmap=plt.cm.Paired)
plt.scatter(X[:, 0], X[:, 1], c=Y)
plt.show()
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交