gpt4 book ai didi

python - 使用 numpy loadtxt 将导入字符串转换为 float

转载 作者:行者123 更新时间:2023-12-02 04:06:37 25 4
gpt4 key购买 nike

我正在尝试从平面文件导入文本并将其转换为单行内的浮点值。我见过this post它有相同的错误,但我还没有找到输入文件中哪些字符无效。或者我有语法错误?

作为字符串导入并打印结果:

data = np.loadtxt(file, delimiter='\t', dtype=str)
print(data[0:2])
...
[["b'Time'" "b'Percent'"]
["b'99'" "b'0.067'"]]

尝试导入为 float :

# Import data as floats and skip the first row: data_float
data_float = np.loadtxt(data, delimiter='\t', dtype=float, skiprows=1)

它抛出以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
data_float = np.loadtxt(data, delimiter='\t', dtype=float, skiprows=1)
File "<stdin>", line 848, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "<stdin>", line 848, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
ValueError: could not convert string to float: b'["b\'99\'" "b\'0.067\'"]'

对了,我也看过this post这解释了 b 字符,但我认为这不是问题所在。

第一个答案建议的附加故障排除步骤:

data = np.loadtxt(file, delimiter="\tb'", dtype=str)

返回:

array(["b'Time\\tPercent'", "b'99\\t0.067'", "b'99\\t0.133'",
"b'99\\t0.067'", "b'99\\t0'", "b'99\\t0'", "b'0\\t0.5'",
"b'0\\t0.467'", "b'0\\t0.857'", "b'0\\t0.5'", "b'0\\t0.357'",
"b'0\\t0.533'", "b'5\\t0.467'", "b'5\\t0.467'", "b'5\\t0.125'",
"b'5\\t0.4'", "b'5\\t0.214'", "b'5\\t0.4'", "b'10\\t0.067'",
"b'10\\t0.067'", "b'10\\t0.333'", "b'10\\t0.333'", "b'10\\t0.133'",
"b'10\\t0.133'", "b'15\\t0.267'", "b'15\\t0.286'", "b'15\\t0.333'",
"b'15\\t0.214'", "b'15\\t0'", "b'15\\t0'", "b'20\\t0.267'",
"b'20\\t0.2'", "b'20\\t0.267'", "b'20\\t0.437'", "b'20\\t0.077'",
"b'20\\t0.067'", "b'25\\t0.133'", "b'25\\t0.267'", "b'25\\t0.412'",
"b'25\\t0'", "b'25\\t0.067'", "b'25\\t0.133'", "b'30\\t0'",
"b'30\\t0.071'", "b'30\\t0'", "b'30\\t0.067'", "b'30\\t0.067'",
"b'30\\t0.133'"],
dtype='<U16')

最佳答案

感谢所有看我问题的人。我重新启动了 IPython,现在可以毫无问题地执行相同的代码。这是与上面相同的有效代码。

data_float = np.loadtxt(file, delimiter='\t', dtype=float, skiprows=1)

结果:

In [1]: data_float
Out[1]:
array([[ 9.90000000e+01, 6.70000000e-02],
[ 9.90000000e+01, 1.33000000e-01],
[ 9.90000000e+01, 6.70000000e-02],
[ 9.90000000e+01, 0.00000000e+00],
[ 9.90000000e+01, 0.00000000e+00],
[ 0.00000000e+00, 5.00000000e-01],
[ 0.00000000e+00, 4.67000000e-01],
[ 0.00000000e+00, 8.57000000e-01],
[ 0.00000000e+00, 5.00000000e-01],
[ 0.00000000e+00, 3.57000000e-01],
[ 0.00000000e+00, 5.33000000e-01],
[ 5.00000000e+00, 4.67000000e-01],
[ 5.00000000e+00, 4.67000000e-01],
[ 5.00000000e+00, 1.25000000e-01],
[ 5.00000000e+00, 4.00000000e-01],
[ 5.00000000e+00, 2.14000000e-01],
[ 5.00000000e+00, 4.00000000e-01],
[ 1.00000000e+01, 6.70000000e-02],
[ 1.00000000e+01, 6.70000000e-02],
[ 1.00000000e+01, 3.33000000e-01],
[ 1.00000000e+01, 3.33000000e-01],
[ 1.00000000e+01, 1.33000000e-01],
[ 1.00000000e+01, 1.33000000e-01],
[ 1.50000000e+01, 2.67000000e-01],
[ 1.50000000e+01, 2.86000000e-01],
[ 1.50000000e+01, 3.33000000e-01],
[ 1.50000000e+01, 2.14000000e-01],
[ 1.50000000e+01, 0.00000000e+00],
[ 1.50000000e+01, 0.00000000e+00],
[ 2.00000000e+01, 2.67000000e-01],
[ 2.00000000e+01, 2.00000000e-01],
[ 2.00000000e+01, 2.67000000e-01],
[ 2.00000000e+01, 4.37000000e-01],
[ 2.00000000e+01, 7.70000000e-02],
[ 2.00000000e+01, 6.70000000e-02],
[ 2.50000000e+01, 1.33000000e-01],
[ 2.50000000e+01, 2.67000000e-01],
[ 2.50000000e+01, 4.12000000e-01],
[ 2.50000000e+01, 0.00000000e+00],
[ 2.50000000e+01, 6.70000000e-02],
[ 2.50000000e+01, 1.33000000e-01],
[ 3.00000000e+01, 0.00000000e+00],
[ 3.00000000e+01, 7.10000000e-02],
[ 3.00000000e+01, 0.00000000e+00],
[ 3.00000000e+01, 6.70000000e-02],
[ 3.00000000e+01, 6.70000000e-02],
[ 3.00000000e+01, 1.33000000e-01]])

关于python - 使用 numpy loadtxt 将导入字符串转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39185612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com