태지쌤

로봇 & 코딩교육 No.1 크리에이터

IT관련

ChatGPT로 코드 오류 찾기/디버깅(LinAlgError)

태지쌤 2023. 2. 21. 21:39
반응형

ChatGPT를 파이썬 코드의

오류를 찾는데 사용해봤어요.

기존에 개발자들이 코딩을 하다가

에러가 발생할 경우는

구글이나 스택오버플로우를 이용합니다.

오늘은 ChatGPT에게 물어봤어요.

LinAlgError Traceback (most recent call last) <ipython-input-7-c09288e68e94> in <module> ----> 1 e = np.linalg.inv(a) 2 print(e) <__array_function__ internals> in inv(*args, **kwargs) 1 frames /usr/local/lib/python3.8/dist-packages/numpy/linalg/linalg.py in _raise_linalgerror_singular(err, flag) 86 87 def _raise_linalgerror_singular(err, flag): ---> 88 raise LinAlgError("Singular matrix") 89 90 def _raise_linalgerror_nonposdef(err, flag): LinAlgError: Singular matrix

This error message indicates that you are trying to compute the inverse of a singular matrix, which is not possible. A singular matrix is a square matrix that does not have an inverse, and its determinant is zero.

There are a few possible reasons why a matrix might be singular:

  1. The matrix is not full rank: This means that some of the rows or columns in the matrix are linearly dependent on other rows or columns, so the matrix does not have full rank. In this case, some information is redundant, and the matrix cannot be inverted.
  2. The matrix is ill-conditioned: This means that the matrix has a very small determinant, which can cause numerical instability when computing the inverse.
  3. The matrix is simply not invertible: There are some matrices that do not have inverses, no matter what.

To avoid this error, you can try to check the properties of the matrix, such as its rank or determinant, before attempting to compute its inverse. You could also try to use a different method or algorithm that is more appropriate for the type of matrix you are working with.

a = np.array([1,2,3]) b = np.array([[1,2,3],[4,5,6]]) c = np.array([[1,2,3],[4,5,6]],[[3,2,1],[6,5,4]]) TypeError Traceback (most recent call last) <ipython-input-9-11aac8d75841> in <module> 1 a = np.array([1,2,3]) 2 b = np.array([[1,2,3],[4,5,6]]) ----> 3 c = np.array([[1,2,3],[4,5,6]],[[3,2,1],[6,5,4]]) TypeError: Field elements must be 2- or 3-tuples, got '[3, 2, 1]'

The error is occurring because there is a mistake in the way the c array is defined. The correct way to define a 3-dimensional array with shape (2, 2, 3) where the first two dimensions have values from the same list is:

python

c = np.array([[[1,2,3],[4,5,6]],[[3,2,1],[6,5,4]]])

Here, the array c is defined as a list of two elements, where each element is a 2-dimensional list containing 3 elements each. These 2-dimensional lists are themselves grouped into a 3-dimensional array with shape (2, 2, 3).

몇 가지 테스트 해본 결과

생각보다 코드의 오류를

아주 잘 찾아내는 모습입니다.

반응형