Week 3, predict: convert float to integer outcome in one line

The notes suggest that the classification (y hat) can be converted to an integer value in one line. I can’t see how to do this so compactly.

I’ve reused working code from the last assignment but although the output is OK I get an error: “predictions referenced before assignment”. The variable predictions is defined in an assignment statement. As far as I know Python creates variables when they are assigned a value. Please advise how to fix this.

See code below:

parameters, t_X = predict_test_case()

predictions = predict(parameters, t_X)

print("Predictions: " + str(predictions))


UnboundLocalError Traceback (most recent call last)
1 parameters, t_X = predict_test_case()
----> 3 predictions = predict(parameters, t_X)
4 print("Predictions: " + str(predictions))

in predict(parameters, X)
29 if A2[0, i] > 0.5 :
—> 30 predictions[0,i] = …
31 else:
32 predictions[0,i] = …

UnboundLocalError: local variable ‘predictions’ referenced before assignment

Hi, Brendon.

Did you miss running any cell before ? Try looking for it. This has caused this unbound error.

That should be a local variable in the local scope of that function. So it should be pretty obvious where to look for the problem. It is true that python will create the variable on the LHS of an assignment, but only when it is a simple reference. Notice that is not what the LHS of that assignment does, right? It indexes predictions. That fails if it is not already defined.

In terms of how to implement predict without a for loop, they give you a pretty big hint in the instructions for that section. They essentially write the code out for you …

@paulinpaloalto, thanks for the feedback. Now if my assignment notebook would just load I could fix it :thinking:

It’s gone from snappy to “Loading mathjax” for ten minutes, and the tab is blank every time I go back to it… Anyway, have logged a report with the help centre