Coverage for mlprodict/grammar/grammar_sklearn/g_sklearn_main.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Main functions to convert machine learned model from *scikit-learn* model.
5"""
6from .g_sklearn_identify import identify_interpreter
9def sklearn2graph(model, output_names=None, **kwargs):
10 """
11 Converts any kind of *scikit-learn* model into a *grammar* model.
13 @param model scikit-learn model
14 @param output_names names of the outputs
15 @param kwargs additional parameters, sent to the converter
16 @return converter to grammar model
18 Short list of additional parameters:
19 - *with_loop*: the pseudo code includes loops,
20 this option is not available everywhere.
22 If *output_names* is None, default values
23 will be given to the inputs and outputs.
24 One example on how to use this function.
25 A *scikit-learn* model is trained and converted
26 into a graph which implements the prediction
27 function with the *grammar* language.
29 .. runpython::
30 :showcode:
31 :warningout: DeprecationWarning
33 from sklearn.linear_model import LogisticRegression
34 from sklearn.datasets import load_iris
35 iris = load_iris()
36 X = iris.data[:, :2]
37 y = iris.target
38 y[y == 2] = 1
39 lr = LogisticRegression()
40 lr.fit(X, y)
42 # grammar is the expected scoring model.
43 from mlprodict.grammar.grammar_sklearn import sklearn2graph
44 gr = sklearn2graph(lr, output_names=['Prediction', 'Score'])
46 # We can even check what the function should produce as a score.
47 # Types are strict.
48 import numpy
49 X = numpy.array([[numpy.float32(1), numpy.float32(2)]])
50 e2 = gr.execute(Features=X[0, :])
51 print(e2)
53 # We display the result in JSON.
54 ser = gr.export(lang='json', hook={'array': lambda v: v.tolist(),
55 'float32': lambda v: float(v)})
56 import json
57 print(json.dumps(ser, sort_keys=True, indent=2))
59 For this particular example, the function is calling
60 :func:`sklearn_logistic_regression <mlprodict.grammar_sklearn.sklearn_converters_linear_model.sklearn_logistic_regression>`
61 and the code which produces the model looks like:
63 ::
65 model = LogisticRegression()
66 model.fit(...)
68 coef = model.coef_.ravel()
69 bias = numpy.float32(model.intercept_[0])
71 gr_coef = MLActionCst(coef)
72 gr_var = MLActionVar(coef, input_names)
73 gr_bias = MLActionCst(bias)
74 gr_dot = MLActionTensorDot(gr_coef, gr_var)
75 gr_dist = MLActionAdd(gr_dot, gr_bias)
76 gr_sign = MLActionSign(gr_dist)
77 gr_conc = MLActionConcat(gr_sign, gr_dist)
78 gr_final = MLModel(gr_conc, output_names, name="LogisticRegression")
80 The function interal represents any kind of function into a graph.
81 This graph can easily exported in any format, :epkg:`Python` or any other programming
82 language. The goal is not to evaluate it as it is slow due to the extra
83 checkings ran all along the evaluation to make sure types are consistent.
84 The current implementation supports conversion into C.
86 .. runpython::
87 :showcode:
88 :warningout: DeprecationWarning
90 from sklearn.linear_model import LogisticRegression
91 from sklearn.datasets import load_iris
92 iris = load_iris()
93 X = iris.data[:, :2]
94 y = iris.target
95 y[y == 2] = 1
96 lr = LogisticRegression()
97 lr.fit(X, y)
99 # a grammar tree is the expected scoring model.
100 from mlprodict.grammar.grammar_sklearn import sklearn2graph
101 gr = sklearn2graph(lr, output_names=['Prediction', 'Score'])
103 # We display the result in JSON.
104 ccode = gr.export(lang='c')
105 # We print after a little bit of cleaning.
106 print("\\n".join(_ for _ in ccode['code'].split("\\n") if "//" not in _))
108 Function ``adot``, ``sign``, ``concat`` are implemented in module
109 :mod:`mlprodict.grammar_sklearn.cc.c_compilation`. Function
110 :func:`compile_c_function <mlprodict.grammar_sklearn.cc.c_compilation.compile_c_function>`
111 can compile this with :epkg:`cffi`.
113 ::
115 from mlprodict.grammar_sklearn.cc.c_compilation import compile_c_function
116 fct = compile_c_function(code_c, 2)
117 e2 = fct(X[0, :])
118 print(e2)
120 The output is the same as the prediction given by *scikit-learn*.
121 """
122 conv = identify_interpreter(model)
123 return conv(model, output_names=output_names, **kwargs)