Coverage for mlprodict/grammar/grammar_sklearn/g_sklearn_preprocessing.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 Converters from scikit-learn model.
5"""
6import numpy
7from .g_sklearn_type_helpers import check_type
8from .grammar.gactions import MLActionVar, MLActionCst, MLActionReturn
9from .grammar.gactions_tensor import MLActionTensorDiv, MLActionTensorSub
10from .grammar.gmlactions import MLModel
13def 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).
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
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'``.
29 No additional parameters is considered.
30 """
31 if output_names is None:
32 output_names = ['Prediction', 'Score']
33 if input_names is None:
34 input_names = 'Features'
36 from sklearn.preprocessing import StandardScaler
37 check_type(model, StandardScaler)
39 lmean = MLActionCst(model.mean_.ravel().astype(numpy.float32))
40 lscale = MLActionCst(model.scale_.ravel().astype(numpy.float32))
42 lvar = MLActionVar(model.var_.astype(numpy.float32), input_names)
43 lno = MLActionTensorSub(lvar, lmean)
44 lno = MLActionTensorDiv(lno, lscale)
45 ret = MLActionReturn(lno)
46 return MLModel(ret, output_names, name=StandardScaler.__name__)