Skip to content

Instantly share code, notes, and snippets.

@glouppe
Last active December 19, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glouppe/5949526 to your computer and use it in GitHub Desktop.
Save glouppe/5949526 to your computer and use it in GitHub Desktop.
import numpy
import random
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original')
# Define training and testing sets
inds = numpy.arange(len(mnist.data))
test_i = random.sample(xrange(len(inds)), int(0.1*len(inds)))
train_i = numpy.delete(inds, test_i)
X_train = mnist.data[train_i].astype(numpy.double)
y_train = mnist.target[train_i].astype(numpy.double)
X_test = mnist.data[test_i].astype(numpy.double)
y_test = mnist.target[test_i].astype(numpy.double)
# scikit-learn single core, MNIST data
from time import time
from sklearn.ensemble import RandomForestClassifier
t1 = time()
rf = RandomForestClassifier(n_estimators=10, max_features="sqrt", n_jobs=1)
rf.fit(X_train, y_train)
score = rf.score(X_test, y_test)
t2 = time()
dt = t2-t1
print "RandomForestClassifier: Accuracy: %0.2f\t%0.2fs" % (score, dt)
from sklearn.ensemble import ExtraTreesClassifier
t1 = time()
rf = ExtraTreesClassifier(n_estimators=10, max_features="sqrt", n_jobs=1)
rf.fit(X_train, y_train)
score = rf.score(X_test, y_test)
t2 = time()
dt = t2-t1
print "ExtraTreesClassifier: Accuracy: %0.2f\t%0.2fs" % (score, dt)
# wiseRF single core, MNIST data
from PyWiseRF import WiseRFClassifier
t1 = time()
rf = WiseRFClassifier(n_estimators=10, n_jobs=1) # max_features=auto == sqrt
rf.fit(X_train, y_train)
score = rf.score(X_test, y_test)
t2 = time()
dt = t2-t1
print "WiseRFClassifier Accuracy: %0.2f\t%0.2fs" % (score, dt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment