• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
将者,智、信、仁、勇、严也。
Hi,我是李智华,华为-安全AI算法专家,欢迎来到安全攻防对抗的有趣世界。
博客园    首页    新随笔    联系   管理    订阅  订阅

sklearn.preprocessing.StandardScaler 离线使用 不使用pickle如何做

Having said that, you can query sklearn.preprocessing.StandardScaler for the fit parameters:

scale_ : ndarray, shape (n_features,) Per feature relative scaling of the data. New in version 0.17: scale_ is recommended instead of deprecated std_. mean_ : array of floats with shape [n_features] The mean value for each feature in the training set.

The following short snippet illustrates this:

from sklearn import preprocessing
import numpy as np

s = preprocessing.StandardScaler()
s.fit(np.array([[1., 2, 3, 4]]).T)
>>> s.mean_, s.scale_
(array([ 2.5]), array([ 1.11803399]))


参考:https://stackoverflow.com/questions/35944783/how-to-store-scaling-parameters-for-later-use

解法:
>>> from sklearn import preprocessing
>>> import numpy as np
>>> 
>>> s = preprocessing.StandardScaler()
>>> s.fit(np.array([[1., 2, 3, 4]]).T)
StandardScaler(copy=True, with_mean=True, with_std=True)
>>> s.mean_, s.scale_
(array([2.5]), array([1.11803399]))
>>> s.transform(np.array([[1., 2, 3, 4]]).T)
array([[-1.34164079],
       [-0.4472136 ],
       [ 0.4472136 ],
       [ 1.34164079]])
>>> (1-s.mean_)/s.scale_
array([-1.34164079])
>>> a=np.array([1,2,3])
>>> b=np.array([1,2,3])
>>> a==b
array([ True,  True,  True])

 (np.array([1., 2, 3, 4])-s.mean_)/s.scale_
array([-1.34164079, -0.4472136 ,  0.4472136 ,  1.34164079]) 和transform效果一样。

可以看到,离线使用StandardScaler时,只需要s.mean_, s.scale_这两个关键参数即可!

posted @ 2018-05-27 10:44  bonelee  阅读(372)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3