2011-12-30 11:17:30 +08:00
|
|
|
"""
|
2013-01-14 21:15:41 +08:00
|
|
|
==================
|
2013-01-15 19:43:16 +08:00
|
|
|
Two-class AdaBoost
|
2013-01-14 21:15:41 +08:00
|
|
|
==================
|
2011-12-30 11:17:30 +08:00
|
|
|
|
2013-01-31 18:57:07 +08:00
|
|
|
This example fits an AdaBoosted decision stump on a non-linearly separable
|
|
|
|
|
classification dataset composed of two "Gaussian quantiles" clusters
|
|
|
|
|
(see :func:`sklearn.datasets.make_gaussian_quantiles`) and plots the decision
|
2013-02-03 05:53:08 +08:00
|
|
|
boundary and decision scores. The distributions of decision scores are shown
|
|
|
|
|
separately for samples of class A and B. The predicted class label for each
|
|
|
|
|
sample is determined by the sign of the decision score. Samples with decision
|
|
|
|
|
scores greater than zero are classified as B, and are otherwise classified
|
|
|
|
|
as A. The magnitude of a decision score determines the degree of likeness with
|
|
|
|
|
the predicted class label. Additionally, a new dataset could be constructed
|
|
|
|
|
containing a desired purity of class B, for example, by only selecting samples
|
|
|
|
|
with a decision score above some value.
|
2013-01-14 19:32:55 +08:00
|
|
|
|
2011-12-30 11:17:30 +08:00
|
|
|
"""
|
|
|
|
|
|
2014-04-17 08:20:53 +08:00
|
|
|
# Author: Noel Dawe <noel.dawe@gmail.com>
|
|
|
|
|
#
|
|
|
|
|
# License: BSD 3 clause
|
|
|
|
|
|
2011-12-30 11:17:30 +08:00
|
|
|
import numpy as np
|
2014-02-27 17:22:21 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-12-30 11:17:30 +08:00
|
|
|
|
|
|
|
|
from sklearn.ensemble import AdaBoostClassifier
|
2013-01-14 19:32:55 +08:00
|
|
|
from sklearn.tree import DecisionTreeClassifier
|
2013-01-30 08:59:18 +08:00
|
|
|
from sklearn.datasets import make_gaussian_quantiles
|
2022-03-29 22:36:31 +08:00
|
|
|
from sklearn.inspection import DecisionBoundaryDisplay
|
2011-12-30 11:17:30 +08:00
|
|
|
|
|
|
|
|
|
2013-01-30 08:59:18 +08:00
|
|
|
# Construct dataset
|
|
|
|
|
X1, y1 = make_gaussian_quantiles(
|
|
|
|
|
cov=2.0, n_samples=200, n_features=2, n_classes=2, random_state=1
|
|
|
|
|
)
|
|
|
|
|
X2, y2 = make_gaussian_quantiles(
|
|
|
|
|
mean=(3, 3), cov=1.5, n_samples=300, n_features=2, n_classes=2, random_state=1
|
|
|
|
|
)
|
|
|
|
|
X = np.concatenate((X1, X2))
|
|
|
|
|
y = np.concatenate((y1, -y2 + 1))
|
|
|
|
|
|
|
|
|
|
# Create and fit an AdaBoosted decision tree
|
2013-02-04 00:16:36 +08:00
|
|
|
bdt = AdaBoostClassifier(
|
|
|
|
|
DecisionTreeClassifier(max_depth=1), algorithm="SAMME", n_estimators=200
|
|
|
|
|
)
|
2011-12-30 11:17:30 +08:00
|
|
|
|
|
|
|
|
bdt.fit(X, y)
|
|
|
|
|
|
|
|
|
|
plot_colors = "br"
|
|
|
|
|
plot_step = 0.02
|
2013-01-14 19:45:56 +08:00
|
|
|
class_names = "AB"
|
2011-12-30 11:17:30 +08:00
|
|
|
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.figure(figsize=(10, 5))
|
2011-12-30 11:17:30 +08:00
|
|
|
|
2013-01-14 19:32:55 +08:00
|
|
|
# Plot the decision boundaries
|
2022-03-29 22:36:31 +08:00
|
|
|
ax = plt.subplot(121)
|
|
|
|
|
disp = DecisionBoundaryDisplay.from_estimator(
|
|
|
|
|
bdt,
|
|
|
|
|
X,
|
|
|
|
|
cmap=plt.cm.Paired,
|
|
|
|
|
response_method="predict",
|
|
|
|
|
ax=ax,
|
|
|
|
|
xlabel="x",
|
|
|
|
|
ylabel="y",
|
2011-12-30 11:17:30 +08:00
|
|
|
)
|
2022-03-29 22:36:31 +08:00
|
|
|
x_min, x_max = disp.xx0.min(), disp.xx0.max()
|
|
|
|
|
y_min, y_max = disp.xx1.min(), disp.xx1.max()
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.axis("tight")
|
2012-12-08 18:23:58 +08:00
|
|
|
|
2011-12-30 11:17:30 +08:00
|
|
|
# Plot the training points
|
2013-02-14 09:05:35 +08:00
|
|
|
for i, n, c in zip(range(2), class_names, plot_colors):
|
2011-12-30 11:17:30 +08:00
|
|
|
idx = np.where(y == i)
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.scatter(
|
|
|
|
|
X[idx, 0],
|
|
|
|
|
X[idx, 1],
|
2014-04-17 08:20:53 +08:00
|
|
|
c=c,
|
|
|
|
|
cmap=plt.cm.Paired,
|
2017-06-28 20:56:27 +08:00
|
|
|
s=20,
|
|
|
|
|
edgecolor="k",
|
2014-04-17 08:20:53 +08:00
|
|
|
label="Class %s" % n,
|
|
|
|
|
)
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.xlim(x_min, x_max)
|
|
|
|
|
plt.ylim(y_min, y_max)
|
|
|
|
|
plt.legend(loc="upper right")
|
2022-03-29 22:36:31 +08:00
|
|
|
|
2014-10-02 11:30:52 +08:00
|
|
|
plt.title("Decision Boundary")
|
2011-12-30 11:17:30 +08:00
|
|
|
|
2013-01-21 05:23:58 +08:00
|
|
|
# Plot the two-class decision scores
|
|
|
|
|
twoclass_output = bdt.decision_function(X)
|
2013-01-30 08:59:18 +08:00
|
|
|
plot_range = (twoclass_output.min(), twoclass_output.max())
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.subplot(122)
|
2013-02-14 09:05:35 +08:00
|
|
|
for i, n, c in zip(range(2), class_names, plot_colors):
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.hist(
|
|
|
|
|
twoclass_output[y == i],
|
2014-04-17 08:20:53 +08:00
|
|
|
bins=10,
|
|
|
|
|
range=plot_range,
|
|
|
|
|
facecolor=c,
|
|
|
|
|
label="Class %s" % n,
|
2017-06-28 20:56:27 +08:00
|
|
|
alpha=0.5,
|
|
|
|
|
edgecolor="k",
|
|
|
|
|
)
|
2014-02-27 17:22:21 +08:00
|
|
|
x1, x2, y1, y2 = plt.axis()
|
|
|
|
|
plt.axis((x1, x2, y1, y2 * 1.2))
|
|
|
|
|
plt.legend(loc="upper right")
|
|
|
|
|
plt.ylabel("Samples")
|
2014-10-02 11:30:52 +08:00
|
|
|
plt.xlabel("Score")
|
|
|
|
|
plt.title("Decision Scores")
|
2013-01-14 21:15:41 +08:00
|
|
|
|
2014-10-02 11:30:52 +08:00
|
|
|
plt.tight_layout()
|
|
|
|
|
plt.subplots_adjust(wspace=0.35)
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.show()
|