numpy.genfromtxt无法正确读取布尔数据
无论输入值是什么,都将始终返回numpy.genfromtxt无法正确读取布尔数据,numpy,Numpy,无论输入值是什么,都将始终返回False 使用dtype='u1'我得到了预期的'1'。但是使用dtype='b1'(Numpy's bool)我得到了“False”。我不知道这是否是一个bug,但到目前为止,我已经能够让dtype=bool工作(没有显式转换器),只有当文件包含文本字符串“False”和“True”时: In [21]: bool_lines = ['False,False', 'False,True', 'True,False', 'True,True'] In [22]:
False
使用
dtype='u1'
我得到了预期的'1'。但是使用dtype='b1'
(Numpy's bool)我得到了“False”。我不知道这是否是一个bug,但到目前为止,我已经能够让dtype=bool
工作(没有显式转换器),只有当文件包含文本字符串“False”和“True”时:
In [21]: bool_lines = ['False,False', 'False,True', 'True,False', 'True,True']
In [22]: genfromtxt(bool_lines, delimiter=',', dtype=bool)
Out[22]:
array([[False, False],
[False, True],
[ True, False],
[ True, True]], dtype=bool)
如果数据为0和1,则可以将其读取为整数,然后转换为布尔值:
In [26]: bits = ['0,0', '0,1', '1,0', '1,1']
In [27]: genfromtxt(bits, delimiter=',', dtype=np.uint8).astype(bool)
Out[27]:
array([[False, False],
[False, True],
[ True, False],
[ True, True]], dtype=bool)
也可以对每列使用转换器
In [28]: cnv = lambda s: bool(int(s))
In [29]: converters = {0: cnv, 1: cnv}
In [30]: genfromtxt(bits, delimiter=',', dtype=bool, converters=converters)
Out[30]:
array([[False, False],
[False, True],
[ True, False],
[ True, True]], dtype=bool)
genfromtxt
正在使用np.lib.\u iotools.StringConverter(bool)
将字符串转换为bool。它处理大写/小写“真/假”,并返回其他任何错误。我无法使用NumPY 1.9.0重现您的问题。你能添加一个明确的例子吗?1.8.x和1.6.x有这个问题。在新版本中,这可能已被修复。