본문 바로가기
Python

[python] StandardScaler와 machine learning 모델 공유

by Chandler.j 2021. 6. 24.
반응형

fig 1. title

standardscaler, machine learning 모델 공유

 

0. background

1. scaler (standardscaler)

2. machine learning model (XGBoost)


0. background

 

- machine learning 모델재현가능하게 공유할 때 모델을 저장하고 불러와야함.

- 그전에 standardization을 진행한 scaler도 포함되어야 완벽한 재현이 가능함.

- fit된 scaler와 machine learning 모델을 저장하고 불러와야 기존 결과를 완벽히 재현할 수 있음.

- 핵심은 reproducible !

fig 2. reproducibility

참고: https://machinelearningmastery.com/reproducible-machine-learning-results-by-default/

 

Reproducible Machine Learning Results By Default

It is good practice to have reproducible outcomes in software projects. It might even be standard practice by now, I […]

machinelearningmastery.com


1. scaler (standardscaler)

 

- 데이터를 standardization 해주었던 scaler도 재현 가능해야함.

- scaler를 저장하고 불러올 수 있어야됨.

- 다양한 scikit-learn에서 제공하는 scaler는 다양함.

  -> 예제 : StandardScaler()

 

- save the scaler

import joblib

# scalre save
scaler = StandardScaler()
scaler.fit(std_df)
joblib.dump(scaler, '/home/danssa/proj_ua/shared/scaler60.pkl')

- load the scaler

## scaler
scaler60 = joblib.load('scaler60.pkl')

## standardization by scaler
X_test60_features_non[std_cols]=scaler60.transform(std_60_non)

2. machine learning model (XGBoost)

 

- fit된 모델을 저장하고 불러와서 사용

 

- save the model

import joblib

joblib.dump(model, "/home/danssa/proj_ua/shared/ua60_model")
load_model = joblib.load('ua60_model')

- load the model

## xgb models
model60 = joblib.load('ua60_model')

 


TOP

Designed by 티스토리