保存 StandardScaler() 模型以用于新数据集 [英] Saving StandardScaler() model for use on new datasets

查看:101
本文介绍了保存 StandardScaler() 模型以用于新数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Sklearn 中保存 StandardScaler() 模型?我需要创建一个可操作的模型,并且不想一次又一次地加载训练数据以供 StandardScaler 学习,然后应用到我想要进行预测的新数据上.

How do I save the StandardScaler() model in Sklearn? I need to make a model operational and don't want to load training data agian and again for StandardScaler to learn and then apply on new data on which I want to make predictions.

from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

#standardizing after splitting
X_train, X_test, y_train, y_test = train_test_split(data, target)
sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)
X_test_std = sc.transform(X_test)

推荐答案

您可以使用 joblib dump 函数来保存标准缩放模型.这里有一个完整的例子供参考.

you could use joblib dump function to save the standard scaler model. Here's a complete example for reference.

from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris

data, target = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(data, target)

sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)

如果您想保存 sc 标准调用程序,请使用以下内容

if you want to save the sc standardscaller use the following

from sklearn.externals.joblib import dump, load
dump(sc, 'std_scaler.bin', compress=True)

这将创建文件 std_scaler.bin 并保存 sklearn 模型.

this will create the file std_scaler.bin and save the sklearn model.

要稍后读取模型,请使用 load

To read the model later use load

sc=load('std_scaler.bin')

注意:sklearn.externals.joblib 已弃用.安装并使用纯 joblib 代替

Note: sklearn.externals.joblib is deprecated. Install and use the pure joblib instead

这篇关于保存 StandardScaler() 模型以用于新数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