Hire One Feature per Round

Feature EngineeringMedium+20 XP+20

Hire One Feature per Round

A team is deciding which columns to feed their model, and they have already done the expensive part: for every subset of the candidate features, a real model was trained and its validation accuracy recorded. Your job is to run forward selection over that table.

  1. Start with no features. The score to beat is scores[()], the validation accuracy of a model given no features at all.
  2. Each round, try adding every feature that has not been chosen yet, one at a time, and look up the score of the current set plus that one feature.
  3. The round's winner is the candidate whose set scores highest. If two candidates tie, the one that comes first in features wins.
  4. If the winner's score is strictly higher than the current score, add that feature and start the next round. Otherwise stop, because no single addition helps any more.

scores is a dict whose keys are tuples of feature names, always listed in the order they appear in features. With features = ["Income", "Age", "Education"], the set {Education, Income} is stored under ("Income", "Education") and the empty set under (). Every subset is in the table.

Task: write forward_selection(features, scores) and return a tuple (chosen, score). chosen lists the selected features in the order they were added, and score is the final validation accuracy rounded to 4 decimal places.

Building Up or Cutting Down

Two ways to search step by step instead of trying everything.

No Features
Accuracy50%

Forward Selection starts empty and adds whichever feature helps most at each step.

Examples

features = ['Income', 'Age', 'Occupation', 'Education']
scores = {(): 0.6, ('Income',): 0.74, ('Age',): 0.66, ('Occupation',): 0.64, ('Education',): 0.65, ('Income', 'Age'): 0.75, ('Income', 'Occupation'): 0.76, ('Income', 'Education'): 0.77, ('Age', 'Occupation'): 0.68, ('Age', 'Education'): 0.69, ('Occupation', 'Education'): 0.78, ('Income', 'Age', 'Occupation'): 0.78, ('Income', 'Age', 'Education'): 0.79, ('Income', 'Occupation', 'Education'): 0.84, ('Age', 'Occupation', 'Education'): 0.8, ('Income', 'Age', 'Occupation', 'Education'): 0.83}
returns (['Income', 'Education', 'Occupation'], 0.84)
features = ['Distance', 'Weather', 'Weekday']
scores = {(): 0.55, ('Distance',): 0.7, ('Weather',): 0.62, ('Weekday',): 0.58, ('Distance', 'Weather'): 0.73, ('Distance', 'Weekday'): 0.7, ('Weather', 'Weekday'): 0.63, ('Distance', 'Weather', 'Weekday'): 0.73}
returns (['Distance', 'Weather'], 0.73)

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