11
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

モデルの保存と取り出し

Last updated at Posted at 2019-11-16

モデルの保存と取り出し

scikit-learn

import pickle
with open('model.pickle', mode='wb') as fp:
    pickle.dump(clf, fp)

import pickle
with open('model.pickle', mode='rb') as fp:
    clf = pickle.load(fp)
clf.predict(df)

XGBoost

import xgboost as xgb
import pickle

# save the model
model = xgb.XGBClassifier(max_depth=4)
model.fit(X_train, y_train)

pickle.dump(model, open("xgb_model.pickle", "wb"))

# load model from file
loaded_model = pickle.load(open("xgb_model.pickle", "rb"))

y_pred_proba = loaded_model.predict_proba(X_test)
y_pred = loaded_model.predict(X_test)

chainer

# save model
from chainer import serializers

model = L.Classifier(myDNN(100, 500, 10))
serializers.save_npz("mymodel.npz", model) 

# load model
from chainer import serializers

model = L.Classifier(myDNN(100, 500, 10)) # saveした時と同じ構成
serializers.load_npz("mymodel.npz", model)

※注意
セーブした時と、同じ構成でmodelを作ってあげないとロードできない。

11
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?