如何解决此错误:数组几乎不等于七个小数?

时间:2019-04-03 19:55:50

标签: python numpy linear-algebra

这是针对Coursera LinAlg + ML项目的。这是我的代码:

# GRADED FUNCTION
import numpy as np

# Our function will go through the matrix replacing each row in order turning it into echelon form.
# If at any point it fails because it can't put a 1 in the leading diagonal,
# we will return the value True, otherwise, we will return False.
# There is no need to edit this function.
def isSingular(A) :
    B = np.array(A, dtype=np.float_) # Make B as a copy of A, since we're going to alter it's values.
    try:
        fixRowZero(B)
        fixRowOne(B)
        fixRowTwo(B)
        fixRowThree(B)
    except MatrixIsSingular:
        return True
    return False

# This next line defines our error flag. For when things go wrong if the matrix is singular.
# There is no need to edit this line.
class MatrixIsSingular(Exception): pass

# For Row Zero, all we require is the first element is equal to 1.
# We'll divide the row by the value of A[0, 0].
# This will get us in trouble though if A[0, 0] equals 0, so first we'll test for that,
# and if this is true, we'll add one of the lower rows to the first one before the division.
# We'll repeat the test going down each lower row until we can do the division.
# There is no need to edit this function.
def fixRowZero(A) :
    if A[0,0] == 0 :
        A[0] = A[0] + A[1]
    if A[0,0] == 0 :
        A[0] = A[0] + A[2]
    if A[0,0] == 0 :
        A[0] = A[0] + A[3]
    if A[0,0] == 0 :
        raise MatrixIsSingular()
    A[0] = A[0] / A[0,0]
    return A

# First we'll set the sub-diagonal elements to zero, i.e. A[1,0].
# Next we want the diagonal element to be equal to one.
# We'll divide the row by the value of A[1, 1].
# Again, we need to test if this is zero.
# If so, we'll add a lower row and repeat setting the sub-diagonal elements to zero.
# There is no need to edit this function.
def fixRowOne(A) :
    A[1] = A[1] - A[1,0] * A[0]
    if A[1,1] == 0 :
        A[1] = A[1] + A[2]
        A[1] = A[1] - A[1,0] * A[0]
    if A[1,1] == 0 :
        A[1] = A[1] + A[3]
        A[1] = A[1] - A[1,0] * A[0]
    if A[1,1] == 0 :
        raise MatrixIsSingular()
    A[1] = A[1] / A[1,1]
    return A

# This is the first function that you should complete.
# Follow the instructions inside the function at each comment.
def fixRowTwo(A) :
    # Insert code below to set the sub-diagonal elements of row two to zero (there are two of them).
    A[2] = A[2] - A[2, 0] * A[0]
    A[2] = A[2] - A[2, 1] * A[1]
    # Next we'll test that the diagonal element is not zero.
    if A[2,2] == 0 :
        # Insert code below that adds a lower row to row 2.
        A[2] = A[2] + A[3]
        # Now repeat your code which sets the sub-diagonal elements to zero.
        A[2] = A[2] - A[2, 0] * A[0]
        A[2] = A[2] - A[2, 1] * A[0] 
    if A[2,2] == 0 :
        raise MatrixIsSingular()
    # Finally set the diagonal element to one by dividing the whole row by that element.
    A[2] = A[2] / A[2, 2]
    return A

# You should also complete this function.
# Follow the instructions inside the function at each comment.
def fixRowThree(A) :
    # Insert code below to set the sub-diagonal elements of row three to zero.
    A[3] = A[3] - A[3, 0] * A[0]
    A[3] = A[3] - A[3, 1] * A[1]
    A[3] = A[3] - A[3, 2] * A[2]
    # Complete the if statement to test if the diagonal element is zero.
    if A[3, 3] == 0:
        raise MatrixIsSingular()
    # Transform the row to set the diagonal element to one.
    A[3] = A[3] / A[3, 3]
    return A

现在我该如何解决此错误:

======================================================================
FAIL: test_fixRowThree (special_matrix_test.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "assignments/la/special_matrix_test.py", line 236, in test_fixRowThree
    self._test_fn(solution_fn, test_fn)
  File "assignments/la/special_matrix_test.py", line 159, in _test_fn
    np.testing.assert_almost_equal(actual, expected)
  File "/opt/conda/lib/python3.6/site-packages/numpy/testing/nose_tools/utils.py", line 565, in assert_almost_equal
    return assert_array_almost_equal(actual, desired, decimal, err_msg)
  File "/opt/conda/lib/python3.6/site-packages/numpy/testing/nose_tools/utils.py", line 963, in assert_array_almost_equal
    precision=decimal)
  File "/opt/conda/lib/python3.6/site-packages/numpy/testing/nose_tools/utils.py", line 779, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not almost equal to 7 decimals

(mismatch 31.25%)
 x: array([[ 1.       ,  7.5      , -2.5      ,  3.5      ],
       [-0.       ,  1.       , -0.7142857,  0.4285714],
       [-0.5806452, -3.7741935,  1.       , -1.8387097],
       [ 0.0849057,  0.5518868,  0.       ,  1.       ]])
 y: array([[ 1.       ,  7.5      , -2.5      ,  3.5      ],
       [-0.       ,  1.       , -0.7142857,  0.4285714],
       [ 0.       ,  0.       ,  1.       ,  1.5      ],
       [ 0.       ,  0.       ,  0.       ,  1.       ]])

======================================================================
FAIL: test_fixRowTwo (special_matrix_test.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "assignments/la/special_matrix_test.py", line 216, in test_fixRowTwo
    self._test_fn(solution_fn, test_fn)
  File "assignments/la/special_matrix_test.py", line 159, in _test_fn
    np.testing.assert_almost_equal(actual, expected)
  File "/opt/conda/lib/python3.6/site-packages/numpy/testing/nose_tools/utils.py", line 565, in assert_almost_equal
    return assert_array_almost_equal(actual, desired, decimal, err_msg)
  File "/opt/conda/lib/python3.6/site-packages/numpy/testing/nose_tools/utils.py", line 963, in assert_array_almost_equal
    precision=decimal)
  File "/opt/conda/lib/python3.6/site-packages/numpy/testing/nose_tools/utils.py", line 779, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not almost equal to 7 decimals

(mismatch 18.75%)
 x: array([[ 1.       ,  7.5      , -2.5      ,  3.5      ],
       [-0.       ,  1.       , -0.7142857,  0.4285714],
       [-0.5806452, -3.7741935,  1.       , -1.8387097],
       [ 1.       ,  3.       ,  1.       ,  3.       ]])
 y: array([[ 1.       ,  7.5      , -2.5      ,  3.5      ],
       [-0.       ,  1.       , -0.7142857,  0.4285714],
       [ 0.       ,  0.       ,  1.       ,  1.5      ],
       [ 1.       ,  3.       ,  1.       ,  3.       ]])

----------------------------------------------------------------------
Ran 4 tests in 0.008s

FAILED (failures=2)

此错误不断出现,我不知道该怎么办! 该算法应该可以工作,但是我不知道“数组几乎不等于7位小数”是什么意思。 请让我知道您的想法以及如何解决此错误? 谢谢!

-萨蒂亚

0 个答案:

没有答案