A Junction for Columns

Data ProcessingMedium+20 XP+20

A Junction for Columns

In Different Columns, Different Steps you gave ColumnTransformer a list of lines, each one (name, what to do, which columns). It sent each group of columns its own way and joined the pieces back into one wide table of numbers, with names that remember the route each column took. Here you build that junction yourself, in plain Python.

Task: write column_transform(table, routes, remainder="drop"). It learns every step from table and transforms table in one go, as fit_transform does, and returns a tuple (names, rows).

The table. table is a dict from column name to that column's list of values, like the dict you hand to pd.DataFrame. Its keys are in the table's column order, and every list has the same length.

The routes. routes is a list of (name, method, columns) tuples, where columns is a list of column names and method is one of:

methodlearns from the columnoutput
"standard"the average μ\muμ and the spread σ\sigmaσ (divide by nnn, as StandardScaler does)one column: each xxx becomes (x−μ)/σ(x - \mu) / \sigma(x−μ)/σ
"minmax"the smallest value mmm and the largest value MMMone column: each xxx becomes (x−m)/(M−m)(x - m) / (M - m)(x−m)/(M−m)
"onehot"the different values in the column, sorted alphabeticallyone column per value, holding 1.0 in rows with that value and 0.0 elsewhere

The remainder. A column that no route names is the remainder. remainder="drop" (the default) throws it away, "passthrough" copies it through unchanged, and a method name sends every remainder column through that method.

The output.

  • Output columns come route by route, in the order of routes. Within a route they follow the order of that route's columns list, which need not be the table's order. Remainder columns come last, in the table's order.
  • Names record the route: route__column for a scaled or passed-through column, route__column_value for a one-hot column. Remainder columns use remainder as their route name.
  • A column may be named by more than one route; each route uses it on its own. Only a column that no route names is part of the remainder.
  • rows has one list per table row, and every value in it is a float rounded to 4 decimal places.

In every test, a column sent to "standard" or "minmax" holds numbers that are not all equal, and a column passed through untouched holds numbers. The first visible test is the lesson's fruit table.

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']}
routes = [('numbers', 'standard', ['weight', 'sweet']), ('words', 'onehot', ['colour'])]
returns (['numbers__weight', 'numbers__sweet', 'words__colour_red', 'words__colour_yellow'], [[0.4472, 0.7845, 1.0, 0.0], [1.3416, 1.1767, 1.0, 0.0], [-0.4472, -0.7845, 0.0, 1.0], [-1.3416, -1.1767, 0.0, 1.0]])
table = {'rooms': [2, 3, 5, 4], 'district': ['north', 'east', 'north', 'south'], 'age': [30, 5, 12, 50], 'garden': [0, 1, 1, 0]}
routes = [('size', 'minmax', ['age', 'rooms']), ('area', 'onehot', ['district'])]
remainder = 'passthrough'
returns (['size__age', 'size__rooms', 'area__district_east', 'area__district_north', 'area__district_south', 'remainder__garden'], [[0.5556, 0.0, 0.0, 1.0, 0.0, 0.0], [0.0, 0.3333, 1.0, 0.0, 0.0, 1.0], [0.1556, 1.0, 0.0, 1.0, 0.0, 1.0], [1.0, 0.6667, 0.0, 0.0, 1.0, 0.0]])

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