【问题标题】:What are the available datatypes for 'dtype' with NumPy's loadtxt() an genfromtxt?带有 NumPy loadtxt() 和 genfromtxt 的“dtype”有哪些可用数据类型?
【发布时间】:2012-12-09 10:39:12
【问题描述】:

有哪些numpy.loadtxtnumpy.genfromtxt 可用于导入具有不同数据类型的表数据,可用的缩写有哪些(例如,i32 表示整数)?

This post 演示了条件的使用,我很好奇是否有人会详细说明。

【问题讨论】:

标签: python numpy


【解决方案1】:

除了np.sctypeDict,还有这些变量:

In [141]: np.typecodes
Out[141]: 
{'All': '?bhilqpBHILQPefdgFDGSUVOMm',
 'AllFloat': 'efdgFDG',
 'AllInteger': 'bBhHiIlLqQpP',
 'Character': 'c',
 'Complex': 'FDG',
 'Datetime': 'Mm',
 'Float': 'efdg',
 'Integer': 'bhilqp',
 'UnsignedInteger': 'BHILQP'}

In [143]: np.sctypes
Out[143]: 
{'complex': [numpy.complex64, numpy.complex128, numpy.complex192],
 'float': [numpy.float16, numpy.float32, numpy.float64, numpy.float96],
 'int': [numpy.int8, numpy.int16, numpy.int32, numpy.int32, numpy.int64],
 'others': [bool, object, str, unicode, numpy.void],
 'uint': [numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint32, numpy.uint64]}

【讨论】:

    【解决方案2】:

    关于dtypes的一般信息:http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

    来自http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars-built-in

    在 NumPy 中,有 24 种新的基本 Python 类型来描述不同类型的标量。这些类型描述符主要基于编写 CPython 的 C 语言中可用的类型,还有几种与 Python 的类型兼容的附加类型。

    而我没有意识到的是:

    类似 C 的名称与字符代码相关联,如表中所示。但是,不鼓励使用字符代码。

    我怀疑numpy 代码/文档库很快就会消失,所以我猜这说明了一切!

    【讨论】:

    • +1(尽管可以想象,numpy 文档库可能会从 docs.scipy.org 移走而不会真正消失,因为 numpy top-level page 最近移走了......)
    • @abarnert 我刚刚学到了一些新东西,同时查看了该页面 - 只是将其放入编辑中
    • 讽刺的是,scipy.org 今天下线了
    【解决方案3】:

    for k, v in np.sctypeDict.iteritems(): print '{0:14s} : {1:40s}'.format(str(k), v)

    Q              : <type 'numpy.uint64'>      
    U              : <type 'numpy.unicode_'>
    a              : <type 'numpy.string_'>
    

    等等

    【讨论】:

      猜你喜欢
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      相关资源
      最近更新 更多