gpt4 book ai didi

python - cython 中 np.int、np.int_、int 和 np.int_t 之间的区别?

转载 作者:IT老高 更新时间:2023-10-28 22:08:35 32 4
gpt4 key购买 nike

我对这么多int有点挣扎cython 中的数据类型。
np.int, np.int_, np.int_t, int
我猜 int在纯python中相当于np.int_ , 那么 np.int 在哪里来自?我无法从 numpy 中找到文档?还有,为什么np.int_存在,因为我们已经有了 int ?

在 cython 中,我猜 int用作 cdef int 时变为 C 类型或 ndarray[int] , 当用作 int() 时它仍然是 python 脚轮?

np.int_相当于 long在 C?所以cdef longcdef np.int_ 相同?

什么情况下应该使用np.int_t而不是 np.int ?例如cdef np.int_t , ndarray[np.int_t] ...

有人可以简要解释一下这些类型的错误使用如何影响编译后的 cython 代码的性能吗?

最佳答案

这有点复杂,因为名称根据上下文具有不同的含义。
int

  • 在 Python 中
    int通常只是一个 Python 类型,它具有任意精度,这意味着您可以在其中存储任何可以想象的整数(只要您有足够的内存)。
    >>> int(10**50)
    100000000000000000000000000000000000000000000000000
  • 但是,当您将其用作 dtype 时对于 NumPy 数组,它将被解释为 np.int_ 1.哪个是不是 任意精度,它将具有与 C 的 long 相同的大小:
    >>> np.array(10**50, dtype=int)
    OverflowError: Python int too large to convert to C long

    这也意味着以下两个是等效的:
    np.array([1,2,3], dtype=int)
    np.array([1,2,3], dtype=np.int_)
  • 作为 Cython 类型标识符它还有另一个含义,这里它代表 类型 int .它的精度有限(通常为 32 位)。您可以将其用作 Cython 类型,例如在使用 cdef 定义变量时:
    cdef int value = 100    # variable
    cdef int[:] arr = ... # memoryview

    作为 cdef 的返回值或参数值或 cpdef职能:
    cdef int my_function(int argument1, int argument2):
    # ...

    作为 ndarray 的“通用” :
    cimport numpy as cnp
    cdef cnp.ndarray[int, ndim=1] val = ...

    对于类型类型转换:
    avalue = <int>(another_value)

    可能还有更多。
  • 在 Cython 中,但作为 Python 类型。您仍然可以调用 int你会得到一个“Python int”(任意精度),或者将它用于 isinstance或作为 dtype np.array 的论据.这里的上下文很重要,因此转换为 Python int与转换为 C int 不同:
    cdef object val = int(10)  # Python int
    cdef int val = <int>(10) # C int
  • np.int
    其实这很容易。它只是 int 的别名:
    >>> int is np.int
    True

    所以以上所有内容都适用于 np.int以及。但是你不能把它用作类型标识符,除非你在 cimport 上使用它。 ed 包。在这种情况下,它表示 Python 整数类型。
    cimport numpy as cnp

    cpdef func(cnp.int obj):
    return obj

    这将期望 obj是一个 Python 整数 不是 NumPy 类型 :
    >>> func(np.int_(10))
    TypeError: Argument 'obj' has incorrect type (expected int, got numpy.int32)
    >>> func(10)
    10

    我的建议 np.int : 尽量避免。在 Python 代码中,它相当于 int在 Cython 代码中,它也等效于 Python int但如果用作类型标识符,它可能会使您和所有阅读代码的人感到困惑!这当然让我感到困惑......
    np.int_
    其实它只有一个意思:它是 Python 类型 表示标量 NumPy 类型。你像 Python 一样使用它 int :
    >>> np.int_(10)        # looks like a normal Python integer
    10
    >>> type(np.int_(10)) # but isn't (output may vary depending on your system!)
    numpy.int32

    或者你用它来指定 dtype ,例如 np.array :
    >>> np.array([1,2,3], dtype=np.int_)
    array([1, 2, 3])

    但是您不能在 Cython 中将其用作类型标识符。
    cnp.int_t
    这是 np.int_ 的类型标识符版本.这意味着您不能将其用作 dtype 参数。但是您可以将其用作 cdef 的类型声明:
    cimport numpy as cnp
    import numpy as np

    cdef cnp.int_t[:] arr = np.array([1,2,3], dtype=np.int_)
    |---TYPE---| |---DTYPE---|

    此示例(希望如此)显示尾随 _t 的类型标识符实际上使用 表示数组的类型数据类型 没有尾随 t .你不能在 Cython 代码中交换它们!

    笔记

    NumPy 中还有更多数字类型,我将包括一个列表,其中包含 NumPy dtype 和 Cython 类型标识符以及 C 类型标识符,此处也可以在 Cython 中使用。但它基本上取自 the NumPy documentationCython NumPy pxd file :
    NumPy dtype          Numpy Cython type         C Cython type identifier

    np.bool_ None None
    np.int_ cnp.int_t long
    np.intc None int
    np.intp cnp.intp_t ssize_t
    np.int8 cnp.int8_t signed char
    np.int16 cnp.int16_t signed short
    np.int32 cnp.int32_t signed int
    np.int64 cnp.int64_t signed long long
    np.uint8 cnp.uint8_t unsigned char
    np.uint16 cnp.uint16_t unsigned short
    np.uint32 cnp.uint32_t unsigned int
    np.uint64 cnp.uint64_t unsigned long
    np.float_ cnp.float64_t double
    np.float32 cnp.float32_t float
    np.float64 cnp.float64_t double
    np.complex_ cnp.complex128_t double complex
    np.complex64 cnp.complex64_t float complex
    np.complex128 cnp.complex128_t double complex

    实际上有 np.bool_ 的 Cython 类型: cnp.npy_boolbint但它们目前都不能用于 NumPy 数组。对于标量 cnp.npy_bool将只是一个无符号整数,而 bint将是一个 bool 值。不知道那里发生了什么......

    1 取自 NumPy documentation "Data type objects"

    Built-in Python types

    Several python types are equivalent to a corresponding array scalar when used to generate a dtype object:

    int           np.int_
    bool np.bool_
    float np.float_
    complex np.cfloat
    bytes np.bytes_
    str np.bytes_ (Python2) or np.unicode_ (Python3)
    unicode np.unicode_
    buffer np.void
    (all others) np.object_

    关于python - cython 中 np.int、np.int_、int 和 np.int_t 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21851985/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com