C1W2_Assignment row_echelon_form

Hello,
I completed the row_echelon_form and ran (print)row_echelon_form(A,B), and got what I expected. However when running “w2_unittest.test_row_echelon_form(row_echelon_form)” I got an error message saying it expected on array and got another. The “received” array looked nothing like the array I got when I ran the print statement. It also said I passed 3 tests and failed 1.

I’ve tried saving my notebook under a different name, deleting the original, and get the latest version.

When a unit test fails it means your code implementation is wrong, there maybe various reasons for that including hardcoding variables! Go back and check your implementation for this case!

I think I tracked down the error but I don’t understand why it’s happening.

I put print() statements at the beginning of one of the functions:

def get_index_first_non_zero_value_from_column(M, column, starting_row):
print("row = ", starting_row)
print("column = ", column)
print(M[starting_row:,column])

#rest of function is unchanged

When I run row_enchelon_form, starting_row and column are the values they should be, but values are different in M[starting_row:, column].

Is this something I can correct?

Not familiar with this course but what are you doing here? M[starting_row:,column])?

If you want to choose only one element of the M matrix then this need be M[starting_row,column]), but I dont know what the goal here so maybe another mentor that has done this course can help better!

That part is actually provided in the assignment. It’s supposed to build an array of just the column, starting with row = starting_row.

Here’s the rest of the function:

Get the column array starting from the specified row

column_array = M[starting_row:,column]
for i, val in enumerate(column_array):
    # Iterate over every value in the column array. 
    # To check for non-zero values, you must always use np.isclose instead of doing "val == 0".
    if not np.isclose(val, 0, atol = 1e-5):
        # If one non zero value is found, then adjust the index to match the correct index in the matrix and return it.
        index = i + starting_row
        return index
# If no non-zero value is found below it, return -1.
return -1

But the point is that the inputs to that function are the results of your code in row_echelon_form, right? So it’s that logic that must be wrong, not the “helper” function they gave you. Perfectly correct functions can fail if you pass them incorrect data.

Please show us the full output trace you are getting with the failure.

The other thing to note is that the unit tests may cover more cases than the visible tests you see in the notebook. It’s always a true statement that just passing one test is not enough to guarantee your code is fully correct. You can examine the unit test logic to understand what is being tested by clicking “File → Open” and then opening w2_unittest.py and having a look. If you can figure out which of the tests is actually failing, one debugging approach would be to “play out” that test case with pencil and paper first and then compare that to what your code is doing.

I’m not sure what you mean by full output trace.

I was wrong about those values being correct. I forgot array index starts at zero,

I’ve changed the arrays to the ones in unittest, so I have a better idea of what’s going on, but I’m going to have to set this aside for the moment and come back to it later today or tomorrow.

Thanks

I just meant all the printed output you get when you run the test that is failing. E.g. if I intentionally create a bug in my row_echelon_form function and run it, here’s what I get:

M =
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]
Outer loop row = 0
M[0] after 1/pivot =
[1. 0. 0. 0.]
Inner loop row = 1
M[1] after = [0. 1. 0. 0.]
Outer loop row = 1
M[1] after 1/pivot =
[0. 1. 0. 0.]
M final =
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]
M =
[[1. 2. 3. 4.]
 [5. 0. 2. 3.]
 [1. 4. 5. 6.]]
Outer loop row = 0
M[0] after 1/pivot =
[1. 2. 3. 4.]
Inner loop row = 1
M[1] after = [  0. -10. -13. -17.]
Outer loop row = 1
M[1] after 1/pivot =
[-0.   1.   1.3  1.7]
M final =
[[ 1.   2.   3.   4. ]
 [-0.   1.   1.3  1.7]
 [ 1.   4.   5.   6. ]]
Wrong output for test case check_matrix_1. 
	Expected:
	 [[ 1.          2.          3.          4.        ]
 [-0.          1.          1.3         1.7       ]
 [-0.         -0.          1.          2.33333333]].
	Got:
 [[ 1.   2.   3.   4. ]
 [-0.   1.   1.3  1.7]
 [ 1.   4.   5.   6. ]].
 3  Tests passed
 1  Tests failed

Note that I’ve got some extra print statements for debugging already in the code.

Sure, just let us know how it goes.

Hah I went to the store and had an Eureka moment. I screwed up on the reduction part of the function. I corrected the issue and all tests passed.

Thanks again.

2 Likes

That’s excellent news! Thanks for confirming. Onward. :nerd_face: