scikit-learn/examples/tree/plot_iris.py

78 lines
2.0 KiB
Python
Raw Normal View History

"""
2011-11-16 22:54:06 +08:00
================================================================
Plot the decision surface of a decision tree on the iris dataset
================================================================
Plot the decision surface of a :ref:`decision tree <tree>` trained on pairs
of features of the iris dataset.
2011-09-26 01:30:23 +08:00
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.
"""
print __doc__
import numpy as np
import pylab as pl
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-11-16 21:44:16 +08:00
# Parameters
n_classes = 3
plot_colors = "bry"
plot_step = 0.02
2011-12-19 22:53:00 +08:00
2011-11-16 21:44:16 +08:00
pl.set_cmap(pl.cm.Paired)
# 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]]):
2011-11-16 21:44:16 +08:00
# We only take the two corresponding features
X = iris.data[:, pair]
y = iris.target
2011-11-16 21:44:16 +08:00
# Shuffle
idx = np.arange(X.shape[0])
np.random.seed(13)
np.random.shuffle(idx)
X = X[idx]
y = y[idx]
2011-11-16 21:44:16 +08:00
# Standardize
mean = X.mean(axis=0)
std = X.std(axis=0)
X = (X - mean) / std
2011-11-16 21:44:16 +08:00
# Train
clf = DecisionTreeClassifier().fit(X, y)
2011-11-16 21:44:16 +08:00
# Plot the decision boundary
pl.subplot(2, 3, pairidx + 1)
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))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
cs = pl.contourf(xx, yy, Z)
2011-11-16 22:54:06 +08:00
pl.xlabel(iris.feature_names[pair[0]])
pl.ylabel(iris.feature_names[pair[1]])
2011-11-16 21:44:16 +08:00
pl.axis("tight")
2011-11-16 21:44:16 +08:00
# Plot the training points
for i, color in zip(xrange(n_classes), plot_colors):
idx = np.where(y == i)
pl.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i])
2011-11-16 21:44:16 +08:00
pl.axis("tight")
2011-11-16 22:54:06 +08:00
pl.suptitle("Decision surface of a decision tree using paired features")
pl.legend()
pl.show()