scikit-learn/examples/ensemble/plot_forest_importances_fac...

50 lines
1.5 KiB
Python
Raw Normal View History

"""
2011-12-30 23:11:20 +08:00
=================================================
Pixel importances with a parallel forest of trees
=================================================
This example shows the use of forests of trees to evaluate the importance
2011-12-20 01:13:45 +08:00
of the pixels in an image classification task (faces). The hotter the pixel,
the more important.
2011-12-30 22:04:24 +08:00
The code below also illustrates how the construction and the computation
of the predictions can be parallelized within multiple jobs.
"""
print(__doc__)
from time import time
import matplotlib.pyplot as plt
2011-12-20 02:06:58 +08:00
2011-12-20 01:13:45 +08:00
from sklearn.datasets import fetch_olivetti_faces
2011-12-20 01:57:15 +08:00
from sklearn.ensemble import ExtraTreesClassifier
# Number of cores to use to perform parallel fitting of the forest model
2012-05-08 04:53:44 +08:00
n_jobs = 1
2012-07-04 18:02:22 +08:00
# Load the faces dataset
2011-12-20 01:13:45 +08:00
data = fetch_olivetti_faces()
X = data.images.reshape((len(data.images), -1))
y = data.target
2012-01-28 21:52:10 +08:00
mask = y < 5 # Limit to 5 classes
2011-12-20 01:13:45 +08:00
X = X[mask]
y = y[mask]
# Build a forest and compute the pixel importances
print("Fitting ExtraTreesClassifier on faces data with %d cores..." % n_jobs)
t0 = time()
2011-12-28 00:54:25 +08:00
forest = ExtraTreesClassifier(n_estimators=1000,
max_features=128,
2011-12-30 22:04:24 +08:00
n_jobs=n_jobs,
random_state=0)
2011-12-28 00:54:25 +08:00
forest.fit(X, y)
print("done in %0.3fs" % (time() - t0))
2011-12-28 00:54:25 +08:00
importances = forest.feature_importances_
2011-12-20 01:13:45 +08:00
importances = importances.reshape(data.images[0].shape)
# Plot pixel importances
plt.matshow(importances, cmap=plt.cm.hot)
plt.title("Pixel importances with forests of trees")
plt.show()