C2_W4_Decision_Tree_with_Markdown

I am not sure why I am getting this error, and what it means. When I run the test, i get the expected results, and the additional error.

I was working on the split_dataset(X, node_indices, feature) function.

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

Hi @gaolatlhe !

It seems that numpy tried to do an operation on arrays that were not compatible in size.

1: What is broadcasting?
Broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations.

2: What are the conditions for Broadcasting?
Two dimensions are compatible when:
1: They are equal, or
2: one of the dimensions is 1

In your example you have a dimension that is 7 and 4 which is incompatible.

For more information on broadcasting see: Broadcasting — NumPy v1.24 Manual

Hope this helps!

thanks for the explanation on broadcasting.

I had tried to create the function using enumerate and it did not work and I am still not clear why it did not work. I understand the shapes did not match but i cant visualise why the shapes differed. I ended up working my head out using the hint format using for i in node_indices.

I am still not clear how the 2 approaches differ and why the other does not work while the other does?

@gaolatlhe, so you have a working version and a non-working version of the code? If so, you might send both of them to me via a private message (click my profile, and hit Message), and I can take a look and try to explain.

Cheers,
Raymond

1 Like

Hi @gaolatlhe

It is possible to make the code work with enumerate.

Consider the two methods:

for i in node_indices:

# and

for idx, val in enumerate(node_indices):

what is i?
what is idx and val?

which one of idx or val is the same as i?

1 Like