基于平行树的森林的像素重要性

这个例子显示了在图像分类任务(Faces)中使用树的森林来评估基于不纯度的像素的重要性。像素越热,越重要。

下面的代码还说明了如何在多个任务中如何并行构造和预测的计算。

Fitting ExtraTreesClassifier on faces data with 1 cores...
done in 1.428s
print(__doc__)

from time import time
import matplotlib.pyplot as plt

from sklearn.datasets import fetch_olivetti_faces
from sklearn.ensemble import ExtraTreesClassifier

# Number of cores to use to perform parallel fitting of the forest model
n_jobs = 1

# Load the faces dataset
data = fetch_olivetti_faces()
X, y = data.data, data.target

mask = y < 5  # Limit to 5 classes
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()
forest = ExtraTreesClassifier(n_estimators=1000,
                              max_features=128,
                              n_jobs=n_jobs,
                              random_state=0)

forest.fit(X, y)
print("done in %0.3fs" % (time() - t0))
importances = forest.feature_importances_
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()

脚本的总运行时间:(0分1.619秒)

Download Python source code:plot_forest_importances_faces.py

Download Jupyter notebook:plot_forest_importances_faces.ipynb