gpt4 book ai didi

python - 将嵌套数据列表转换为多维 Numpy 数组

转载 作者:太空狗 更新时间:2023-10-30 00:07:51 25 4
gpt4 key购买 nike

在下面的代码中,我在嵌套列表中构建数据。在 for 循环之后,我想要的是尽可能整齐地将它转换为多维 Numpy 数组。但是,当我对其进行数组转换时,它似乎只是将外部列表转换为数组。更糟糕的是,当我继续向下时,我最终得到形状为 (100L,) 的数据点...所以列表数组,其中每个列表都是我的数据(显然我想要一个 (100,3 ))。我也尝试过使用 numpy.asanyarray() 来愚弄,但我似乎无法解决。如果可能的话,我真的很想从一开始就从我的 3d 列表中选择一个 3d 数组。如果不是,我如何才能将列表数组转换为二维数组,而不必迭代并全部转换它们?

编辑:我也愿意接受从一开始就构建数据的更好方法,如果它能让处理更容易的话。但是,它是通过串行端口传输的,并且事先不知道大小。

import numpy as np
import time

data = []
for _i in range(100): #build some list of lists
d = [np.random.rand(), np.random.rand(), np.random.rand()]
data.append([d,time.clock()])

dataArray = np.array(data) #now I have an array of lists of a list(of data) and a time
dataPoints = dataArray[:,0] #this is the data in an array of lists

最佳答案

dataPoints 不是二维列表。先把它转换成一个二维列表,然后它就可以工作了:

d=np.array(dataPoints.tolist())

现在 d 是您想要的 (100,3)。

关于python - 将嵌套数据列表转换为多维 Numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13746955/

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