简体   繁体   English

ValueError:无法将字符串转换为float ---如何将字符串列表转换为numpy数组类型float?

[英]ValueError: could not convert string to float---how to convert a list of lists of strings into a numpy array type float?

It looks like I have a malformed numpy array in Python3.x---this was saved as a list of lists of strings. 看起来我在Python3.x中有一个格式错误的numpy数组-将其保存为字符串列表的列表。

foo = [[7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06]
  [1.3130367e-06 2.4584832e-01 2.2375602e-05 7.3299240e-06] [7.2646574e-06 7.1252006e-06 3.0184277e-01 ... 1.0048618e-05 3.1828706e-06 1.0196264e-06]..]

I get the following error trying to read in this data as np.float32 into a numpy array: 我收到以下错误,尝试将np.float32数据np.float32入numpy数组:

np.asarray(foo, dtype=np.float32)

error: 错误:

ValueError: could not convert string to float:[[7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06][1.3130367e-06 2.4584832e-01 2.2375602e-05 7.3299240e-06] [7.2646574e-06 7.1252006e-06 3.0184277e-01 ... 1.0048618e-05 3.1828706e-06 1.0196264e-06]..]

I've tried explicitly converting each list element into a float as follows: 我尝试将每个列表元素显式转换为float,如下所示:

try2 = np.asarray(map(np.float32, foo))

but it snags on a bracket: 但它在支架上有障碍:

ValueError: could not convert string to float: [

What is the recommended way to convert a list of lists of strings into a numpy array, type float? 建议将字符串列表转换为numpy数组(类型为float)的建议方法是什么?

If you replace the spaces with commas, you can use json.loads to read the string as a list, and pass that to np.asarray : 如果用逗号替换空格,则可以使用json.loads将该字符串作为列表读取,并将其传递给np.asarray

import json
import numpy as np

foo = "[[7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06] \
[1.3130367e-06 2.4584832e-01 2.2375602e-05 7.3299240e-06]]"

a = np.asarray(json.loads(foo.replace(" ", ",")), dtype=np.float32)
print(a)
#array([[7.0352220e-01, 5.3130367e-06, 1.5167372e-05, 1.0797821e-06],
#       [1.3130367e-06, 2.4584832e-01, 2.2375602e-05, 7.3299240e-06]])

print(a.dtype)
#float32

This assumes there is exactly 1 space between values. 假定值之间恰好有1个空格。 If that is not the case, you can use re.sub to replace multiple spaces with a comma: 如果不是这种情况,则可以使用re.sub将多个空格替换为逗号:

import re
a = np.asarray(json.loads(re.sub("\s+", ",", foo)))
#array([[7.0352221e-01, 5.3130366e-06, 1.5167372e-05, 1.0797821e-06],
#       [1.3130367e-06, 2.4584831e-01, 2.2375601e-05, 7.3299238e-06]],
#      dtype=float32)

As far as I have seen, np.asarray() works only if dtype has a different datatype from the initial datatype. 据我所知, 仅当 dtype的数据类型与初始数据类型不同时,np.asarray()才起作用。 Please try and remove that argument and see if it works. 请尝试删除该参数,看看是否可行。

How is your string data shaped? 字符串数据的形状如何? Probably the simplest way is to use split() and iterate over the list. 可能最简单的方法是使用split()并遍历列表。 Example (list of lists of strings) that worked for me: 适用于我的示例(字符串列表列表):

foo = [['7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06'],
       ['7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06']]
arr = np.array([[value.split() for value in row][0] for row in foo], dtype='<f8')

(Note: the [0] is used as split creates a list itself. You can use np.reshape in alternative) (注意:[0]用作拆分本身会创建一个列表。您可以使用np.reshape替代方法)

EDIT: if its a string representation (not a list of strings as stated in the OP): 编辑:如果它是字符串表示形式(不是OP中所述的字符串列表):

foo = '[[7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06][7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06]'
arr=np.array([line.split() for line in foo.replace('[','').replace(']]','').split(']')], dtype='<f8')

Given: 鉴于:

foo = [['7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06'],
       ['1.3130367e-06 2.4584832e-01 2.2375602e-05 7.3299240e-06'], 
       ['7.2646574e-06 7.1252006e-06 3.0184277e-01 1.0048618e-05']]

Try this to split each string 试试这个拆分每个字符串

foo = [row[i].split() for row in foo for i in range(len(foo[0]))]

This for changing type to floats. 这种改变类型为浮动。

foo = [[float(row[i]) for i in range(len(foo[0]))] for row in foo]

print(type(foo[0][1]))

>> float

Then turn it into a numpy array: 然后将其转换为一个numpy数组:

foo = np.array(foo)

print(type(foo[0][1]))

>> numpy.float64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM