Where Did Each Column Go?

Machine Learning in CodeEasy+10 XP+10

Where Did Each Column Go?

A ColumnTransformer sends different columns down different routes, and the output names remember each route.

Task: write prepared_names(table, number_columns, word_columns).

  • table is a dict mapping column names to lists. number_columns and word_columns are lists of column names in it.
  • Build a ColumnTransformer with two lines, in this order:
    • ("numbers", StandardScaler(), number_columns)
    • ("words", OneHotEncoder(handle_unknown="ignore"), word_columns)
  • Fit it on the table and return get_feature_names_out() as a list of strings.

Examples

table = {"weight": [150.0, 170.0, 130.0, 110.0], "sweet": [7.0, 8.0, 3.0, 2.0], "colour": ["red", "red", "yellow", "yellow"]}
number_columns = ["weight", "sweet"]
word_columns = ["colour"]
returns ['numbers__weight', 'numbers__sweet', 'words__colour_red', 'words__colour_yellow']
table = {"city": ["Oslo", "Rome", "Oslo"], "rooms": [2, 3, 4], "type": ["flat", "house", "flat"]}
number_columns = ["rooms"]
word_columns = ["city", "type"]
returns ['numbers__rooms', 'words__city_Oslo', 'words__city_Rome', 'words__type_flat', 'words__type_house']

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