Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ パイプラインによる効率的なデータ前処理 | パイプライン
Pythonによる機械学習入門

bookパイプラインによる効率的なデータ前処理

メニューを表示するにはスワイプしてください

make_column_transformer 関数を使用して列ごとに個別に変換できるようになったら、次のステップはパイプラインの構築です。パイプラインは、前処理の手順を整理し、それらを順番に適用するコンテナです。

Scikit-learn でのパイプラインは、Pipeline モジュールの make_pipeline クラスコンストラクタまたは sklearn.pipeline 関数のいずれかを使って作成できます。本コースでは、より簡単に適用できる make_pipeline に焦点を当てます。

すべての変換器を関数の引数として渡すだけで、パイプラインを作成できます。これだけで十分に簡単です。

ただし、.fit_transform(X) オブジェクトで Pipeline メソッドを呼び出すと、パイプライン内のすべての変換器に対して .fit_transform(X) が適用されます。そのため、特定の列を異なる方法で処理したい場合は、ColumnTransformer を使用し、それを make_pipeline() に渡す必要があります。

前の章と同じファイルを使用してパイプラインを構築。パイプラインには、SimpleImputerに加えてカテゴリカル特徴量用のエンコーダを含めること。データセットには名義特徴量と順序特徴量の両方が含まれているため、ColumnTransformerを使って個別に処理すること。

1234567891011121314151617
import pandas as pd from sklearn.compose import make_column_transformer from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder from sklearn.impute import SimpleImputer from sklearn.pipeline import make_pipeline df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/exams.csv') # Making a column transformer edu_categories = ['high school', 'some high school', 'some college', "associate's degree", "bachelor's degree", "master's degree"] ct = make_column_transformer( (OrdinalEncoder(categories=[edu_categories]), ['parental level of education']), (OneHotEncoder(), ['gender', 'race/ethnicity', 'lunch', 'test preparation course']), remainder='passthrough' ) # Making a Pipeline pipe = make_pipeline(ct, SimpleImputer(strategy='most_frequent')) print(pipe.fit_transform(df))
copy
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  3
some-alt