Boosting speed of scikit-learn regression algorithms

When browsing the web, numerous posts can be found discussing techniques to speed up the training time of well-known machine learning algorithms. Surprisingly, there is limited information available regarding the prediction phase. However, from a practical standpoint, this aspect holds great relevance. Once a satisfactory regression algorithm has been trained, it is typically deployed in a real-world system. In such cases, the speed at which predictions are obtained becomes crucial. Faster prediction times enable real-time or near-real-time decision-making, enhance user experience in interactive applications, and facilitate efficient processing of large-scale datasets. Therefore, optimizing inference speed can have significant implications for various domains and applications.

The purpose of this blog post is to investigate the performance and prediction speed behavior of popular regression algorithms, i.e. models that predict numerical values based on a set of input variables. Considering that scikit-learn is the most extensively utilized machine learning framework [1], our focus will be on exploring methods to accelerate its algorithms' predictions.

Benchmarking regression algorithms

To assess the current state of popular regression algorithms, we selected four popular regression datasets from Kaggle [2], along with an internal dataset from our company. These datasets vary in sample size, number and type of features, capturing performance for different data structures.

To ensure fair comparisons, we need to optimize hyperparameters before testing to unlock the models' full potential. We will benchmark the following regression algorithms:

 

The different versions of regularized linear regression, such as lasso, ridge, and elastic net, are not analyzed separately as they were comparable to pure linear regression in terms of prediction speed and accuracy in a pre-evaluation step.

Prediction speed vs. accuracy

The plot below displays the benchmarking results on our company's internal dataset. We can observe a sweet spot in the bottom left, where both the error - measured via root mean square error (RMSE) - and prediction times are low. Simple neural networks (Multilayer Perceptron, MLP) and gradient boosted regression trees demonstrate good performance in both dimensions. Random forest also shows decent accuracy but has the highest prediction time. Other algorithms exhibit reasonable prediction speed but relatively high errors.

 

However, it is crucial to try different algorithms on the specific dataset at hand. Accuracy and prediction time heavily depend on the number of features used, their transformations, as well as the model's parameters. Linear models, for example, may perform well with properly transformed features, while larger MLPs might exhibit longer prediction times. Nevertheless, algorithms like random forest and k-NN are by construction expected to be slower in inference speed.

How to speed up inference

Generally, scikit-learn models are already optimized through compiled Cython extensions or optimized computing libraries [3]. However, there are additional ways to accelerate prediction latency, apart from using faster hardware. In this blog post, we’ll benchmark the following optimizations:

Data-based approaches:

Implementation-based approaches:

Furthermore, we want to mention the following optimization approaches, which we did not include in our benchmark, partly because they are problem specific:

Data-based approaches:

Model-based approaches:

Implementation-based approaches:

  • Implement the prediction step with given weights independently, potentially in a faster programming language, to avoid unnecessary overhead

  • Use cloud services for prediction, such as Google ML, Amazon ML or MS Azure

As you can see, there are numerous ways to influence inference time, ranging from fundamental approaches to simple tricks. Changing the data structure and implementing algorithms from scratch optimized for efficiency may be more involved, while the latter approaches can be easily applied even to existing systems that use scikit-learn.

Note that all of the above approaches do not affect prediction quality, except reducing the number of features and model complexity. For these approaches, it is important to evaluate the trade-off between prediction speed and quality.

In this blog post, we mostly benchmark approaches that do not affect prediction quality, and therefore focus on evaluating the speedup in the next section.

Evaluating some speedup tricks

Check out the technical appendix to see how the time measurement is performed.

Reducing the number of features by half (in our case from 106 to 53 features) only leads to small decreases in inference speed for KNN, SVR while it had an major influence on the MLP. Disabling scikit-learn's finiteness checkup, which is just one line of code, improves prediction speed more significantly. As can be seen below, inference time can be reduced up to 40% depending on the algorithm. Utilizing the Intel extension for scikit-learn, also requiring only one line of code, results in substantial speed improvements for random forest, SVR and the KNN regressor. For the latter two algorithms, a time reduction of more than 50% could be achieved, while for random forest, prediction time decreases by impressive 98%. In the plots below there are no values shown for the other algorithms as the Intel extension currently does not support those.

 

