Which Column Is the Model Leaning On?

Explainable AIMedium+20 XP+20

Which Column Is the Model Leaning On?

A loan model is already trained, and retraining it once per feature would take days. Permutation importance questions the model you already have instead: break one column of the held-out data by shuffling it, re-score the same model, and measure how far the score falls.

So that everyone gets the same answer, the shuffles are handed to you rather than drawn at random. A shuffle is a list order of row indices. Applying it to column j means:

after the shuffle, row i holds the column-j value that originally belonged to row order[i].

For example, order = [2, 0, 1] turns a column [a, b, c] into [c, a, b]. Every other column stays exactly where it was.

One shuffle can be lucky, so each column is shuffled with each order in turn, separately, and the drops are averaged.

Task: write permutation_importance(predict, X, y, shuffles).

  • predict(row) is the trained model: it takes one row (a list of feature values) and returns a predicted label.
  • X is the list of held-out rows, y their true labels, and shuffles a list of orders.
  • Accuracy is the fraction of rows whose prediction equals the true label.
  • For every column j and every order: start from the original held-out rows, shuffle column j only, compute the accuracy, and record
drop=baseline accuracy−shuffled accuracy\text{drop} = \text{baseline accuracy} - \text{shuffled accuracy}drop=baseline accuracy−shuffled accuracy
  • The importance of column j is the average of its drops. Report it as it comes out, whatever its sign.

Return a list with one importance per column, in column order, each rounded to 4 decimal places.

Trained model: not retrained11 held-out applicants, never used in trainingRepaid? is the right answer. R = repaid, D = didn't repay.
accuracy
91%
10 of 11 right
NameCredit ScoreIncome ($k)Zip CodeRepaid?Model's guess
Ana78078062623041230412RR
Ben54054028283097730977DD
Cara69069045453011830118RR
Dev61061035353084530845DD
Eli73573530303065530655RR
Fay58058070703050330503RD ✗
Gus65065024243076230762DD
Hana72072051513032930329RR
Ivo56056040403023130231DD
Jin66066038383090630906DD
Kit76076044443018730187RR
average66242.5
drop when shuffled (points)
Credit Score
?
Income
?
Zip Code
?

The trained model gets 10 of 11 held-out applicants right. Only Fay is missed. Shuffle one column, then score the same model again.

Examples

predict = lambda r: 1 if r[0] >= 650 and r[1] >= 40 else 0
X = [[720, 55, 3], [610, 80, 1], [680, 35, 2], [700, 62, 3], [590, 30, 1], [660, 48, 2]]
y = [1, 0, 1, 1, 0, 1]
shuffles = [[4, 3, 1, 5, 0, 2], [1, 5, 0, 4, 3, 2]]
returns [0.4167, 0.1667, 0.0]
predict = lambda r: 'late' if r[0] + r[1] > 10 else 'on time'
X = [[2, 3], [8, 4], [5, 7], [1, 1], [9, 6], [4, 2], [6, 6], [3, 9]]
y = ['on time', 'late', 'late', 'on time', 'late', 'on time', 'late', 'on time']
shuffles = [[7, 0, 1, 2, 3, 4, 5, 6], [2, 6, 4, 0, 7, 1, 3, 5]]
returns [0.4375, 0.1875]

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