Home > Back-end >  Python Machine Learning - how to get the prediction speed of a model?
Python Machine Learning - how to get the prediction speed of a model?

Time:08-27

I am trying to measure the prediction speed (in seconds) of my python machine learning model. I can't find a good answer for this yet. Can you guys help me? this is my example code:

# Create Decision Tree classifer object
clf = DecisionTreeClassifier(criterion="entropy", max_depth=3)

# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)

#Predict the response for test dataset. I want to measure the speed of this prediction here.
y_pred = clf.predict(X_test)

CodePudding user response:

You just need to time it! Use timestamps before and after the inference and take the difference.

import time

#get the timestamp before inference in seconds
start_ts = time.time()

#Predict the response for test dataset. I want to measure the speed of this prediction here.
y_pred = clf.predict(X_test)

#get the timestamp after the inference in second
end_ts = time.time()

# print the time difference in between start and end timestamps in seconds
print(f"Prediction Time [s]: {(end_ts-start_ts):.3f}")

CodePudding user response:

I use below code to measure time taken:

from time import time 

ti = time()
y_pred = clf.predict(X_test)
print('Time taken: {:.1f} sec'.format(time() - ti))

CodePudding user response:

You can save a timestamp before running your prediction code and one after. The duration is then the difference between timestamps. Note the use of time.perf_counter_ns(), which returns monotonic time that only increases. In contrast to time.time(), which can also decrease when for example the system clock is synchronised with an external server.

from time import perf_counter_ns

# Start time stamp
t_start = perf_counter_ns() 

# Predict the response for the test dataset
y_pred = clf.predict(X_test)

# Stop time stamp
t_stop = perf_counter_ns()

# The difference between both time stamps is the duration
duration = t_stop - t_start

print("Elapsed time: {1000 * duration:.2f} ms") 

See also PEP 418 – Add monotonic time, performance counter, and process time functions.

  • Related