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
|
|
|
|
2020-02-01 20:18:11 +08:00
|
|
|
This example shows the use of forests of trees to evaluate the impurity-based
|
|
|
|
|
importance 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
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-12-19 20:10:44 +08:00
|
|
|
|
2011-12-30 17:00:39 +08:00
|
|
|
from time import time
|
2014-02-27 17:22:21 +08:00
|
|
|
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
|
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
|
2012-05-08 04:53:44 +08:00
|
|
|
n_jobs = 1
|
2011-12-30 17:00:39 +08:00
|
|
|
|
2012-07-04 18:02:22 +08:00
|
|
|
# Load the faces dataset
|
2011-12-20 01:13:45 +08:00
|
|
|
data = fetch_olivetti_faces()
|
2019-07-20 10:30:09 +08:00
|
|
|
X, y = data.data, data.target
|
2011-12-20 01:13:45 +08:00
|
|
|
|
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
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Fitting ExtraTreesClassifier on faces data with %d cores..." % n_jobs)
|
2011-12-30 17:00:39 +08:00
|
|
|
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
|
|
|
|
2011-12-19 20:10:44 +08:00
|
|
|
forest.fit(X, y)
|
2013-02-01 22:04:03 +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
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.matshow(importances, cmap=plt.cm.hot)
|
|
|
|
|
plt.title("Pixel importances with forests of trees")
|
|
|
|
|
plt.show()
|