1
|
|
# -*- coding: utf-8 -*-
|
2
|
3
|
"""
|
3
|
|
@file
|
4
|
|
@brief Converters from scikit-learn model.
|
5
|
|
"""
|
6
|
3
|
import numpy
|
7
|
3
|
from .g_sklearn_type_helpers import check_type
|
8
|
3
|
from .grammar.gactions import MLActionVar, MLActionCst, MLActionReturn
|
9
|
3
|
from .grammar.gactions_tensor import MLActionTensorDiv, MLActionTensorSub
|
10
|
3
|
from .grammar.gmlactions import MLModel
|
11
|
|
|
12
|
|
|
13
|
3
|
def sklearn_standard_scaler(model, input_names=None, output_names=None, **kwargs):
|
14
|
|
"""
|
15
|
|
Converts a `standard scaler <http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html>`_
|
16
|
|
model into a *grammar* model (semantic graph representation).
|
17
|
|
|
18
|
|
@param model scikit-learn model
|
19
|
|
@param input_names name of the input features
|
20
|
|
@param output_names name of the output predictions
|
21
|
|
@param kwargs additional parameters (none)
|
22
|
|
@return graph model
|
23
|
|
|
24
|
|
If *input* is None or *output* is None, default values
|
25
|
|
will be given to the outputs
|
26
|
|
``['Prediction', 'Score']`` for the outputs.
|
27
|
|
If *input_names* is None, it wil be ``'Features'``.
|
28
|
|
|
29
|
|
No additional parameters is considered.
|
30
|
|
"""
|
31
|
3
|
if output_names is None:
|
32
|
3
|
output_names = ['Prediction', 'Score']
|
33
|
3
|
if input_names is None:
|
34
|
3
|
input_names = 'Features'
|
35
|
|
|
36
|
3
|
from sklearn.preprocessing import StandardScaler
|
37
|
3
|
check_type(model, StandardScaler)
|
38
|
|
|
39
|
3
|
lmean = MLActionCst(model.mean_.ravel().astype(numpy.float32))
|
40
|
3
|
lscale = MLActionCst(model.scale_.ravel().astype(numpy.float32))
|
41
|
|
|
42
|
3
|
lvar = MLActionVar(model.var_.astype(numpy.float32), input_names)
|
43
|
3
|
lno = MLActionTensorSub(lvar, lmean)
|
44
|
3
|
lno = MLActionTensorDiv(lno, lscale)
|
45
|
3
|
ret = MLActionReturn(lno)
|
46
|
3
|
return MLModel(ret, output_names, name=StandardScaler.__name__)
|