A District Nobody Saw

Machine Learning in CodeMedium+10 XP+10

A District Nobody Saw

A house-price model target-encodes the district column. Some new houses are in districts the encoder never saw during training.

Task: write encode_districts(train_districts, train_prices, new_districts, smooth).

  • Put the districts in one-column DataFrames with the column name "district". The prices are a list of numbers, one per training house.
  • Fit a TargetEncoder(smooth=smooth, target_type="continuous") on the training districts and prices.
  • Transform new_districts with it. A district the encoder never saw gets the overall average price of the training houses.
  • Return the encoded values as a list, each rounded to 1 decimal place.

Examples

train_districts = ["north", "south", "north", "east", "south", "north"]
train_prices = [250, 400, 270, 180, 420, 260]
new_districts = ["south", "west", "east"]
smooth = 0
returns [410.0, 296.7, 180.0]
train_districts = ["north", "south", "north", "east", "south", "north"]
train_prices = [250, 400, 270, 180, 420, 260]
new_districts = ["east", "north"]
smooth = 2.0
returns [257.8, 274.7]

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