LoginSignup
23
16

More than 3 years have passed since last update.

写経中に遭遇したscikit-learnのワーニングに対応してみた

Posted at

概要

あたらしいPythonによるデータ分析の教科書を写経中に遭遇したワーニングへ対応したことに関する忘備録。

環境

  • Windows 10 Pro
  • Python 3.7.3

ことの始まり

  1. 案件が無いので会社を休業になる
  2. 何か勉強しようとPythonと機械学習に挑戦(2019/1~)
  3. とりあえず目についた入門書を購入
  4. 以下のコマンドで写経に必要なライブラリをインストールしてJupyter Notebook上で進める
pip install numpy
pip install pandas
pip install matplotlib
pip install scikit-learn
pip install jupyter

ワーニングの出た根本的原因

本書に記載されたサンプルデータ一式をダウンロードし、requirements.txtを見てみると、以下の記載がある。

scikit-learn==0.19.1

そして現在(2019/05)インストールしたバージョンをチェックすると、以下の通りとなる。

scikit-learn==0.21.2

遭遇したワーニングとその対応

4.4 scikit-learn

欠損値の補完

from sklearn.preprocessing import Imputer
# 平均値で欠損値を補完するためのインスタンスを作成する
imp = Imputer(strategy = 'mean', axis = 0)
# 欠損値を補完
imp.fit(df)
imp.transform(df)

参考書通りに写経して実行すると

DeprecationWarning: Class Imputer is deprecated; Imputer was deprecated in version 0.20 and will be removed in 0.22. Import impute.SimpleImputer from sklearn instead.
warnings.warn(msg, category=DeprecationWarning)

のワーニングが表示される。
意味としては、「Imputerクラスは0.20で廃止予定となっていて、0.22で削除されます。SimpleInputerクラスを使用してください」ということ。

ワーニングにしたがって、SimpleImputerクラスを使うと、ワーニングは表示されない

from sklearn.impute import SimpleImputer

# 平均値で欠損値を補完するためのインスタンスを作成する
imp = SimpleImputer(strategy = 'mean')
# 欠損値を補完
imp.fit(df)
imp.transform(df)

sklearn.preprocessing.Inputerクラス

~0.19までで欠損値補完に使用するクラス。

  • strategyキーワード引数:文字列をとり、欠損値の補完方法を指定する。デフォルトは"mean"

    • "mean" :平均値
    • "median":中央値
    • "most_frequent":最頻値
  • axisキーワード引数:数値をとり、補完の方向を指定する。デフォルトは0

    • 0:列方向
    • 1:行方向

sklearn.impute.SimpleImputerクラス

0.20以降で欠損値補完に使用するクラス。

  • strategyキーワード引数:文字列をとり、欠損値の補完方法を指定する。デフォルトは"mean"
    • "mean":平均値
    • "median":中央値
    • "most_frequent":最頻値
    • "constant":fill_valueキーワード引数で指定した値(fill_valueキーワード引数を指定しない場合はデフォルトのNone

axisキーワード引数は存在せず、列方向の補完のみを行う

One-hotエンコーディング

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

# DataFrameをコピー
df_ohe = df.copy()

# ラベルエンコーダのインスタンス化
le = LabelEncoder()
# 英語のa,b,cを1,2,3に変換
df_ohe['B'] = le.fit_transform(df_ohe['B'])

# One-hotエンコーダのインスタンス化
ohe = OneHotEncoder(categorical_features = [1])
# One-hotエンコーディング
ohe.fit_transform(df_ohe).toarray()

参考書通りに写経して実行すると

FutureWarning: The handling of integer data will change in version 0.22. Currently, the categories are determined based on the range [0, max(values)], while in the future they will be determined based on the unique values.
If you want the future behaviour and silence this warning, you can specify "categories='auto'".
In case you used a LabelEncoder before this OneHotEncoder to convert the categories to integers, then you can now use the OneHotEncoder directly.
  warnings.warn(msg, FutureWarning)

DeprecationWarning: The 'categorical_features' keyword is deprecated in version 0.20 and will be removed in 0.22. You can use the ColumnTransformer instead.
  "use the ColumnTransformer instead.", DeprecationWarning)

のワーニングが表示される。
全体の意味としてはcategorical_featuresキーワードは0.20で廃止予定となっていて、0.22で削除されます。ColumnTransformerを使用してください」ということらしい。

ワーニングに従って、ColumnTransformerを使用すると、ワーニングは表示されない。
scikit-learn公式:ColumnTransformerのUsers Guide

from sklearn.compose import ColumnTransformer
from sklearn.feature_extraction.text import CountVectorizer

# DataFrameをコピー
df_ohe = df.copy()
print(df_ohe)

# ColumnTransformerのインスタンス化
ct = ColumnTransformer([('B_x', CountVectorizer(analyzer=lambda x: [x]), 'B')],
    remainder = 'passthrough')
ct.fit_transform(df_ohe)

sklearn.preprocessing.OneHotEncoderクラス

~0.19までで、One-Hotエンコードを行うためのクラス。

  • categorical_featuresキーワード引数:どの列をOne-Hotエンコードの対象とするかを指定する
    0.20で廃止予定、0.22で削除予定

sklearn.compose.ColumnTransformerクラス

DataFrameの列の変更を行うためのクラス。

  • 第1引数:リストで[変換後の列名パターン名称, 列の変換方法, 対象となる列]を指定する。
  • remainderキーワード引数:文字列をとり、変更しない列の戻り値での取り扱いを指定する。デフォルトはdrop
    • drop:変更しない列を取り除いた配列を戻り値とする
    • passthrough:変更しない列を含めた配列を戻り値とする

sklearn.feature_extraction.text.CountVectorizerクラス

文章から単語または文字の出現の有無をマトリックス化するためのクラス。

  • analyzerキーワード引数:文字列または呼び出し可能オブジェクトをとり、N-gramを文字によって行うか、単語によって行うか、あるいはその他の方法で行うかを指定する。
    • word:単語を用いたN-gramを使用する。
    • char:文字を用いたN-gramを使用する。
    • char_wb:文章内の文字のみを用いたN-gramを使用する。
    • 呼び出し可能オブジェクト:文章を受け取り、抽出結果を返す。
23
16
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
23
16