2011-04-02 05:28:20 +08:00
|
|
|
"""
|
2011-11-16 22:54:06 +08:00
|
|
|
================================================================
|
|
|
|
|
Plot the decision surface of a decision tree on the iris dataset
|
|
|
|
|
================================================================
|
2011-04-02 05:28:20 +08:00
|
|
|
|
2013-07-22 21:48:44 +08:00
|
|
|
Plot the decision surface of a decision tree trained on pairs
|
2011-11-17 02:13:23 +08:00
|
|
|
of features of the iris dataset.
|
2011-09-26 01:30:23 +08:00
|
|
|
|
2013-07-22 21:48:44 +08:00
|
|
|
See :ref:`decision tree <tree>` for more information on the estimator.
|
|
|
|
|
|
2011-11-16 22:54:06 +08:00
|
|
|
For each pair of iris features, the decision tree learns decision
|
|
|
|
|
boundaries made of combinations of simple thresholding rules inferred from
|
|
|
|
|
the training samples.
|
2011-04-02 05:28:20 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-04-02 05:28:20 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-12-19 22:53:00 +08:00
|
|
|
|
2011-11-16 21:44:16 +08:00
|
|
|
from sklearn.datasets import load_iris
|
2011-09-04 02:58:20 +08:00
|
|
|
from sklearn.tree import DecisionTreeClassifier
|
2011-04-02 05:28:20 +08:00
|
|
|
|
2011-11-16 21:44:16 +08:00
|
|
|
# Parameters
|
|
|
|
|
n_classes = 3
|
2017-08-16 07:01:28 +08:00
|
|
|
plot_colors = "ryb"
|
2011-11-16 21:44:16 +08:00
|
|
|
plot_step = 0.02
|
2011-12-19 22:53:00 +08:00
|
|
|
|
2011-11-16 21:44:16 +08:00
|
|
|
# Load data
|
|
|
|
|
iris = load_iris()
|
|
|
|
|
|
2011-09-26 01:30:23 +08:00
|
|
|
for pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3],
|
|
|
|
|
[1, 2], [1, 3], [2, 3]]):
|
2014-05-15 12:05:24 +08:00
|
|
|
# We only take the two corresponding features
|
2011-11-16 21:44:16 +08:00
|
|
|
X = iris.data[:, pair]
|
2011-04-02 05:28:20 +08:00
|
|
|
y = iris.target
|
|
|
|
|
|
2011-11-16 21:44:16 +08:00
|
|
|
# Train
|
2011-08-11 18:17:14 +08:00
|
|
|
clf = DecisionTreeClassifier().fit(X, y)
|
2011-04-02 05:28:20 +08:00
|
|
|
|
2011-11-16 21:44:16 +08:00
|
|
|
# Plot the decision boundary
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.subplot(2, 3, pairidx + 1)
|
2011-11-16 21:44:16 +08:00
|
|
|
|
2011-04-02 05:28:20 +08:00
|
|
|
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
|
|
|
|
|
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
|
2011-11-16 21:44:16 +08:00
|
|
|
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
|
|
|
|
|
np.arange(y_min, y_max, plot_step))
|
2017-08-16 07:01:28 +08:00
|
|
|
plt.tight_layout(h_pad=0.5, w_pad=0.5, pad=2.5)
|
2011-04-02 05:28:20 +08:00
|
|
|
|
|
|
|
|
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
|
|
|
|
|
Z = Z.reshape(xx.shape)
|
2017-08-16 07:01:28 +08:00
|
|
|
cs = plt.contourf(xx, yy, Z, cmap=plt.cm.RdYlBu)
|
2011-11-16 22:54:06 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlabel(iris.feature_names[pair[0]])
|
|
|
|
|
plt.ylabel(iris.feature_names[pair[1]])
|
2011-04-02 05:28:20 +08:00
|
|
|
|
2011-11-16 21:44:16 +08:00
|
|
|
# Plot the training points
|
2013-02-14 09:05:35 +08:00
|
|
|
for i, color in zip(range(n_classes), plot_colors):
|
2011-04-02 05:28:20 +08:00
|
|
|
idx = np.where(y == i)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i],
|
2017-08-16 07:01:28 +08:00
|
|
|
cmap=plt.cm.RdYlBu, edgecolor='black', s=15)
|
2011-04-02 05:28:20 +08:00
|
|
|
|
2016-02-04 23:09:36 +08:00
|
|
|
plt.suptitle("Decision surface of a decision tree using paired features")
|
2017-08-16 07:01:28 +08:00
|
|
|
plt.legend(loc='lower right', borderpad=0, handletextpad=0)
|
|
|
|
|
plt.axis("tight")
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|