NumPy数据类型比较 [英] NumPy data type comparison

查看:117
本文介绍了NumPy数据类型比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在比较两个不同数组的数据类型,以选择一个适合将两者结合的数组.我很高兴发现自己可以执行比较操作,但是在此过程中发现了以下奇怪的行为:

I was playing with comparing data types of two different arrays to pick one that is suitable for combining the two. I was happy to discover that I could perform comparison operations, but in the process discovered the following strange behavior:

In [1]: numpy.int16 > numpy.float32
Out[1]: True

In [2]: numpy.dtype('int16') > numpy.dtype('float32')
Out[2]: False

有人可以解释这里发生了什么吗?这是NumPy 1.8.2.

Can anyone explain what is going on here? This is NumPy 1.8.2.

推荐答案

第一个比较没有意义,第二个有意义.

The first comparison is not meaningful, the second is meaningful.

对于numpy.int16 > numpy.float32,我们正在比较两个type对象:

With numpy.int16 > numpy.float32 we are comparing two type objects:

>>> type(numpy.int16)
type
>>> numpy.int16 > numpy.float32 # I'm using Python 3
TypeError: unorderable types: type() > type()

在Python 3中,此比较立即失败,因为type实例没有定义的顺序.在Python 2中,返回的是布尔值,但不能依赖于布尔值的一致性(回退到比较内存地址或其他实现级别的东西).

In Python 3 this comparison fails immediately since there is no defined ordering for type instances. In Python 2, a boolean is returned but cannot be relied upon for consistency (it falls back to comparing memory addresses or other implementation-level stuff).

第二个比较确实在Python 3中有效,并且始终如一地工作(在Python 2中相同).这是因为我们现在正在比较dtype实例:

The second comparison does work in Python 3, and it works consistently (same in Python 2). This is because we're now comparing dtype instances:

>>> type(numpy.dtype('int16'))
numpy.dtype
>>> numpy.dtype('int16') > numpy.dtype('float32')
False
>>> numpy.dtype('int32') < numpy.dtype('|S10')
False
>>> numpy.dtype('int32') < numpy.dtype('|S11')
True

此顺序背后的逻辑是什么?

What's the logic behind this ordering?

dtype实例根据是否可以(安全地)强制转换为另一实例来排序.如果可以安全地将强制转换为该类型,则该类型小于.

dtype instances are ordered according to whether one can be cast (safely) to another. One type is less than another if it can be safely cast to that type.

有关比较运算符的实现,请参见 arraydescr_richcompare 功能.

For the implementation of the comparison operators, look at descriptor.c; specifically at the arraydescr_richcompare function.

这是<运算符映射到的内容:

Here's what the < operator maps to:

switch (cmp_op) {
 case Py_LT:
        if (!PyArray_EquivTypes(self, new) && PyArray_CanCastTo(self, new)) {
            result = Py_True;
        }
        else {
            result = Py_False;
        }
        break;

从本质上讲,NumPy只是检查两种类型是否(i)不等效,并且(ii)第一类型可以转换为第二类型.

Essentially, NumPy just checks that the two types are (i) not equivalent, and (ii) that the first type can be cast to the second type.

此功能在NumPy API中也以 :

This functionality is also exposed in the NumPy API as np.can_cast:

>>> np.can_cast('int32', '|S10')
False
>>> np.can_cast('int32', '|S11')
True

这篇关于NumPy数据类型比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