In [8]:
import numpy as np
test_A = ([
[1, 2, 3, 14, 15],
[6, 17, 18, 9, 10]
])
type(test_A)
Out[8]:
list
In [9]:
# test_A에서 10 초과인 값의 인덱스를 출력한다.
np.where(test_A > 10)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_18068/477538254.py in <module> 1 # test_A에서 10 초과인 값의 인덱스를 출력한다. ----> 2 np.where(test_A > 10) TypeError: '>' not supported between instances of 'list' and 'int'
In [25]:
# 'TypeError: '>' not supported between instances of 'list' and 'int'' 오류 출력
# 이는 np.where를 사용하는 container의 data_type이 list이기 때문에 벌어지는 문제였다.
# 적용하고자 하는 container를 array로 선언한다면 문제없이 잘 작동한다.
In [11]:
test_B = np.array([
[1, 2, 3, 14, 15],
[6, 17, 18, 9, 10]
])
type(test_B)
Out[11]:
numpy.ndarray
In [12]:
# test_B에서 10 초과인 값으 인덱스를 출력한다.
np.where(test_B > 10)
Out[12]:
(array([0, 0, 1, 1], dtype=int64), array([3, 4, 1, 2], dtype=int64))
In [13]:
test_C = np.array([
[1, 2, 3, 14, 15],
[6, 7, 18, 19, 0]
])
type(test_C)
Out[13]:
numpy.ndarray
In [16]:
# test_C에서 10 미만인 값으로 인덱스를 출력한다.
test_C_result = np.where(test_C < 10)
test_C_result
Out[16]:
(array([0, 0, 0, 1, 1, 1], dtype=int64), array([0, 1, 2, 0, 1, 4], dtype=int64))
In [19]:
# np.where의 결과값은 tuple로 출력된다.
type(test_C_result)
Out[19]:
tuple
In [21]:
# np.where 결과값의 [0]인덱스는 해당 인덱스가 위치한 행 인덱스를 표시한다.
test_C_result[0]
Out[21]:
array([0, 0, 0, 1, 1, 1], dtype=int64)
In [22]:
# np.where 결과값의 [1]인덱스는 해당 인덱스가 위치한 열 인덱스를 표시한다.
test_C_result[1]
Out[22]:
array([0, 1, 2, 0, 1, 4], dtype=int64)
In [24]:
# np.where의 결과값에서 인덱스를 추출하면,
# 추출된 값의 타입은 array이다.
type(test_C_result[0])
Out[24]:
numpy.ndarray
In [ ]:
'전문지식 함양 > TIL' 카테고리의 다른 글
[프로그래머스 겨울방학 인공지능 과정] 딥러닝 기초이론 (0) | 2022.01.24 |
---|---|
[프로그래머스 겨울방학 인공지능 과정] numpy 복습 및 심화문제 (0) | 2022.01.20 |
[프로그래머스 겨울방학 인공지능 과정] numpy 복습 2 (0) | 2022.01.18 |
[프로그래머스 겨울방학 인공지능 과정] numpy 복습 (0) | 2022.01.17 |
[프로그래머스 겨울방학 인공지능 과정] EDA Project2 (0) | 2022.01.15 |