기다려주세요
도도동짱
전체 방문자
오늘
어제
  • 전체보기 (65)
    • Diary - Life (9)
    • Diary - IT (7)
    • Data Science (6)
    • CS & Programming (20)
    • Debugging (3)
    • Papers (10)
    • Project (3)
    • Lecture (7)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • Github
  • Instagram

공지사항

  • 김동혁입니다 :)

인기 글

태그

  • segmentation
  • gpt4
  • 회고
  • ubuntu
  • 리눅스
  • 빅데이터
  • python
  • 우분투
  • weakly supervised learning
  • 디버깅
  • 네이버 개발자 오픈클래스
  • joblib
  • 파이썬
  • 개발
  • 알고리즘
  • numpay
  • 투자
  • polyp segmentation
  • GIT
  • WSSS
  • weakly supervised semantic segmentation
  • 일상
  • 프로젝트
  • attribution method
  • 취업
  • Deep learning
  • 프로그래밍
  • Linux
  • 에러
  • Transformer

최근 댓글

최근 글

기다려주세요

Debugging

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 해결법

2023. 1. 5. 09:42

파이썬에서 이미지나 리스트 등 array 형태의 무언가를 다루다 보면 다음과 같은 에러가 발생합니다.

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

에러의 뜻은 말 그대로 "하나 이상의 원소를 가진 array의 참값은 판정하기 모호하니까, 하나의 값으로 줘라~" 이겁니다.

 

 

예시를 들자면 다음과 같습니다. 어떤 이미지가 다른 이미지와 같다는 조건문을 넣고 싶을 때,

img1 = cv2.imread("car.png")
img2 = cv2.imread("car.png")
if img1 == img2:
    print("같은 그림")
    
-----------------------------------------------------
ValueError Traceback (most recent call last)
Cell In [38], line 1
----> 1 if img1 == img2:
      2     print("같은 그림")

ValueError: The truth value of an array with more
than one element is ambiguous. Use a.any() or a.all()

 

같은 그림을 불러와서 같은 지 확인했는데 왜 안될까요?

>>> img1 == img2
array([[[ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        ...,
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True]],

       [[ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        ...,
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True]],

       [[ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        ...,
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True]],

       ...,

바로 img1==img2 결과 자체가 array 형태이기 때문입니다. if 문이 array의 어느 부분을 보고 참/거짓을 판별할지를 모르는 겁니다. 그래서 파이썬의 numpy는. all()과. any()라는 메서드를 가지고 있습니다!

  • . all(): 모든 값이 True면 True, 하나라도 False면 False 반환
  • . any(): 하나라도 True면 True, 모두 False면 False 반환

 

그래서 위처럼 조건문을 쓰고 싶으면, 다음과 같이 사용합시다.

>>>if (img1 == img2).all():
...    print("같은 그림")
같은 그림

 

같은 그림을 비교할 때 (img1 == img2).all()은 True니까 (img1 == img2).all() is True라는 코드도 True가 나오겠죠?

절대 아닙니다.

>>>(img1 == img2).all() is True
False

 

왜 False인지는 type을 비교해보면 알 수 있습니다.

>>>print(type((img1 == img2).all()), type(True))
<class 'numpy.bool_'> <class 'bool'>

타입이 다르므로 위 조건문에서는 False를 반환하는 겁니다.

저작자표시 동일조건 (새창열림)

'Debugging' 카테고리의 다른 글

python 에러: UnboundLocalError: local variable 'tmp' referenced before assignment  (0) 2023.01.02
python joblib 에러: _pickle.PicklingError: Could not pickle the task to send it to the workers 해결하기  (0) 2022.12.26
도도동짱
Working on it, Walking on IT
Designed By 정상우.

티스토리툴바