When get_dummies Shrinks

Machine Learning in CodeMedium+10 XP+10

When get_dummies Shrinks

pd.get_dummies is handy, but it learns nothing. It makes one column for each category in the rows you hand it. A fitted OneHotEncoder always makes the columns it learned during fit.

Task: write column_counts(train_values, batch_values).

  • Both lists are values of one category column called "size".
  • Count the columns that pd.get_dummies makes from just batch_values (as a one-column DataFrame).
  • Fit a OneHotEncoder(handle_unknown="ignore") on train_values, transform batch_values, and count the columns it gives.
  • Return [get_dummies column count, encoder column count].

Examples

train_values = ["S", "M", "L", "M"]
batch_values = ["M", "M", "S"]
returns [2, 3]
train_values = ["S", "M", "L"]
batch_values = ["XL", "S"]
returns [2, 3]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run