Serving Logistic Regression from Other Libraries

The Extension for Scikit-learn* offers a prediction-only class daal4py.mb.LogisticDAALModel which can be used to accelerate calculations of predictions from logistic regression models (both binary and multinomial) produced by other libraries such as scikit-learn.

Logistic regression models from scikit-learn (classes sklearn.linear_model.LogisticRegression and sklearn.linear_model.SGDClassifier) can be converted to daal4py’s daal4py.mb.LogisticDAALModel through function daal4py.mb.convert_model in the model builders (mb) submodule, which can also convert gradient-boosted decision tree models (Serving GBT models from other libraries). See About daal4py for more information about the daal4py module.

Example

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
import numpy as np
import daal4py

X, y = load_iris(return_X_y=True)
model_skl = LogisticRegression().fit(X, y)
model_d4p = daal4py.mb.convert_model(model_skl)

np.testing.assert_almost_equal(
    model_d4p.predict(X),
    model_skl.predict(X),
)

np.testing.assert_almost_equal(
    model_d4p.predict_proba(X),
    model_skl.predict_proba(X),
)

Details

Acceleration is achieved by leveraging the MKL library for both faster linear algebra operations and faster exponentials / logarithms on Intel hardware, and optionally by using a lower-precision data type (np.float32) than what sklearn.linear_model.LogisticRegression uses.

If you are already using Python libraries (NumPy and SciPy) linked against MKL - for example, by installing them from the Intel conda channel https://software.repos.intel.com/python/conda/ - then this class might not offer much of a speedup over scikit-learn, but otherwise it offers an easy way to speed up inference by better utilizing capabilities of Intel hardware.

Note that besides the daal4py.mb.convert_model function, class daal4py.mb.LogisticDAALModel can also be instantiated directly from arrays of fitted coefficients and intercepts, thereby allowing to create predictors out of models from other libraries beyond scikit-learn, such as glum.

Documentation

See the section about model builders in the daal4py API reference for full documentation.