Skip to content

Instantly share code, notes, and snippets.

@jnothman
Created June 8, 2013 12:08
Show Gist options
  • Select an option

  • Save jnothman/5734967 to your computer and use it in GitHub Desktop.

Select an option

Save jnothman/5734967 to your computer and use it in GitHub Desktop.
benchmarking sklearn.metrics.precision_recall_fscore_support
import numpy as np, timeit, sklearn.metrics, sklearn.preprocessing, functools
from joblib import Memory
N_TRIALS = 50
memory = Memory('/tmp', verbose=0)
@memory.cache
def gen_mc(s, N=1000000, K=10):
return np.random.randint(K, size=N)
@memory.cache
def gen_ml(s, N=1000, K=10):
r = np.arange(K)
return [r[np.random.randint(2, size=K).astype(bool)].tolist() for i in xrange(N)]
@memory.cache
def gen_ml_li(s, N=1000000, K=10):
lb = sklearn.preprocessing.LabelBinarizer().fit([list(range(K))])
return lb.transform(gen_ml(N, K))
def go(gen):
return timeit.timeit(functools.partial(sklearn.metrics.f1_score, gen('true'), gen('pred')), number=N_TRIALS)
for gen in [gen_mc, gen_ml, gen_ml_li]:
print '%0.3g' % (go(gen) / N_TRIALS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment