Note that type(numpy.ndarray) is type itself and watch out for boolean and scalar types. Do not be discouraged if it is not intuitive or simple, it is pain at first.
See also: - 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
Fun with logical values:
>>> 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
See more fun with scalar types: - 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
Darren Weber Feb 01 '19 at 2:16 2019-02-01 02:16
source share