Im running several machine learning algorithms with sklearn in a for loop and want to see how long each of them takes. The problem is I also need to return a value and DONT want to have to run it more than once because each algorithm takes so long. Is there a way to capture the return value 'clf' using python's timeit module or a similar one with a function like this...
def RandomForest(train_input, train_output):
clf = ensemble.RandomForestClassifier(n_estimators=10)
clf.fit(train_input, train_output)
return clf
when I call the function like this
t = Timer(lambda : RandomForest(trainX,trainy))
print t.timeit(number=1)
P.S. I also dont want to set a global 'clf' because I might want to do multithreading or multiprocessing later.
timeit
if you forcenumber=1
?timeit
is useful to automatically handle repetitive timing, where you don't know how much time you should run the function to get a good timing etc. In your case simply usingtime
would be fine and you wouldn't need any hack to get the return value. – Hamlentime.time()
? Ortime.clock()
? Thetimeit
module uses these functions to perform the timings. If you only have to do one timing you can simply call them directly, in the same way as the_timer
function is used in unutbu answer (that is actually a reference totime.time
ortime.clock
depending on the OS). – Hamlen