检查numpy数组中的类型 [英] check type within numpy array
问题描述
我有不同类型的数据.其中大多数是int
,有时是float
. int
的大小不同,因此大小为8/16/32位.
对于这种情况,我正在创建一个数值类型转换器.因此,我通过使用isinstence()
检查类型.这是因为我已经读到isinstance()
比type()
差.
I have different types of data. most of them are int
and sometimes float
. The int
is different in size so 8/ 16/ 32 bits are the sizes.
For this situation I'm creating a numerical type converter. therefore i check the type by using isinstence()
. This because I have read that isinstance()
is less worse than type()
.
问题是我得到的很多数据都是numpy数组.我将spyder用作IDE,然后通过变量也可以看到类型.但是当我键入isinstance(var,'type i read')
时,我得到的是False
.
The point is that a lot of data i get is numpy arrays. I use spyder as IDE and then i see by the variables also a type. but when i type isinstance(var,'type i read')
i get False
.
我做了一些检查:
a = 2.17
b = 3
c = np.array(np.random.rand(2, 8))
d = np.array([1])
在那里isinstance(var,type)
我得到:
isinstance(a, float)
True
isinstance(b, int)
True
isinstance(c, float) # or isinstance(c, np.float64)
False
isinstance(d, int) # or isinstance(c, np.int32)
False
我问时
c
和d
为真
isinstance(c, np.ndarray)
True
isinstance(d, np.ndarray)
True
我可以通过
isinstance(c[i][j], np.float64)
True
isinstance(d[i], np.int32)
True
但是这意味着我必须为每个维度添加一个新索引,否则它将再次为False
.
我可以用dtype
检查那里的类型,例如c.dtype == 'float64'
...
but this means that for every dimension i have to add a new index otherwise it is False
again.
I can check there type with dtype
like c.dtype == 'float64'
...
好吧,对于我发现并尝试过的... 我的问题基本上是:
Oke so for what i have find and tried... My questions are basicly:
- 与
isinstance()
和type()
相比,var.dtype
方法如何(最差/更好等)? - 如果
var.dtype
更糟糕,因为isinstance()
isinstance()
中是否有某些方法没有全部手动索引? (自动索引等)?
- how is the
var.dtype
method compared toisinstance()
andtype()
(worst/ better etc)? - if
var.dtype
is even worse asisinstance()
is there some method in theisinstance()
without all the manual indexing? (autoindexing etc)?
推荐答案
数组是类型为np.ndarray
的对象.它的值或元素存储在数据缓冲区中,可以将其视为内存字节的连续块.数据缓冲区中的字节没有类型,因为它们不是Python对象.
An array is an object of type np.ndarray
. Its values or elements are stored in a data buffer, which can be thought of as a contiguous block of memory bytes. The bytes in the data buffer do not have a type, because they are not Python objects.
该数组具有一个dtype
参数,该参数用于解释这些字节.如果dtype
是int32
(存在各种同义词),则将4个字节解释为整数.访问元素,例如c[0]
给出了一个取决于dtype的新对象,例如对象类型np.int32
.
The array has a dtype
parameter, which is used to interpret those bytes. If dtype
is int32
(there are various synonyms), 4 bytes are interpreted as an integer. Accessing an element, say c[0]
gives a new object that depends on the dtype, e.g. an object type np.int32
.
c[0].item
将给出相应类型的Python对象:
c[0].item
will give an Python object of the corresponding type:
In [2102]: c=np.array([1])
In [2103]: c.dtype
Out[2103]: dtype('int32')
In [2104]: type(c)
Out[2104]: numpy.ndarray
In [2105]: type(c[0])
Out[2105]: numpy.int32
In [2107]: c[0].item()
Out[2107]: 1
In [2108]: type(c[0].item())
Out[2108]: int
(并且c[0].dtype
与c.dtype
相同;您无需索引数组中的各个元素即可检查其dtype).
(And c[0].dtype
is the same as for c.dtype
; you don't need to index individual elements of an array to check their dtype).
该数组的相同4个字节可以视为dtype
int8
-一个单字节整数.
The same 4 bytes of this array can be viewed as dtype
int8
- a single byte integer.
In [2112]: c.view('b')
Out[2112]: array([1, 0, 0, 0], dtype=int8)
此替代视图的单个元素是np.int8
,但是当我使用item()
时,我得到了一个Python整数.没有int8
Python数字类型.
A single element of this alternate view is np.int8
, but when I take item()
, I get a Python integer. There isn't a int8
Python numeric type.
In [2113]: type(c.view('b')[0])
Out[2113]: numpy.int8
In [2115]: type(c.view('b')[0].item())
Out[2115]: int
一个列表包含指向Python对象的指针,每个对象都有一个类型. dtype=object
数组也是如此.但是公共数值数组不包含Python整数或浮点数.它具有可以根据dtype
以各种方式解释的数据缓冲区. Python整数的大小不同,至少与numpy dtype的大小不同.
A list contains pointers to Python objects, each of which has a type. So does an array of dtype=object
. But the common numeric array does not contain Python integers or floats. It has a data buffer that can interpreted in various ways according to the dtype
. Python integers don't come in different sizes, at least not to the same extent as numpy dtypes.
因此,isinstance
和type()
内容不适用于ndarray
的内容.
So the isinstance
and type()
stuff does not apply to the contents of an ndarray
.
===================
====================
根据我收集的注释,您正在尝试将整数数组转换为浮点型.您不是在转换标量.如果是这样,那么dtype
就很重要.数组始终具有dtype
.尚不清楚是否可以将np.float32
强制转换为np.float64
.
From the comments I gather you are trying to convert integer arrays to float. You aren't converting scalars. If so then dtype
is all that matters; an array always has a dtype
. It's unclear whether you are ok with casting a np.float32
to np.float64
.
我建议学习并尝试使用np.can_cast
函数和x.astype
方法.
I'd suggest studying, and experimenting with the np.can_cast
function and the x.astype
method.
x.astype(np.float64, copy=False)
例如,
会将所有int dtypes转换为float,而不会复制已经为float64的类型.它可能会复制并转换np.float32
个.
还要查看这些功能的casting
参数.
Look also at the casting
parameter of these functions.
==========================
===========================
我在scipy.optimize.minimize
中找到了另一种测试工具
I found in scipy.optimize.minimize
another testing tool
In [156]: np.typecodes
Out[156]:
{'All': '?bhilqpBHILQPefdgFDGSUVOMm',
'AllFloat': 'efdgFDG',
'AllInteger': 'bBhHiIlLqQpP',
'Character': 'c',
'Complex': 'FDG',
'Datetime': 'Mm',
'Float': 'efdg',
'Integer': 'bhilqp',
'UnsignedInteger': 'BHILQP'}
它可以用来检查整数:
if x0.dtype.kind in np.typecodes["AllInteger"]:
x0 = np.asarray(x0, dtype=float)
这篇关于检查numpy数组中的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!