LoginSignup
3
0

More than 3 years have passed since last update.

ローディングしたStandardScaler で transform するとエラーになる

Last updated at Posted at 2020-05-30

前提

Machine Learningでは、学習の前処理としてデータを標準化します。
よく使われるのが、StandardScalar。
これは、学習時に利用したStandardScalarを保存し、本番データの予測時にも使います。

学習時は以下のような感じ

train.py
import pickle
from sklearn.preprocessing import StandardScaler

sc = StandardScalar()
sc.fit(df)
sc.transform(df)
pickle.dump(sc, open("./scaler/scaler.p", "wb"))

予測時は以下のような感じで、トレーニング時の標準化と揃える。

predict.py
sc = pickle.load(open('./scaler/scaler.p', "rb"))
_df = sc.transform(df)

問題点

上記のプログラムを実行すると下記のようなエラーが発生。

RuntimeError: The reset parameter is False but there is no n_features_in_ attribute. Is this estimator fitted?

なんだこりゃ。ググったなかで唯一ヒットしたのが以下。

Workaround: I was using scikit-learn version 0.23.0. Going back to version 0.22.0 solved this for me
(0.23.0だとうまくいかないから、0.22.0使ってみな)

なるほど。
というわけで uninstall して、0.22をインストール

pip uninstall scikit-learn
pip install scikit-learn=0.22.0

結果、これでうまくいきました。
一過性のバグだとは思いますが、情報が少ないので残しておこうと思います。

3
0
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
3
0