numpy.genfromtxt中的dtype参数 [英] dtype argument in numpy.genfromtxt

查看:528
本文介绍了numpy.genfromtxt中的dtype参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>>> from io import StringIO
>>> import numpy as np
>>> s = StringIO("1,1.3,abcde")
>>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),
... ('mystring','S5')], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
      dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])

我的问题与dtype参数有关. 我无法理解dtype="i8,f8,|S5"代表什么. 我可以确定 i 是整数, f 是浮点数,而 s 是字符串,但是i8中的8是什么?我首先以字节为单位理解它,但是s5怎么可能. 我了解 dtype 有助于指定数据类型,以便我们可以从CSV文件读取数据,但是有人可以对数据类型提供一些见识

My question is related to dtype argument. I am unable to understand what dtype="i8,f8,|S5" stands for. I can make out that i is an integer,f is the float and s is the string but what is 8 in i8? I first understood it for bytes but how can then s5 be possible. I understand that dtype helps to specify the data type so that we can read from CSV file but can someone give some insight on data types

推荐答案

i8f8中的8是字节数.有几种不同的方法可以在numpy中表示相同的数据类型.您从np.genfromtxt中看到的字符串为紧凑格式.前面的<>符号表示尾数大或小(请参见文档),后跟i表示整数或f表示浮点/双精度以及字节数.

The 8 in i8 or f8 is the number of bytes. There are several different ways to express the same datatype in numpy. The strings you see from np.genfromtxt are in the compact format. The < or > sign in front mean little or big endian (see documentation), followed by i for integer or f for float/double, and the number of bytes.

较长的数据类型名称的大小为位,而不是字节,这意味着i8int64f4float32,依此类推.例如:

The longer datatype names have the size in bits instead of bytes, meaning that i8 is int64, f4 is float32 and so on. E.g.:

>>> np.dtype('i8')
dtype('int64')
>>> np.dtype('f4')
dtype('float32')

默认情况下,这些都是小尾数法.据我所知,如果要大字节序,np.dtype不会返回长格式:

By default these are all little endian. If you want big endian, as far as I know, np.dtype does not return the long form:

>>> np.dtype('>c16')
dtype('>c16') 

字符串是一种特殊的数据类型,数字表示最大字符串字符数.有关更多详细信息,请参见此问题.

Strings are a special datatype, and the number means the maximum number of string characters. See this question for more details.

这篇关于numpy.genfromtxt中的dtype参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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