2011-12-19 20:10:44 +08:00
|
|
|
"""
|
2011-12-30 23:11:20 +08:00
|
|
|
=================================================
|
|
|
|
|
Pixel importances with a parallel forest of trees
|
|
|
|
|
=================================================
|
2011-12-19 20:10:44 +08:00
|
|
|
|
|
|
|
|
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.
|
2011-12-19 20:10:44 +08:00
|
|
|
"""
|
|
|
|
|
print __doc__
|
|
|
|
|
|
2011-12-30 17:00:39 +08:00
|
|
|
from time import time
|
2011-12-20 02:06:58 +08:00
|
|
|
import pylab as pl
|
|
|
|
|
|
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
|
2011-12-19 20:10:44 +08:00
|
|
|
|
2011-12-30 17:00:39 +08:00
|
|
|
# Number of cores to use to perform parallel fitting of the forest model
|
2011-12-30 21:19:12 +08:00
|
|
|
n_jobs = 2
|
2011-12-30 17:00:39 +08:00
|
|
|
|
2011-12-19 20:10:44 +08:00
|
|
|
# Loading the digits 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]
|
2011-12-19 20:10:44 +08:00
|
|
|
|
|
|
|
|
# Build a forest and compute the pixel importances
|
2011-12-30 17:00:39 +08:00
|
|
|
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,
|
|
|
|
|
compute_importances=True,
|
2011-12-30 22:04:24 +08:00
|
|
|
n_jobs=n_jobs,
|
|
|
|
|
random_state=0)
|
2011-12-28 00:54:25 +08:00
|
|
|
|
2011-12-19 20:10:44 +08:00
|
|
|
forest.fit(X, y)
|
2011-12-30 17:00:39 +08:00
|
|
|
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)
|
2011-12-19 20:10:44 +08:00
|
|
|
|
|
|
|
|
# Plot pixel importances
|
2011-12-23 14:37:02 +08:00
|
|
|
pl.matshow(importances, cmap=pl.cm.hot)
|
2011-12-19 20:10:44 +08:00
|
|
|
pl.title("Pixel importances with forests of trees")
|
|
|
|
|
pl.show()
|