異なる型にColumnTransformerを使う
列グループごとに異なる前処理を行う
「異なる型にColumnTransformerを使う」はCoddyKit上の無料Data Science Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはData Science Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Data Science Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Columns Are Not Equal
Real tables mix numbers and categories, and each kind needs different prep. One scaler for everything simply will not work. 🧩
Meet ColumnTransformer
A ColumnTransformer applies different transformers to different column groups, all in one step you can drop inside a pipeline.
List Your Column Groups
First decide which columns are numeric and which are categorical. You will hand each group its own transformer by name.
num_cols = ['age', 'income']
cat_cols = ['city', 'plan']Scale the Numbers
Numeric columns usually want a StandardScaler so large and small values share a fair footing before modeling.
from sklearn.preprocessing import StandardScalerEncode the Categories
Categorical columns need numbers, so reach for OneHotEncoder to turn each category into its own 0/1 column.
from sklearn.preprocessing import OneHotEncoderWire It Together
Each entry is a tuple of name, transformer, and the columns it touches. The ColumnTransformer runs them side by side.
from sklearn.compose import ColumnTransformer
prep = ColumnTransformer([('num', StandardScaler(), num_cols), ('cat', OneHotEncoder(), cat_cols)])Slot It Into a Pipeline
Place the transformer as the first step and a model as the last. Now mixed-type prep and modeling live in one pipeline.
pipe = Pipeline([('prep', prep), ('model', LogisticRegression())])Leftover Columns
Columns you did not list are dropped by default. Set remainder to passthrough if you want to keep the rest untouched.
ColumnTransformer([...], remainder='passthrough')Handle Unseen Categories
Test data may contain a category training never saw. Set handle_unknown to ignore so the encoder stays calm instead of crashing.
OneHotEncoder(handle_unknown='ignore')Select Columns by Type
Tired of typing column names? make_column_selector grabs columns by dtype so your prep adapts as the table changes.
from sklearn.compose import make_column_selector as selectorOne Fit, Both Paths
Calling fit once trains the scaler and the encoder together on the right columns. Each path learns only from its own columns.
Quick Check
You have numeric and categorical columns needing different prep. What handles that?
Recap
You can now route numeric and categorical columns to their own transformers with a ColumnTransformer inside one pipeline. Next, tuning. 🔧
よくある質問
「異なる型にColumnTransformerを使う」レッスンは無料ですか?
はい。「異なる型にColumnTransformerを使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Data Science Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Data Science Academyコースには全4レッスンが含まれています。
「異なる型にColumnTransformerを使う」で何を学びますか?
列グループごとに異なる前処理を行う ブラウザで直接実行するハンズオンコードでData Science Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Data Science Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのData Science Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「異なる型にColumnTransformerを使う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このData Science Academyレッスンでコードを書いて実行できますか?
はい。すべてのData Science Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 手作業の手順よりパイプラインを使う理由
- 異なる型にColumnTransformerを使う
- GridSearchCVでチューニングする
- 学習済みパイプラインを保存して再読み込みする