Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-335

Two-Feature Partial Dependence

Problem

Compute the two-feature partial dependence of a linear model. For every grid pair, replace features f1 and f2 with the grid values, keep the other features, and average the prediction over all rows. Return a list of lists indexed by grid1 then grid2; empty X or an empty grid returns [].

Hint

Partial dependence marginalizes the model over the other features while sweeping the chosen pair.

Test cases

case 1
in: [[[1,2]],[1,1],0,1,[0,10],[1,2]]
expected: [[1,2],[11,12]]
case 2
in: [[[1,2],[3,4]],[0.5,2],0,1,[0,1],[0,2]]
expected: [[0,4],[0.5,4.5]]
case 3
in: [[],[1,1],0,1,[0],[0]]
expected: []
case 4
in: [[[1,2]],[1,1],0,1,[],[0]]
expected: []
Starter codePython
def partial_dependence_2d(X, w, f1, f2, grid1, grid2):
    # Your code here
    pass

4 test cases · run your code against them in the editor