C1W2_Assignment Week

i am trying to solve exercise 1, but i keep getting errors.

After inputting the required values in the grade code, I got 3 tests passed, and one failed for w2_unittest.test_row_echelon_form(row_echelon_form).

I need suggestions on how to solve the issue.

Thank you.

Welcome to the community.

The error message suggests there is an error in your row_echelon_form() function.

How do I resolve this?

Study your code in row_echelon_form() very carefully, and compare it to the example in Section 4.2.

Okay. I will do that.

Thank you.

It might also help to see the actual output you get when you run the various test cases for your function. You can take a look at what the tests do by opening the file w2_unittests.py. The tests should tell you which one failed, which is part of what we can hope to learn by looking at the output.

I have yet to solve my problem. I do not understand w2_unittests.py. I opened it but did not know how to solve the problem.
The exercise strikes me. I followed the examples in Section 4.2 and am still getting three passes and one failed. I understand the code to fill in the missing values, but I am not getting the output. Please, I need help.

What is the output you get when you actually run the cell that is failing? Please “copy/paste” the entire exception trace or output that you see.

A = np.array([[1,2,3],[0,1,0], [0,0,5]])
B = np.array([[1], [2], [4]])
row_echelon_form(A,B)
array([[1., 2., 3., 1.],
[0., 1., 0., 2.],
[0., 0., 5., 4.]])
w2_unittest.test_row_echelon_form(row_echelon_form)
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.25 1. 1.25 1.5 ]
[5. 0. 2. 3. ]].
3 Tests passed
1 Tests failed

Ok, your code clearly wrong, since you get the wrong answer. Now the question is how to figure out why it is wrong. As Tom pointed out, they gave you detailed step by step example in the instructions. My recommendation would be to “instrument” your code with print statements, so that you can track the steps and compare to their example and figure out where you are “going off the rails”.

As an example, I instrumented my code with print statements and here’s my output for that same test with all the intermediate prints:

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.]
Inner loop row = 2
M[2] after = [0. 2. 2. 2.]
Outer loop row = 1
M[1] after 1/pivot =
[-0.   1.   1.3  1.7]
Inner loop row = 2
M[2] after = [ 0.   0.  -0.6 -1.4]
Outer loop row = 2
M[2] after 1/pivot =
[-0.         -0.          1.          2.33333333]
M final =
[[ 1.          2.          3.          4.        ]
 [-0.          1.          1.3         1.7       ]
 [-0.         -0.          1.          2.33333333]]
 All tests passed

So the first M value there is the original input for the test case. And you can see that my final answer agrees with the “Expected” value shown in your failure message.

Notice that you don’t even end up with 0s in position [1,0] and [2,0] of the output matrix. So the basic inner loop is not working correctly. Now you need to figure out why.

Thank you very much. I can see that my M is different from this

I thought the num_rows is the number of rows the matrix has, which is three.

Yes, but the point is that is just with one particular test matrix. Then what happens if they give you a different test input that has 4 or 5 rows?

You’ve hard-coded the loop limit to be 2, but the point is to use the variable that has the actual number of rows, right? It may be different in each invocation of your function, which is why “hard-coding” is a bad idea.

Footnote for other readers: some of the discussion here is based on a DM thread in which we actually looked at the code, which we can’t really do here in the public thread.