Saving StandardScaler() model for use on new datasets

后端 未结 2 1396
长情又很酷
长情又很酷 2020-12-15 10:19

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

相关标签:
2条回答
  • 2020-12-15 11:00

    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)
    

    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)
    

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

    To read the model later use load

    sc=load('std_scaler.bin')
    

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

    0 讨论(0)
  • 2020-12-15 11:15

    Or if you like to pickle:

    import pickle
    pickle.dump(sc, open('file/path/scaler.pkl','wb'))
    
    sc = pickle.load(open('file/path/scaler.pkl','rb'))
    
    0 讨论(0)
提交回复
热议问题