[python] 파이썬에서 numpy 유형을 식별하는 방법은 무엇입니까?

객체에 numpy 유형이 있는지 어떻게 안정적으로 결정할 수 있습니까?

이 질문이 덕 타이핑의 철학에 위배된다는 것을 알고 있지만, 아이디어는 scipy와 numpy를 사용하는 함수가 numpy 유형으로 호출되지 않는 한 numpy 유형을 반환하지 않도록하는 것입니다. 이것은 다른 질문에 대한 해결책으로 나오지만, 객체가 numpy 유형을 가지고 있는지를 결정하는 일반적인 문제는 분리되어야하는 원래의 질문에서 충분히 멀리 떨어져 있다고 생각합니다.



답변

내장 type함수를 사용하여 유형을 가져온 다음 __module__속성을 사용하여 정의 된 위치를 찾을 수 있습니다 .

>>> import numpy as np
a = np.array([1, 2, 3])
>>> type(a)
<type 'numpy.ndarray'>
>>> type(a).__module__
'numpy'
>>> type(a).__module__ == np.__name__
True


답변

내가 생각 해낸 해결책은 다음과 같습니다.

isinstance(y, (np.ndarray, np.generic) )

그러나 모든 numpy 유형이 또는 중 하나임을 보장하는 것은 100 % 명확 하지 않으며 이는 버전이 견고하지 않을 수 있습니다.np.ndarraynp.generic


답변

오래된 질문이지만 예를 들어 확실한 대답을 얻었습니다. 동일한 문제가 있었고 명확한 답을 찾지 못했기 때문에 질문을 최신 상태로 유지하는 것이 아프지 않습니다. 핵심은 numpy가져 왔는지 확인한 다음 isinstancebool 을 실행하는 것 입니다. 간단 해 보일 수 있지만 다른 데이터 유형에 대해 계산을 수행하는 경우이 작은 검사는 벡터화 된 작업을 시작하기 전에 빠른 테스트 역할을 할 수 있습니다.

##################
# important part!
##################

import numpy as np

####################
# toy array for demo
####################

arr = np.asarray(range(1,100,2))

########################
# The instance check
######################## 

isinstance(arr,np.ndarray)


답변

그것은 실제로 당신이 찾고있는 것에 달려 있습니다.

  • 시퀀스가 실제로 있는지 테스트하려면 ndarraya isinstance(..., np.ndarray)가 가장 쉬울 것입니다. 모듈이 다를 수 있으므로 백그라운드에서 numpy를 다시로드하지 않도록하십시오. 그렇지 않으면 괜찮습니다. MaskedArrays, matrix, recarray의 모든 서브 클래스 ndarray그래서 당신은 설정해야합니다.
  • 스칼라가 numpy 스칼라인지 테스트하려면 상황이 좀 더 복잡해집니다. shapedtype속성 이 있는지 확인할 수 있습니다 . dtype에서 찾을 수있는 목록의 기본 dtypes와 비교할 수 있습니다 np.core.numerictypes.genericTypeRank. 이 목록의 요소는 문자열이므로 다음을 수행해야합니다 tested.dtype is np.dtype(an_element_of_the_list).


답변

유형을 얻으려면 내장 type함수를 사용하십시오 . in연산자를 사용하면 문자열이 포함되어 있는지 확인하여 유형이 numpy 유형인지 테스트 할 수 있습니다 numpy.

In [1]: import numpy as np

In [2]: a = np.array([1, 2, 3])

In [3]: type(a)
Out[3]: <type 'numpy.ndarray'>

In [4]: 'numpy' in str(type(a))
Out[4]: True

(이 예제는 IPython 에서 실행되었습니다 . 대화 형 사용과 빠른 테스트에 매우 편리합니다.)


답변

참고가 type(numpy.ndarray)A는 type자체 부울 스칼라 유형에 대한 조심을. 직관적이거나 쉽지 않은 경우 너무 낙심하지 마십시오. 처음에는 고통 스럽습니다.

참조 : -https : //docs.scipy.org/doc/numpy-1.15.1/reference/arrays.dtypes.html-https
: //github.com/machinalis/mypy-data/tree/master/numpy- mypy

>>> import numpy as np
>>> np.ndarray
<class 'numpy.ndarray'>
>>> type(np.ndarray)
<class 'type'>
>>> a = np.linspace(1,25)
>>> type(a)
<class 'numpy.ndarray'>
>>> type(a) == type(np.ndarray)
False
>>> type(a) == np.ndarray
True
>>> isinstance(a, np.ndarray)
True

부울로 재미 :

>>> b = a.astype('int32') == 11
>>> b[0]
False
>>> isinstance(b[0], bool)
False
>>> isinstance(b[0], np.bool)
False
>>> isinstance(b[0], np.bool_)
True
>>> isinstance(b[0], np.bool8)
True
>>> b[0].dtype == np.bool
True
>>> b[0].dtype == bool  # python equivalent
True

스칼라 유형에 대한 더 많은 재미는 다음을 참조하십시오.- https //docs.scipy.org/doc/numpy-1.15.1/reference/arrays.scalars.html#arrays-scalars-built-in

>>> x = np.array([1,], dtype=np.uint64)
>>> x[0].dtype
dtype('uint64')
>>> isinstance(x[0], np.uint64)
True
>>> isinstance(x[0], np.integer)
True  # generic integer
>>> isinstance(x[0], int)
False  # but not a python int in this case

# Try matching the `kind` strings, e.g.
>>> np.dtype('bool').kind
'b'
>>> np.dtype('int64').kind
'i'
>>> np.dtype('float').kind
'f'
>>> np.dtype('half').kind
'f'

# But be weary of matching dtypes
>>> np.integer
<class 'numpy.integer'>
>>> np.dtype(np.integer)
dtype('int64')
>>> x[0].dtype == np.dtype(np.integer)
False

# Down these paths there be dragons:

# the .dtype attribute returns a kind of dtype, not a specific dtype
>>> isinstance(x[0].dtype, np.dtype)
True
>>> isinstance(x[0].dtype, np.uint64)
False
>>> isinstance(x[0].dtype, np.dtype(np.uint64))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: isinstance() arg 2 must be a type or tuple of types
# yea, don't go there
>>> isinstance(x[0].dtype, np.int_)
False  # again, confusing the .dtype with a specific dtype


# Inequalities can be tricky, although they might
# work sometimes, try to avoid these idioms:

>>> x[0].dtype <= np.dtype(np.uint64)
True
>>> x[0].dtype <= np.dtype(np.float)
True
>>> x[0].dtype <= np.dtype(np.half)
False  # just when things were going well
>>> x[0].dtype <= np.dtype(np.float16)
False  # oh boy
>>> x[0].dtype == np.int
False  # ya, no luck here either
>>> x[0].dtype == np.int_
False  # or here
>>> x[0].dtype == np.uint64
True  # have to end on a good note!


답변