Could anybody help me to solve this issue please?

I am in the lab of the first week of the course of probability &Statistics for machine learning but I am getting this error in the exercise 6. I am not sure if I selected the correct parameters for the different cases:


UnboundLocalError Traceback (most recent call last)

Cell In[65], line 7

4 example_breed = df_test[[“breed”]].loc[0][“breed”]

5 print(f"Example dog has breed { example_breed**}** and features: height = { example_dog[‘height’]: .2f**}** , weight = { example_dog[‘weight’]: .2f**}** , bark_days = { example_dog[‘bark_days’]: .2f**}** , ear_head_ratio = { example_dog[‘ear_head_ratio’]: .2f**}** \n ")

----> 7 print(f"Probability of these features if dog is classified as breed 0: { prob_of_X_given_C([*example_dog], FEATURES, 0, train_params)} ")

8 print(f"Probability of these features if dog is classified as breed 1: { prob_of_X_given_C([*example_dog], FEATURES, 1, train_params)} ")

9 print(f"Probability of these features if dog is classified as breed 2: { prob_of_X_given_C([*example_dog], FEATURES, 2, train_params)} ")

Cell In[64], line 53, in prob_of_X_given_C(X, features, breed, params_dict)

50 probability_f = pdf_uniform(x, params.a, params.b)

52 # Multiply by probability of current feature

—> 53 probability *= probability_f

55 ### END CODE HERE ###

57 return probability

UnboundLocalError: local variable ‘probability_f’ referenced before assignment

I recommend you read the FAQ for this course, it has some details about this specific function.

You can find it by using the forum Search tool for “M4ML FAQ”.

It looks like it is an issue that this function thought you didn’t define your variable properly.

A quick fix I could thought of is to give the variable probability_f an initial value, say -9999? so it could still be differentiated with the “real value” you will get later.

If the code is written correctly, that’s not necessary.

That’s true.
He can check whether Line 50 is correctly executed or not.

Ok I will do it, thanks

Ok let me check that thanks

That error pops out when you do not define a variable before calling or using it in a return statement. For example, It usually happens to me when I define a function in a for loop but call it outside without globally defining it.