.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gyexamples/plot_logistic_regression.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gyexamples_plot_logistic_regression.py: ========================================== Conversion of a logistic regression into C ========================================== Simple example which shows how to predict with a logistic regression using a code implemented in C. This configuration is significantly faster in case of one-off prediction. It usually happens when the machine learned model is embedded in a service. .. GENERATED FROM PYTHON SOURCE LINES 15-16 Train a logistic regression .. GENERATED FROM PYTHON SOURCE LINES 16-28 .. code-block:: default from sklearn.linear_model import LogisticRegression from sklearn.datasets import load_iris from mlprodict.grammar.grammar_sklearn import sklearn2graph iris = load_iris() X = iris.data[:, :2] y = iris.target y[y == 2] = 1 lr = LogisticRegression() lr.fit(X, y) .. raw:: html
LogisticRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 29-30 Conversion into a graph. .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: default gr = sklearn2graph(lr, output_names=['Prediction', 'Score']) .. GENERATED FROM PYTHON SOURCE LINES 33-34 Conversion into C .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: default ccode = gr.export(lang='c') print(ccode['code']) .. rst-class:: sphx-glr-script-out .. code-block:: none int LogisticRegression (float* pred, float* Features) { // 139907793097008-LogisticRegression - children // 139907754562080-concat - children // 139907754562368-sign - children // 139907789016272-+ - children // 139907793169232-adot - children float pred0c0c00c0[2] = {(float)3.3882975578308105, (float)-3.164527654647827}; float* pred0c0c00c1 = Features; // 139907793169232-adot - itself float pred0c0c00; adot_float_float(&pred0c0c00, pred0c0c00c0, pred0c0c00c1, 2); // 139907793169232-adot - done float pred0c0c01 = (float)-8.323304176330566; // 139907789016272-+ - itself float pred0c0c0 = pred0c0c00 + pred0c0c01; // 139907789016272-+ - done // 139907754562368-sign - itself float pred0c0; sign_float(&pred0c0, pred0c0c0); // 139907754562368-sign - done // 139907754562080-concat - itself float pred0[2]; concat_float_float(pred0, pred0c0, pred0c0c0); // 139907754562080-concat - done memcpy(pred, pred0, 2*sizeof(float)); // 139907793097008-LogisticRegression - itself return 0; // 139907793097008-LogisticRegression - done } .. GENERATED FROM PYTHON SOURCE LINES 38-42 This approach may work on small models. On bigger models with many dimensions, it would be better to use AVX instructions and parallelisation. Below, the optimisation this machine can offer. .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: default from mlprodict.testing.experimental_c_impl.experimental_c import code_optimisation print(code_optimisation()) .. rst-class:: sphx-glr-script-out .. code-block:: none AVX-omp=8 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.078 seconds) .. _sphx_glr_download_gyexamples_plot_logistic_regression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_logistic_regression.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_logistic_regression.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_