Start learning free

LESSON 61 OF 165 · MACHINE LEARNING: DOING IT RIGHT

Different Columns, Different Steps

Practice

A model only does math with numbers, but city and newsletter hold words. pd.get_dummies(table, columns=[...]) turns each word column into 0/1 columns, one for each value it finds in the table you give it. That is the trap: the training rows have 7 different city values and give 14 columns, while two new customers, from Paris and Cluj, give only 9. A model fitted on 14 columns can't use 9.

A ColumnTransformer gives each group of columns its own step and puts the results side by side in one table of numbers. Each step is a tuple: a name you choose, what to do, and which columns. A OneHotEncoder makes the 0/1 columns, but it remembers the values it saw while fitting, so new rows always get the same columns. With handle_unknown="ignore", a value it never saw gets 0 in all of them instead of an error.

prep = ColumnTransformer([
    ("money", StandardScaler(), ["unit_price", "quantity"]),
    ("kind", OneHotEncoder(handle_unknown="ignore"), ["category"]),
])
table = prep.fit_transform(orders)
print(table.shape[1], "columns")

fit_transform learns from the rows and gives back the prepared table; transform prepares more rows with what was already learned; .shape[1] is how many columns a table has. Put the ColumnTransformer first in a pipeline and the pipeline takes the raw table, words and all. Your script must print:

get_dummies, training rows: 14 columns
get_dummies, new customers: 9 columns
ColumnTransformer, new customers: 14 columns
New customers: [1 0]

Your task: build prepare, a ColumnTransformer that scales the numbers columns and one-hot encodes the words columns with handle_unknown="ignore". Join it with LogisticRegression(max_iter=2000) in pipeline, fit it on the training rows, then print how many columns prepare gives the new customers and the pipeline's predictions for them.


Assignment
Follow the Coding Tutorial to build the ColumnTransformer, put it in a pipeline and predict for the two new customers.


Hint
Fitting the pipeline fits prepare too, so prepare.transform(new_customers) works only after pipeline.fit. Paris never appears in the training rows, which is why the encoder needs handle_unknown="ignore".


STEP 1 OF 7
    Ctrl + Enter to check