Just to follow up here, I did file the bug about this to get the instructions fixed. But there is another level of subtlety here: using “in place” operators like /=, +=, -= and so forth with numpy arrays has another potential downside. When you pass a numpy array or any other python “object” (as opposed to a simple scalar value) as a parameter to a function, it is passed “by reference” and not “by value”. In the case at hand here, that means that inside the local scope of the normalize_rows
function, the variable x is actually a reference (pointer) to the same global object that was passed on the call to normalize_rows
. That means if you use /= as you did in your code, you have overwritten the global variable, unless you take some action to prevent that. That would mean creating a local copy that is not a reference to the object that was passed. There are a number of ways to do that:
xlocal = x.copy()
xlocal = np.copy(x)
xlocal = np.array(x, copy = True)
xlocal = copy.deepcopy(x)
We will run into this issue later in the Week 3 and Week 4 assignments in DLS C1. Here’s a thread which explains the issue in more detail. Please read all the way through, not just the first post linked there.