Scikit-learn: パイプラインからプルされたpickle化モデルをロードするときにインポートエラーが発生しました

作成日 2020年02月27日  ·  3コメント  ·  ソース: scikit-learn/scikit-learn

バグを説明する

sklearnパイプラインからプルされたRandomForestClassifierをピクルス化すると、別のノートブックにロードするときにModuleNotFoundErrorになるように見えます。 誤ったモジュールは存在しますが、見つかりません: sklearn.ensemble._forest

再現する手順/コード

from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import make_pipeline
import pickle
import numpy as np


pipeline = make_pipeline(
    # Other steps in pipeline as well
    RandomForestClassifier(),
)

# Create some fake data
X_train = np.array([[2,8,5],[4,7,2],[1,9,4]])
y_train = np.array([26, 29, 18])
# Train the model
pipeline.fit(X_train, y_train)

# Pickle the model
model = pipeline.named_steps['randomforestclassifier']
outfile = open("model.pkl", "wb")
pickle.dump(model, outfile)
outfile.close()

別のノートブック:

from sklearn.ensemble import RandomForestClassifier
import pickle


# Attempt to load the pickled model in another file / notebook:
infile = open("model.pkl", "rb")
model = pickle.load(infile)
infile.close()

# It's lonely over here
model

推測される結果

RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None,
                       criterion='gini', max_depth=None, max_features='auto',
                       max_leaf_nodes=None, max_samples=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, n_estimators=100,
                       n_jobs=None, oob_score=False, random_state=None,
                       verbose=0, warm_start=False)

実績

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-20-c8d1783e8b58> in <module>
      5 # Attempt to load the pickled model in another file / notebook:
      6 infile = open("model.pkl", "rb")
----> 7 model = pickle.load(infile)
      8 infile.close()

ModuleNotFoundError: No module named 'sklearn.ensemble._forest'

バージョン

System:
    python: 3.7.4 (default, Oct  9 2019, 16:55:50)  [GCC 7.4.0]
executable: /usr/local/bin/python3.7
   machine: Linux-4.15.0-88-generic-x86_64-with-debian-buster-sid

Python deps:
       pip: 19.3
setuptools: 40.8.0
   sklearn: 0.21.3
     numpy: 1.17.2
     scipy: 1.3.1
    Cython: None
    pandas: 0.25.1

編集:説明を修正

Question

全てのコメント3件

sklearn.ensemble._forestからインポートできないようです。 これは意図されたものですか?

パイプラインをピクルス化するために使用したscikit-learnのバージョンと、ピクルスを解除しようとしているバージョンを教えてください。 0.22.1で酸洗いしながら0.21.3で酸洗いを解除しようとしていると思います
その後、更新するだけです。 ただし、異なるscikit-learnバージョン間でのピクルス化/ピクルス解除はサポートされていないため、バグとは見なされないことに注意してください。

それがまさに問題でした。 新しいノートブックを別のカーネルで起動したことに気づきませんでした。 迅速な対応ありがとうございました。

このページは役に立ちましたか?
0 / 5 - 0 評価