Dou*_*ple 80 python duck-typing numpy dynamic-typing
如何可靠地确定对象是否具有numpy类型?
我意识到这个问题违背了鸭子打字的哲学,但是想法是确保一个函数(使用scipy和numpy)永远不会返回一个numpy类型,除非它被调用numpy类型. 这出现在另一个问题的解决方案中,但我认为确定一个对象是否具有numpy类型的一般问题远远远离它们应该分开的原始问题.
aba*_*ert 92
使用内置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
Run Code Online (Sandbox Code Playgroud)
Dou*_*ple 60
我提出的解决方案是:
isinstance(y, (np.ndarray, np.generic) )
Run Code Online (Sandbox Code Playgroud)
然而,并非100%清楚所有numpy类型都保证是np.ndarray
或者np.generic
,这可能不是版本强大.
Lin*_*dc3 18
老问题,但我想出了一个明确的答案与一个例子.因为我遇到同样的问题而没有找到明确的答案,所以不能让问题保持新鲜.关键是要确保已numpy
导入,然后运行isinstance
bool.虽然这看起来很简单,但如果您在不同的数据类型中进行一些计算,这个小的检查可以在您开始一些numpy矢量化操作之前作为快速测试.
##################
# important part!
##################
import numpy as np
####################
# toy array for demo
####################
arr = np.asarray(range(1,100,2))
########################
# The instance check
########################
isinstance(arr,np.ndarray)
Run Code Online (Sandbox Code Playgroud)
要获取类型,请使用内置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
Run Code Online (Sandbox Code Playgroud)
(顺便说一下,这个例子在IPython中运行.非常方便交互式使用和快速测试.)
这实际上取决于你在寻找什么.
ndarray
,那么a isinstance(..., np.ndarray)
可能是最简单的.确保你不在后台重新加载numpy,因为模块可能不同,但除此之外,你应该没问题.MaskedArrays
,matrix
,recarray
是所有子类ndarray
,所以你应该设置.shape
和dtype
属性.您可以将其dtype
与基本dtypes 进行比较,您可以在其中找到列表np.core.numerictypes.genericTypeRank
.请注意,此列表的元素是字符串,因此您必须执行tested.dtype is np.dtype(an_element_of_the_list)
...请注意, 是type(numpy.ndarray)
一个type
自身,并注意布尔类型和标量类型。如果它不直观或不简单,请不要太沮丧,一开始会很痛苦。
另请参阅: - https://docs.scipy.org/doc/numpy-1.15.1/reference/arrays.dtypes.html - https://github.com/machinalis/mypy-data/tree/master/numpy-米皮
>>> 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
Run Code Online (Sandbox Code Playgroud)
布尔值的乐趣:
>>> 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
Run Code Online (Sandbox Code Playgroud)
标量类型的更多乐趣,请参阅: - 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!
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
86711 次 |
最近记录: |