numpy.genfromtxt 中的 dtype 参数

dtype argument in numpy.genfromtxt

>>> 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 文件中读取,但有人可以对数据类型提供一些见解吗

i8f8中的8为字节数。相同的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)有几种不同的表达方式,后跟i表示整数或f表示float/double,以及字节数。

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

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

默认情况下,这些都是小端。如果你想要大端,据我所知,np.dtype 不是 return 长格式:

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

字符串是一种特殊的数据类型,数字表示字符串字符的最大个数。有关详细信息,请参阅 this question