Coverage for mlprodict/grammar/grammar_sklearn/g_sklearn_linear_model.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 List of interpreted from scikit-learn model.
5"""
6import numpy
7from .g_sklearn_type_helpers import check_type
8from .grammar.exc import Float32InfError
9from .grammar.gactions import MLActionCst, MLActionVar, MLActionConcat, MLActionReturn
10from .grammar.gactions_num import MLActionAdd, MLActionSign
11from .grammar.gactions_tensor import MLActionTensorDot
12from .grammar.gmlactions import MLModel
15def sklearn_logistic_regression(model, input_names=None, output_names=None, **kwargs):
16 """
17 Interprets a `logistic regression <http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html>`_
18 model into a *grammar* model (semantic graph representation).
20 @param model *scikit-learn* model
21 @param input_names name of the input features
22 @param output_names name of the output predictions
23 @param kwargs additional parameters (*with_loop*)
24 @return graph model
26 If *input* is None or *output* is None, default values
27 will be given to the outputs
28 ``['Prediction', 'Score']`` for the outputs.
29 If *input_names* is None, it wil be ``'Features'``.
31 Additional parameters:
32 - *with_loop*: False by default, *True* not implemented.
33 - *dtype*: float32 or float64
34 """
35 if kwargs.get('with_loop', False):
36 raise NotImplementedError( # pragma: no cover
37 "Loop version is not implemented.")
38 if output_names is None:
39 output_names = ['Prediction', 'Score'] # pragma: no cover
40 if input_names is None:
41 input_names = 'Features'
43 from sklearn.linear_model import LogisticRegression
44 check_type(model, LogisticRegression)
45 if len(model.coef_.shape) > 1 and min(model.coef_.shape) != 1:
46 raise NotImplementedError( # pragma: no cover
47 "Multiclass is not implemented yet: coef_.shape={0}.".format(model.coef_.shape))
48 dtype = kwargs.get('dtype', numpy.float32)
49 coef_ = model.coef_.ravel()
50 coef = coef_.astype(dtype)
51 bias = dtype(model.intercept_[0])
53 for i, c in enumerate(coef):
54 if numpy.isinf(c):
55 raise Float32InfError( # pragma: no cover
56 'Unable to convert coefficient {0}: {1}'.format(i, coef[i]))
57 if numpy.isinf(bias):
58 raise Float32InfError( # pragma: no cover
59 'Unable to convert intercept {0}'.format(model.intercept_[0]))
61 gr_coef = MLActionCst(coef)
62 gr_var = MLActionVar(coef, input_names)
63 gr_bias = MLActionCst(bias)
64 gr_dot = MLActionTensorDot(gr_coef, gr_var)
65 gr_dist = MLActionAdd(gr_dot, gr_bias)
66 gr_sign = MLActionSign(gr_dist)
67 gr_conc = MLActionConcat(gr_sign, gr_dist)
68 ret = MLActionReturn(gr_conc)
69 return MLModel(ret, output_names, name=LogisticRegression.__name__)
72def sklearn_linear_regression(model, input_names=None, output_names=None, **kwargs):
73 """
74 Converts a `linear regression <http://scikit-learn.org/stable/modules/
75 generated/sklearn.linear_model.LinearRegression.html>`_
76 into a *grammar* model (semantic graph representation).
78 @param model *scikit-learn* model
79 @param input_names name of the input features
80 @param output_names name of the output predictions
81 @param kwargs additional parameter (*with_loop*)
82 @return graph model
84 If *input* is None or *output* is None, default values
85 will be given to the outputs
86 ``['Prediction', 'Score']`` for the outputs.
87 If *input_names* is None, it wil be ``'Features'``.
89 Additional parameters:
90 - *with_loop*: False by default, *True* not implemented.
91 - *dtype*: float32 or float64
92 """
93 if kwargs.get('with_loop', False): # pragma: no cover
94 raise NotImplementedError("Loop version is not implemented.")
95 if output_names is None:
96 output_names = ['Prediction', 'Score']
97 if input_names is None:
98 input_names = 'Features'
100 from sklearn.linear_model import LinearRegression
101 check_type(model, LinearRegression)
102 if len(model.coef_.shape) > 1 and min(model.coef_.shape) != 1:
103 raise NotImplementedError( # pragma: no cover
104 "MultiOutput is not implemented yet: coef_.shape={0}.".format(model.coef_.shape))
106 dtype = kwargs.get('dtype', numpy.float32)
107 coef_ = model.coef_.ravel()
108 coef = coef_.astype(dtype)
109 bias = dtype(model.intercept_)
111 for i, c in enumerate(coef):
112 if numpy.isinf(c):
113 raise Float32InfError( # pragma: no cover
114 'Unable to convert coefficient {0}: {1}'.format(i, coef[i]))
115 if numpy.isinf(bias):
116 raise Float32InfError( # pragma: no cover
117 'Unable to convert intercept {0}'.format(model.intercept_))
119 gr_coef = MLActionCst(coef)
120 gr_var = MLActionVar(coef, input_names)
121 gr_bias = MLActionCst(bias)
122 gr_dot = MLActionTensorDot(gr_coef, gr_var)
123 gr_dist = MLActionAdd(gr_dot, gr_bias)
124 ret = MLActionReturn(gr_dist)
125 return MLModel(ret, output_names, name=LinearRegression.__name__)