Decision Tree lab - split_dataset unit test error

Hello
In this lab C2_W4_Decision_Tree_with_Markdown I am working on the split_dataset function.
I get output (left and right indices arrays) the same as expected, the picture looks correct, too, but when the Unit Test split_dataset_test(split_dataset) runs, I get the following error:
ValueError: operands could not be broadcast together with shapes (7,) (4,)

I am not sure where to look to figure out this error?
thank you!

Hello @Svetlana_Verthein,

Please post the full error Traceback here. Thank you!

Raymond

Here you go


ValueError Traceback (most recent call last)
in
14
15 # UNIT TESTS
—> 16 split_dataset_test(split_dataset)

~/work/public_tests.py in split_dataset_test(target)
70 ‘right’: np.array([2, 7, 9, 10])}
71
—> 72 assert np.allclose(right, expected[‘right’]) and np.allclose(left, expected[‘left’]), f"Wrong value when target is at index 0. \nExpected: {expected} \ngot: {left:{left}, ‘right’: {right}}"
73
74

<array_function internals> in allclose(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in allclose(a, b, rtol, atol, equal_nan)
2247
2248 “”"
→ 2249 res = all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
2250 return bool(res)
2251

<array_function internals> in isclose(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
2356 yfin = isfinite(y)
2357 if all(xfin) and all(yfin):
→ 2358 return within_tol(x, y, atol, rtol)
2359 else:
2360 finite = xfin & yfin

/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in within_tol(x, y, atol, rtol)
2337 def within_tol(x, y, atol, rtol):
2338 with errstate(invalid=‘ignore’):
→ 2339 return less_equal(abs(x-y), atol + rtol * abs(y))
2340
2341 x = asanyarray(a)

ValueError: operands could not be broadcast together with shapes (7,) (4,)

So, the problem comes from this line

—> 72 assert np.allclose(right, expected[‘right’]) and np.allclose(left, expected[‘left’])

This is part of a public test that attempts to compare the left_indices and right_indices returned from your solution to the expected answers. According to the test (which you can also check out yourself, by clicking “File” > “Open” > “public_tests.py”), it expects these

    expected = {'left': np.array([1, 3, 6]),
                'right': np.array([2, 7, 9, 10])}

According to the error:

ValueError: operands could not be broadcast together with shapes (7,) (4,)

apparently your right_indices has 7 elements which is more than the expected 4 elements.

This is the test case:

   left, right = target(X, [1, 2, 3, 6, 7, 9, 10], 3)

where target is just the name of the function under test, which is your split_dataset.

You need to make sure your solution can pass this test case, not just all those that have been passed.

Good luck,
Raymond

1 Like

I got it now, I was only splitting at the root node rather than at any node.
What confused me was the “Expected Output” which matched mine, so I thought my code worked fine.
thank you again, Raymond, for a clear and thorough explanation!

Grear job figuring the problem out, @Svetlana_Verthein!

Raymond