Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Machine Learning – how to get the prediction speed of a model?

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading