首先,您的导入语句是倒置的。应该是:
from numpy import genfromtxt
其次,显然genfromtxt() 无法将字符串'[ 2. 4. 1120.]' 转换为浮点数,就像它对数组中的所有其他值所做的那样,这就是它返回nan 的原因。
numpy.loadtxt() 也是如此。
不“丢失”这些值的选项可以是使用 pandas 读取 csv 文件:
import numpy as np
import pandas as pd
my_data = pd.read_csv('data.csv').to_numpy()
其中my_data 包含:
array([['[ 2. 4.5 100. ]', 0.0, 0.0, -0.30000000000000004, -0.7, 0.0,
0.0, 99.7002, 0.0, 0.0, -0.30000000000000004, -0.7],
['[ 2. 4. 1130.]', 70.8, 52.2, -672.7, -346.5, 0.0, 0.0, 0.0,
0.0, 0.0, -601.9000000000002, -294.3],
['[ 2. 4.5 110. ]', 0.0, 0.2, -0.7, -0.1, 0.0, 0.0,
99.3010995, 0.0, 0.0, -0.7, 0.1]], dtype=object)
尽管您仍然需要解析第一列上的每个值以将它们转换为 numpy 数组。
为此,您可以使用np.fromstring,但您需要避免使用括号字符才能使其按预期工作。
如果不避免使用括号,您将看到一条错误消息:
np.fromstring(my_data[:, 0], sep=' ')
<ipython-input-65-7d75c8d121f5>:1: DeprecationWarning: string or file could not be read to its end due to unmatched data; this will raise a ValueError in the future.
np.fromstring(my_data[:, 0], sep=' ')
不幸的是,为了避免使用括号,您需要循环数组:
for i, row in enumerate(my_data[:, 0]):
my_data[i, 0] = np.fromstring(data[i, 0][1:-1], sep=' ').astype(np.float32)
通过使用[1:-1] 进行索引,在将值传递给np.fromstring 之前“删除”括号字符。
之后,my_data 将在第一列中包含 numpy 数组:
array([[array([ 2. , 4.5, 100. ], dtype=float32), 0.0, 0.0,
-0.30000000000000004, -0.7, 0.0, 0.0, 99.7002, 0.0, 0.0,
-0.30000000000000004, -0.7],
[array([ 2., 4., 1130.], dtype=float32), 70.8, 52.2, -672.7,
-346.5, 0.0, 0.0, 0.0, 0.0, 0.0, -601.9000000000002, -294.3],
[array([ 2. , 4.5, 110. ], dtype=float32), 0.0, 0.2, -0.7,
-0.1, 0.0, 0.0, 99.3010995, 0.0, 0.0, -0.7, 0.1]], dtype=object)
所以第一列会有:
print(my_data[:, 0])
array([array([ 2. , 4.5, 100. ], dtype=float32),
array([ 2., 4., 1130.], dtype=float32),
array([ 2. , 4.5, 110. ], dtype=float32)], dtype=object)
虽然是一个精心设计的解决方案,但它确实有效。也许有更好或更简单的方法,无需循环数组即可进行转换。