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

解法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> 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([ TrueTrueTrue])<br>

 (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 @   bonelee  阅读(370)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示