Hello

I am getting this assertion error in computy entropy function. I dont understand the reason.

Any help much appreciated.

```
Entropy at root node: 1.0
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-20-05098bb5d0e3> in <module>
5
6 # UNIT TESTS
----> 7 compute_entropy_test(compute_entropy)
~/work/public_tests.py in compute_entropy_test(target)
5 result = target(y)
6
----> 7 assert result == 0, "Entropy must be 0 with array of ones"
8
9 y = np.array([0] * 10)
AssertionError: Entropy must be 0 with array of ones
```

Regards

Hey @arunkumar,

Welcome to the community. This means that your implementation of `compute_entropy`

is incorrect. In the markdown cell just before this code cell, the formulation of entropy is mentioned. If we pass an array of ones and try to calculate the entropy using the mentioned formulation, you will find that p_1 = 1, and hence

H(p_1) = - 1 * log_2(1) - (1 - 1) * log_2(1 - 1) \\
H(p_1) = 0

Try to print the value of entropy that your implementation is giving as the output, and you will see that it isnβt 0. For correcting your implementation, if you unable to figure out the error, you can check out the hints mentioned just next to the code cell. I hope this helps.

Cheers,

Elemento

1 Like

Thanks @Elemento for the answer. I understood. Got it working.

Regards

If the array is a list of ones, how can I calculate y[y == 1] as mentioned in the hint? For me instead of an array, the output is a single number β1β.

Hey @Benjamin_Martini,

When we do `[y == 1]`

, this returns us an array of True or False. Now, when we are writing `y[ y == 1]`

, we are essentially indexing the array `y`

, and we get all the values at the indices of the **True** values. This is more clearly understood with the help of an example.

```
y = np.array([4, 5, 0, 1, 0])
print(y >= 1)
print(y[y >= 1])
print(len(y[y >= 1]))
```

It gives the following output:

```
[ True True False True False]
[4 5 1]
3
```

So, if you are getting a single number, by any chance, are you referring to the output of `len`

? If not, can you please DM your code to me, the output of which you are getting as a single number. I hope this helps.

Cheers,

Elemento

Hi @Elemento ,

thank you a lot for your prompt reply. I found my mistake. In my training example I wanted to create a new array y_ones but I used the wrong syntax:

With the correct syntax for an array y_ones = np.array() it worked.

Thank you for your help!

Cheers

Benjamin