【问题标题】:What do the > < signs in numpy dtype mean?numpy dtype 中的 > < 符号是什么意思?
【发布时间】:2016-11-14 13:06:14
【问题描述】:

dtype='f'dtype='f4'dtype='&gt;f4'dtype'&lt;f4' 有什么区别? docs on types 中没有解释语法(除了 'f' 是 'float32' 的简写);它在records的页面中被广泛使用,但&gt;/&lt;的含义在那里也没有解释。

经过一些实验,我发现

    In [13]: a = np.array([1.0], dtype='f')
    In [15]: print(a.dtype)
    float32

    In [16]: a = np.array([1.0], dtype='<f4')
    In [17]: print(a.dtype)
    float32

但是

    In [18]: a = np.array([1.0], dtype='>f4')
    In [19]: print(a.dtype)
    >f4

这让我相信这些是不等价的,这可能是我在使用外部库时遇到的问题的解释。

【问题讨论】:

    标签: python numpy types


    【解决方案1】:

    字节序。

    &lt; = little-endian(LSB 优先)

    &gt; = 大端(MSB 优先)

    https://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.byteorder.html

    【讨论】:

    • = = 原生,| = 不适用
    【解决方案2】:

    通过查找数据类型对象,您可以看到“>”和“

    https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

    >>> dt = np.dtype('>H') # big-endian unsigned short
    >>> dt = np.dtype('<f') # little-endian single-precision float
    

    f 是一个单精度浮点数,在您的情况下它使用 4 个字节(4 x 8 = 32 位)。

    dtype='<f4'
    

    使用 little endian 字节顺序使 dtype 成为 32 位单精度浮点数。

    有关字节顺序的更多信息可以使用 wiki 找到 https://en.wikipedia.org/wiki/Endianness

    【讨论】:

      【解决方案3】:

      我刚遇到同样的问题,想上网找答案。

      “f”是“float32”的简写。

      “f4”也表示“float32”,因为它有 4 个字节,每个字节有 8 位。

      同样,“f8”表示“float64”,因为 8*8 = 64。

      对于'>f4'和'&lt;f4'的区别,这与32位如何存储在4个字节中有关。

      ('>')Big Endian 字节顺序:数据的最高有效字节(“大端”)放置在地址最低的字节处。其余数据按顺序放置在内存中接下来的三个字节中。

      ('

      更多详细信息请查看此链接:https://chortle.ccsu.edu/AssemblyTutorial/Chapter-15/ass15_3.html

      【讨论】:

        猜你喜欢
        • 2010-09-29
        • 2011-04-18
        • 2015-07-04
        • 1970-01-01
        • 2011-04-07
        • 2014-05-11
        • 1970-01-01
        • 1970-01-01
        • 2022-12-22
        相关资源
        最近更新 更多