C2_W4_Exercise 2

Hi - There are couple of issues I’m facing -

  1. Exercise 2 - # UNIT TESTS
    split_dataset_test(split_dataset) - Stuck in an ValueError: operands could not be broadcast together with shapes (3,) (4,) )

  2. Exercise 4 - # UNIT TESTS
    get_best_split_test(get_best_split) - Stuck in an AssertionError: When the target variable is pure, there is no best split to do. Expected -1, got 0

Detailed Issue #1 error

  1. When running the cell that has the below code, I’m getting the below error( ValueError: operands could not be broadcast together with shapes (3,) (4,) ).

UNIT TESTS

split_dataset_test(split_dataset)


ValueError Traceback (most recent call last)
in
11
12 # UNIT TESTS
—> 13 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 (3,) (4,)

  1. Details of issue #2 error

AssertionError Traceback (most recent call last)
in
3
4 # UNIT TESTS
----> 5 get_best_split_test(get_best_split)

~/work/public_tests.py in get_best_split_test(target)
120 result = target(X, y, node_indexes)
121
→ 122 assert result == -1, f"When the target variable is pure, there is no best split to do. Expected -1, got {result}"
123
124 y = X[:,0]

AssertionError: When the target variable is pure, there is no best split to do. Expected -1, got 0

Hello @Shan_Dhandapani,

For 1, are you iterating through the elements in node_indices?
For 2, the provided default best_feature value is -1. Did you change it? best_feature is only updated when there is a higher info gain, did you implement “higher” or “higher or equal to”?

Raymond

Hi -
for 1 - Yes, going through the elements in node_indices to find whether the value is 1 or 0
For 2 - using “higher” not “higher or equal to”

Thanks,
Shan

Learner solved the problem in Exercise 2 by properly iterating through the elements of node_indices.
Learner solved the problem in Exercise 4 by properly initializing max_info_gain.