Sha256: a892dae2a7b614f7d700416ad0d9feb8b5dd4666a9ea8d6db5a8f7e37f912e2b

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require 'pycall/import'

include PyCall::Import

pyimport 'numpy', as: 'np'
pyimport 'matplotlib.pyplot', as: 'plt'

pyfrom 'sklearn.datasets', import: 'fetch_olivetti_faces'
pyfrom 'sklearn.ensemble', import: 'ExtraTreesClassifier'

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

# Load the faces datasets
data = fetch_olivetti_faces.()
x = data.images.reshape.(PyCall.tuple(PyCall.len(data.images), -1))
y = data.target

mask = y < 5  # Limit to 5 classes
x = x[mask]
y = y[mask]

# Build a forest and compute the pixel importances
puts "Fitting ExtraTreesClassifier on faces data with #{n_jobs} cores..."
t0 = Time.now
forest = ExtraTreesClassifier.(
  n_estimators: 1_000,
  max_features: 128,
  n_jobs: n_jobs,
  random_state: 0
)

forest = forest.fit.(x, y)
puts "done in %0.3fs" % (Time.now - 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.()

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pycall-0.1.0.alpha.20170224b examples/plot_forest_importances_faces.rb
pycall-0.1.0.alpha.20170224 examples/plot_forest_importances_faces.rb