As can be seen below, most potential lies in bulk inference. By predicting several samples simultaneously (here: 100 or 1000 samples at once), the average prediction time per sample decreases significantly for most of the algorithms. Overall, bulk prediction can lead up to 200-fold speed increases in this test setting. This approach is particularly effective for the MLP as well as linear and tree based methods, greatly accelerating their performance.

 

Summary

Fast predictions are crucial for various use cases, in particular when it comes to real-time predictions. Moreover, investing in efficiency always pays off by reducing energy consumption, thus saving money and at the same time lowering carbon emissions.

In this blog post we have explored multiple ways to achieve faster prediction times. Firstly, the dimensionality of the data and the algorithm chosen have major influence on inference speed and scalability behaviour. However, there are various tricks to even accelerate existing scikit-learn code. Disabling scikit-learn's finite data validation or utilizing the Intel extension for supported algorithms can already yield considerable improvements depending on the algorithm. However, the most substantial gains can be achieved by addressing fundamental aspects, such as reducing the number of features (in particular for high-dimensional data), implementing bulk prediction or custom prediction methods. These strategies can result in speed increases by factors of several hundred.

In our small test setting, we could additionally show that a small neural network, gradient boosted regressor and random forest appear to be the most promising choices in terms of both accuracy and speed, when using the above-mentioned speedup tricks.


Sources

[1] https://storage.googleapis.com/kaggle-media/surveys/Kaggle%20State%20of%20Machine%20Learning%20and%20Data%20Science%202020.pdf 

[2] House sales: House Sales in King County, USA

red wine quality: Red Wine Quality ,

avocado prices: Avocado Prices ,

medical insurance costs: Medical Cost Personal Datasets

[3] 8. Computing with scikit-learn — scikit-learn 0.23.2 documentation

 


Technical Appendix

Speedtests were performed with all unnecessary background processes stopped.

 

Inference time measurement for one test sample (“atomic prediction”):

n = 500 # number of consecutive runs r = 10 # number of repeats of above pred_times = timeit.repeat(stmt=lambda: model.predict(X_test[0]), repeat=r, number=n) pred_times = np.array(pred_times) / n # divide by number of runs pred_time = np.min(pred_times) # take minimum of all repetitions

Inference time measurement for several samples at once (“bulk prediction”):

n = 50 # number of consecutive runs r = 5 # number of repeats of above X_test_sample = X_test[0:1000] # 100 or 1000 pred_times = timeit.repeat(stmt=lambda: model.predict(X_test_sample), repeat=r, number=n) pred_times = np.array(pred_times) / n # divide by number of runs pred_times = pred_times / len(X_test_sample) # divide by number of samples pred_time = np.min(pred_times) # take minimum of all repetitions

With “model” being the scikit-learn models mentioned above which were trained with the first 10.000 observations of the “house sales” data and using default model paramters.

 

Versions used:

  • Python: 3.9.7

  • Scikit-learn: 1.0.2

  • Scikit-learn-intelex: 2021.20210714.120553

Blog 6/22/23

Strategic Impact of Large Language Models

This blog discusses the rapid advancements in large language models, particularly highlighting the impact of OpenAI's GPT models.

Blog

What is SAP S/4HANA Cloud Public Edition?

This blog post covers everything you need to know about SAP S/4HANA Cloud Public Edition, including its key features, implementation options, and the advantages of adopting this powerful ERP solution.

Blog 5/1/21

Ways of Creating Single Case Discriminated Unions in F#

There are quite a few ways of creating single case discriminated unions in F# and this makes them popular for wrapping primitives.

Messeszene des Ledvance Messestands
Success Story

Ledvance: Efficient management of SharePoint migrations

