NumPy loadtxt 中的转换器行为无法理解

mis*_*haF 5 python numpy

我正在尝试使用 numpy.loadtxt 和转换器参数从文本文件中读取数据。我有整数列和字符串的混合。代码是:

a, b, c, d, e = np.loadtxt(infile, delimiter = ',', usecols=(0, 2, 5, 8, 9), skiprows = 1,
                           unpack = True, converters = dict(zip((0, 2, 5, 8, 9), (int, float, float, int, int))))
Run Code Online (Sandbox Code Playgroud)

数据被正确读取并正确解包,但所有变量(a、b、c、d 和 e)最终都为浮点数。我在转换器语法中犯了错误吗?

编辑尝试答案

我尝试按照@joris的建议使用 dtype = (int,float,float,int,int) :

a,b,c,d,e = np.loadtxt(infile,delimiter = ',', usecols=(0,2,5,8,9), skiprows = 1, unpack = True, dtype = (int,float,float,int,int))
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

     41                                            skiprows = 1,
     42                                            unpack = True,
---> 43                                            dtype = (int,float,float,int,int))
     44
     45

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/npyio.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack)
    665     try:
    666         # Make sure we're dealing with a proper dtype

--> 667         dtype = np.dtype(dtype)
    668         defconv = _getconv(dtype)
    669

TypeError: data type not understood
WARNING: Failure executing file: <forward_NDMMF.py>
Run Code Online (Sandbox Code Playgroud)

我正在使用 NumPy v.1.5.1。

jor*_*ris 4

要指定不同列的类型,您可以使用参数dtype而不是converters

dtype=(int,float,float,int,int)
Run Code Online (Sandbox Code Playgroud)

编辑:

显然,这种类型的dtype规范似乎不适用于loadtxt,但它适用于genfromtxt有谁知道为什么这不适用于loadtxt,或者这是 的额外功能之一genfromtxt

如果您想使用loadtxt,则可以使用带有元组的结构化数据类型规范,例如[('f0', int), ('f1', float)]而不是(int, float)

但还有另一个问题。当使用此类结构化数据类型和结构化数组(不同列的不同类型)时,似乎unpack不起作用。至少我尝试过一个简单的例子。但这可能是一个已经解决的错误:http://projects.scipy.org/numpy/ticket/1458(但为此,你甚至必须升级到 1.6)。