What is wrong with this syntax?

Greetings fellow learners, it is requested to explain what is incorrect about this syntax?

See one line before, and count the number of parenthesis … :slight_smile:

@anon57530071 has it right, but here is an under appreciated subtlety from the python doc on syntax errors…

The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected. The error is caused by (or at least detected at) the token preceding the arrow

(Emphasis in the original)

What that means is the parser is complaining already at the token left_entropy. The normal assumption is that the error is occurring in the expression following, but that is not the case. The arrow at the initial token of the line indicates that the syntax error is on the preceding one. Hope this helps.


Look why is this error coming:

The error suggests that the length of “y_left” is zero.

Curious that line 23 is protected against 0 lengths but line 24 is not. Is that toolkit/ deeplearning provided code? Maybe its an indentation mistake and both should be?

Indeed it is curious.

The issue appears to be in the student’s compute_entropy() function.

They appear to be following the guidance in the third note of section 4.1, but maybe is not fully correctly.

I think you re-wrote your code to use compute_entropy(). That’s good step.

And, you may be better to double check what @ai_curious advised. Python code block is recognized by an interpreter with using “indentation”. I believe you corrected it, but this is not the right way for Python. (I intentionally cut off the right-hand side, to avoid pasting code itself.)

Then, as your next step, you may want to revisit your implementation of compute_entropy();
As “hints” describes, the most important thing is to avoid error conditions like “inf”, “nan”, etc.
The hint is simple enough to check the length at first. This kind of “possible error removal” before getting int the deep logic is important. Please double check your implementation in compute_entropy().

    ### START CODE HERE ###
    if len(y) != 0:
        # Your code here to calculate the fraction of edible examples (i.e with value = 1 in y)
        p1 =

        # For p1 = 0 and 1, set the entropy to 0 (to handle 0log0)
        if p1 != 0 and p1 != 1:
            # Your code here to calculate the entropy using the formula provided above
            entropy = 
            entropy = 0. 
    ### END CODE HERE ###