我想从以下格式的文本文件中加载数据:
萨拉,0.5 0.2 2.0
其中(0.5,0.2,2.0)是描述莎拉的向量。矢量在这里缩短。在实际的文本文件中,矢量跨多行。
我尝试过:
data = np.genfromtxt(filename, dtype =[("label","U10"),("description","f4",(3,))], delimiter = ",")
但是,我最终得到以下错误代码:
ValueError: could not assign tuple of length 2 to structure with 4 fields.
理想情况下,我想要访问的是这样的向量描述:
data["description"]
答案 0 :(得分:0)
我根据hpaulj的第二个建议解决了这个问题:
file=open(filename, "r")
lines = file.readlines()
lines = [x.split("\t") for x in lines]
X = [list(map(float, line[2].split())) for line in lines]