C2 W4 Giving String Format Errors

The above image shows it all. I’m pretty sure this is not an error on my part, as it gets the sample tests just fine.

Ok so my code was wrong, but I don’t understand why. Here is what I was doing

left_indices.extend(np.where(X[node_indices, feature]==1)[0].tolist())
right_indices.extend(np.where(X[node_indices, feature]==0)[0].tolist())

Anyone know why that doesn’t work?


I did some looking into the code in question and found that it fails when the node_indices are not a continuous Range increasing by 1 and starting at zero. This is because it is returning the indices of node_index where the condition is true and not the value at that index.

See below:

A solution in the same style can be done using list comprehensions if you wish to avoid using a for loop. I’ll let you find that solution.

I submitted a report for this question as I think there are two problems with it. The first is that the error above is caused by the f-string not being formatted correctly. This line:

f"Wrong value when target is at index 0. \nExpected: {expected} \ngot: \{left:{left}, 'right': {right}\}"

Should actually be this, where instead of escaping the braces you need to use double braces:

f"Wrong value when target is at index 0. \nExpected: {expected} \ngot: {{left:{left}, 'right': {right}}}"

Secondly, I think the expected output is simply incorrect. Looking at the test, the feature being split on is:

X_t = np.array([[0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0]])

Since the active indices are [1, 2, 3, 6, 7, 9, 10], the above would reduce down to:

X_t = np.array([[1, 0, 1, 1, 0, 0, 0]])

The indices of the left (values of 1) are then [0, 2, 3] and the right (values of 0) are [1, 4, 5, 6].

Am I missing something else here?

Hello @brideau!

Agreed, and I reported to the team about this.

The indices you need to return are the node indices (among [1, 2, 3, 6, 7, 9, 10]), so they should be [1, 3, 6] instead for the left indices.