Ledvance is migrating its data to the hybrid world: thanks to the enhanced features of the novaCapta Migration Manager, this mammoth project went off without a hitch.

Blog 4/8/25

The End of SAP Business ByDesign

This article explores why the end of SAP Business ByDesign is being widely anticipated, what this means for your business, and how you can plan a successful transition to future-ready ERP solutions.

Partner

IPG is Partner of Okta

Okta is one of the leading independent identity providers for companies around the world.

Microsoft Keyvisual
Partner 5/23/22

IPG is Partner of Microsoft

Microsoft is one of the leading software providers worldwide, with its own identity manager and many ideas for the future in the cloud.

Wissen 5/2/24

Unlock the Potential of Data Culture in Your Organization

Are you ready to revolutionize your organization's potential by unleashing the power of data culture? Imagine a workplace where every decision is backed by insights, every strategy informed by data, and every employee equipped to navigate the digital landscape with confidence. This is the transformative impact of cultivating a robust data culture within your enterprise.

Titelbild IPG Partner Imprivata
Partner

IPG is Premier Partner of Imprivata

Imprivata is a leading provider of authentication access management solutions for the healthcare sector.

Headerbild zum TIMETOACT Onboarding
Referenz

Onboarding solution of TIMETOACT

Introducing new employees to the company is faster, easier and more efficient with an efficient ticket system in Jira, for example. Our experts have developed a solution for this.

Referenz

Mix of IASP & ILMT support for optimal license management

UTA resorts to proactive management of the license inventory (IASP) by TIMETOACT. In this way, TIMETOACT will ensure compliance-compliant use of the ILMT as part of license management.

Blog

catworkx behind the scenes - „The Lord of the Screens”

IIn our new blog article, we take a look behind the scenes and see who actually works at catworkx. Today: The lord of the screens.

Unternehmen

Contact

This is how you reach the CLOUDPILOTS!

Keyvisual_eviden
Partner

IPG is Premium Partner of Eviden

Eviden is an independent software vendor (ISV) and a market leader in the field of Single Sign-On.

News 1/1/22

Appointment of a new Managing Director as of 1 January 2022

With continuity into the cloud - Robin Müller-Cajar is appointed Managing Director on January 1, 2022 and drives the further expansion of product development.

Blog 8/7/20

Understanding F# Type Aliases

In this post, we discuss the difference between F# types and aliases that from a glance may appear to be the same thing.

Referenz

Schaeffler: Atlassian tools successfully transformed

Schaeffler is focusing on digitalization and transforming its Atlassian infrastructure. catworkx is supporting the strategy and implementation of this ambitious project.

Führender Atlassian-Champion STAGIL wird Teil der Timetoact Group
News 7/6/23

Leading Atlassian Champion: STAGIL becomes part of TIMETOACT

TIMETOACT GROUP, a leading provider of IT services for upper mid-sized companies, corporations and public institutions, acquires STAGIL, one of Germany's largest Atlassian Platinum and Enterprise Solution Partners: With this acquisition, TIMETOACT GROUP's Atlassian consulting portfolio, which is managed under the catworkx brand, moves up into the top league in the German-speaking region. The former STAGIL managing director Björn Frauen becomes co-managing director of catworkx Germany in the course of the merger. He will also become a shareholder in TIMETOACT GROUP. The parties have agreed not to disclose details of the transaction.

Headerbild zu Digitale Transformation bei Versicherern
Leistung

Mastering digital transformation in insurance

Digital transformation is the transformation of the corporate world through new technologies and the Internet ► Learn how insurers can master this.

News 12/11/24

JOIN(+) becomes part of TIMETOACT GROUP

Cologne/Villingen-Schwenningen, 11 December 2024 – TIMETOACT GROUP is acquiring JOIN(+), a consulting company in the field of Big Data & AI. The managing directors will continue to manage the company.

Bleiben Sie mit dem TIMETOACT GROUP Newsletter auf dem Laufenden!